2017-10-11 17 views
2

一部の画像をPDDocumentオブジェクトに変換し、ハードウェアに保存しません。 このPDDocumentオブジェクトから入力ストリームを取得する方法は? 私は吹き飛ばすように書いています。「ストリームに書き込む前にデータを書き込まずに、InputStreamを作成する」。エラー。pdfbox画像をpdfファイルに変換

源の一部である:

public ByteArrayOutputStream imagesToPdf(final List<ImageEntity> images) throws IOException { 

    final PDDocument doc = new PDDocument(); 

    final int count = images.size(); 
    InputStream in = null; 
    PDPageContentStream contentStream = null; 
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    try { 

     for(int i = 0; i < count; i ++) { 

      final ImageEntity image = images.get(i); 

      byte[] byteCode = image.getByteCode(); 

      in = new ByteArrayInputStream(byteCode); 

      BufferedImage bi = ImageIO.read(in); 
      float width = bi.getWidth(); 
      float height = bi.getHeight(); 
      PDPage page = new PDPage(new PDRectangle(width, height)); 
      doc.addPage(page); 

      PDImageXObject pdImage = PDImageXObject.createFromByteArray(doc, byteCode, null); 
      contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true); 

      float scale = 1f; 
      contentStream.drawImage(pdImage, 0, 0, pdImage.getWidth()*scale, pdImage.getHeight()*scale); 

      IOUtils.closeQuietly(contentStream); 
      IOUtils.closeQuietly(in); 
     } 
     PDStream ps = new PDStream(doc); 
     is = ps.createInputStream(); 
     IOUtils.copy(is, baos); 
     return baos; 

    } finally { 

     IOUtils.closeQuietly(contentStream); 
     IOUtils.closeQuietly(in); 
    } 
} 

答えて

1
new PDStream(doc) 

はあなたが想定するようdocがシリアライズ形式で取得することができ、そこからオブジェクトを作成できませんありません。実際には、文書docに属するPDFストリームオブジェクトを作成します。あなたが何をしたいか

は単に

doc.save(baos); 
+0

おかげで、それが動作です。 – user1737352

+0

@ user1737352素晴らしい!その場合は、回答を受け入れたものとしてマークしてください(左上のチェックマークをクリックしてください)。 – mkl

+0

残念ですが、私の評判が15未満であるため、左上の三角形のシンボルをクリックすることはできません。 – user1737352

関連する問題