iText - 如何将页面添加到使用PdfCopy创建的文档中 [英] iText - how to add pages to a document created with PdfCopy

查看:1183
本文介绍了iText - 如何将页面添加到使用PdfCopy创建的文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText(特别是iTextSharp 4.1.6),我想通过组合现有PDF中的页面来创建PDF,还要插入从图像创建的新页面。



<我分别使用PdfCopy和PdfWriter将这两部分分开工作。从图像创建页面的代码如下所示:

  PdfWriter pw = PdfWriter.GetInstance(doc,outputStream); 
Image img = Image.GetInstance(inputStream);
doc.Add(img);
doc.NewPage();

现在,由于PdfCopy继承自PdfWriter,我以为我能够添加这样的图像页面使用相同的技术到我的PdfCopy对象,但它不起作用(如果在上面的例子中实例化PdfCopy而不是PdfWriter,页面上没有任何内容)。



从快速查看源代码我注意到,当PdfCopy的contstructor调用超类构造函数时,它会使用一个新的Document对象,而不是传入的对象,所以我猜这就是原因。



还有更好的方法吗?目前,我最好的猜测是使用PdfWriter从图像创建单页Pdf,然后使用PdfCopy将其添加到文档中,但这似乎是一种解决方法。

解决方案

我最近遇到过这个问题,这里的答案实际上并没有用。我的用例基本上是获取一堆PDF和图像(.jpg,.png等)并将它们全部合并为1个PDF。我不得不使用PdfCopy,因为它保留了表单字段和标签之类的东西,其中PdfWriter没有。



基本上,因为PdfCopy不允许你用addPage()创建新页面,你必须使用页面上的图像在内存中创建一个新的PDF,然后使用PdfCopy从该PDF中复制出页面。



例如:

 文件pdfDocument = new Document(); 
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(pdfDocument,pdfOutputStream);

pdfDocument.open();

for(文件文件:allFiles){
if(/ * file is PDF * /){
/ *将PDF文件中的所有页面复制到新PDF * /
PdfReader reader = new PdfReader(file.getAllBytes());
for(int i = 1; i< = reader.getNumberOfPages(); i ++){
copy.addPage(copy.getImportedPage(reader,i);
}
} else {
/ * File is image。在内存中创建一个新的PDF,将图像写入其第一页,然后使用PdfCopy将第一页复制回主PDF * /
Document imageDocument = new Document();
ByteArrayOutputStream imageDocumentOutputStream = new ByteArrayOutputStream();
PdfWriter imageDocumentWriter = PdfWriter.getInstance(imageDocument,imageDocumentOutputStream);

imageDocument.open();

if(imageDocument.newPage()){

image = Image.getInstance(file.getAllBytes());

if(!imageDocument.add (图片)){
抛出新的异常(无法将图像添加到页面!);
}

imageDocument.close();
imageDocumentWriter.close();

PdfReader imageDocumentReader = new PdfReader(imageDocumentOutputStream.toByteArray());

copy.addPage(copy.getImportedPage(imageDocumentReader,1));

imageDocumentReader.close();
}

}


I am using iText (specifically iTextSharp 4.1.6) and I want to create a PDF by combining pages from existing PDFs but also inserting new pages created from an image.

I have got these two parts working separately using PdfCopy and PdfWriter respectively. The code to create a page from an image looks like this:

PdfWriter pw = PdfWriter.GetInstance(doc, outputStream);
Image img = Image.GetInstance(inputStream);
doc.Add(img);
doc.NewPage();

Now, Since PdfCopy inherits from PdfWriter, I thought I would be able to add such "image pages" to my PdfCopy object using the same technique, but it doesn't work (if you instantiate a PdfCopy instead of a PdfWriter in the above example, nothing comes out on the page).

From a quick peek at the source code I notice that when the contstructor for PdfCopy calls the superclass constructor it does so with a new Document object, not the one passed in, so I guess this is the reason.

Is there a better way to go about this? At the moment my best guess is to create a single page Pdf from the image using PdfWriter and then add it to the document using PdfCopy, but that seems like a bit of a workaround.

解决方案

I recently had this issue, and the answers here arent actually that helpful. My use-case was basically "Take a bunch of PDFs and images (.jpg, .png etc) and combine them all into 1 PDF". I had to use PdfCopy because it preserves things like form fields and labels, where PdfWriter doesnt.

Basically, because PdfCopy wont allow you to create new pages with addPage(), you have to create a new PDF in memory with the image on the page, and then use PdfCopy to copy out the page from that PDF.

For example:

    Document pdfDocument = new Document();
    ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
    PdfCopy copy = new PdfCopy(pdfDocument, pdfOutputStream);

    pdfDocument.open();

    for (File file : allFiles) {
       if (/* file is PDF */) {
           /* Copy all the pages in the PDF file into the new PDF */
           PdfReader reader = new PdfReader(file.getAllBytes());
           for (int i = 1; i <= reader.getNumberOfPages(); i++) {
              copy.addPage(copy.getImportedPage(reader, i);
           }
       } else {
           /* File is image. Create a new PDF in memory, write the image to its first page, and then use PdfCopy to copy that first page back into the main PDF */
           Document imageDocument = new Document();
           ByteArrayOutputStream imageDocumentOutputStream = new ByteArrayOutputStream();
           PdfWriter imageDocumentWriter = PdfWriter.getInstance(imageDocument, imageDocumentOutputStream);

            imageDocument.open();

            if (imageDocument.newPage()) {

                image = Image.getInstance(file.getAllBytes());

                if (!imageDocument.add(image)) {
                   throw new Exception("Unable to add image to page!");
                }

                imageDocument.close();
                imageDocumentWriter.close();

                PdfReader imageDocumentReader = new PdfReader(imageDocumentOutputStream.toByteArray());

                copy.addPage(copy.getImportedPage(imageDocumentReader, 1));

                imageDocumentReader.close();
         }

     }

这篇关于iText - 如何将页面添加到使用PdfCopy创建的文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆