2015-12-28 27 views
1

与PDFBox的-的Android,PDFBOX-机器人卡住:1.8.9.0PDFBOX-Android的页面添加文本叠加

我打开一个PDF文件的第一页,在书写文字和导入此页到最后的一个新的一页文件。

问题是创建新的页面时,它使用最后一个页面包含以前的文字...
所以,第一页是好的,但也的nextS叠加文本..

private File writeReport() { 
    File fileSource = new File(getActivity().getApplicationContext().getExternalCacheDir(), "fileSource.pdf"); 

    // get file model in assets 
    InputStream inputAsset = null; 
    try { 
     inputAsset = getActivity().getApplicationContext().getResources().getAssets().open("file_model.pdf"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // copy file model in fileSource 
    try { 
     OutputStream outputStream = new FileOutputStream(file); 
     byte buffer[] = new byte[1024]; 
     int length = 0; 

     while ((length = inputAsset.read(buffer)) > 0) { 
      outputStream.write(buffer, 0, length); 
     } 
     outputStream.close(); 
     inputAsset.close(); 
    } catch (IOException e) { 
     System.out.print("Copy assets : IOException" + e.getMessage()); 
    } 

    File fileTarget = new File(getActivity().getApplicationContext().getCacheDir(), "fileTarget.pdf"); 


    try { 
     PDFBoxResourceLoader.init(getActivity().getApplicationContext());  // init lib 

     PDDocument documentSource = PDDocument.load(fileSource); 
     PDDocument documentTarget = new PDDocument(); 

     // iteration == a new page 
     for(int i=0 ; i < 3 ; i++) 
     { 
      PDPage page = documentSource.getDocumentCatalog().getPages().get(0); 
      PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true); 

      PDFont font1 = PDType1Font.HELVETICA; 

      float startY = page.getMediaBox().getUpperRightY(); 
      float factor = 2.83f; 

      page.getStream(); 

      // add text 
      contentStream.beginText(); 
      contentStream.setFont(font1, 10); 
      contentStream.newLineAtOffset(factor * 60, startY - (factor * 53)); 
      contentStream.showText("test text" + i); 
      contentStream.endText(); 

      contentStream.close(); 

      // import source page to output file-> Problem here ! new page contain old overlay text... 
      documentTarget.importPage(page); 
     } 
     documentTarget.save(fileTarget); 
     documentTarget.close(); 
     documentSource.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return fileTarget; 
} 

有没有一种办法每次迭代都有新的页面? 谢谢!

+0

“它使用包含以前文本的最后一页”来源或目标页面的最后一页? “以前的文字”是什么?以前的迭代?如果是这样,那么它就像预期的那样,因为你正在修改同一页PDPage对象。 –

+0

更好的解决方案(请先测试一下)将首先导入页面,获取返回的PDPage(它现在是一个新对象),并使用该参数添加额外的内容流。 –

+0

感谢您的回复! - 它使用包含上一次迭代文本的最后一个_destination_页面。 我在想PDPage是在每次迭代中新创建的,因为在循环中声明了... - 我已经尝试了您的解决方案但没有成功:结果相同... – partout

回答

0

问题是您重复使用了相同的PDPage对象,导致它包含上一次迭代的文本。解决方法:使用

documentTarget.importPage(page) 

结果,并与工作之一。因为这是一个克隆所有东西的新PDPage对象。因此,您的新代码将如下所示:

// iteration == a new page 
    for(int i=0 ; i < 3 ; i++) 
    { 
     PDPage page = documentSource.getDocumentCatalog().getPages().get(0); 
     page = documentTarget.importPage(page); // this is now a new PDPage object 
     PDPageContentStream contentStream = new PDPageContentStream(documentSource, page, true, true); 

     PDFont font1 = PDType1Font.HELVETICA; 

     float startY = page.getMediaBox().getUpperRightY(); 
     float factor = 2.83f; 

     page.getStream(); 

     // add text 
     contentStream.beginText(); 
     contentStream.setFont(font1, 10); 
     contentStream.newLineAtOffset(factor * 60, startY - (factor * 53)); 
     contentStream.showText("test text" + i); 
     contentStream.endText(); 

     contentStream.close();    
    }