如何正确合并文件?

时间:2014-02-12 14:58:33

标签: java pdf pdf-generation itext

合并后打印pdf文件时出现以下问题,pdf文档被截断。 有时会发生这种情况,因为文档不是8.5 x 11 它们可能像11 x 17。

我们可以让它检测页面大小,然后对这些文档使用相同的页面大小吗? 或者,如果没有,是否可以让它适合页面?

以下是代码:

package com.sumit.program;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class MergePdf {

    public static void main(String[] args) {
        try {
            List<InputStream> pdfs = new ArrayList<InputStream>();

            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Document1.pdf"));
            pdfs.add(new FileInputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\Landscape.pdf"));           
            OutputStream output = new FileOutputStream("C:\\Documents and Settings\\Sumit\\Desktop\\NewEcnProject\\merge1.pdf");
            MergePdf.concatPDFs(pdfs, output, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void concatPDFs(List<InputStream> streamOfPDFFiles,
            OutputStream outputStream, boolean paginate) {

        Document document = new Document();
        try {
            List<InputStream> pdfs = streamOfPDFFiles;
            List<PdfReader> readers = new ArrayList<PdfReader>();
            int totalPages = 0;
            Iterator<InputStream> iteratorPDFs = pdfs.iterator();

            // Create Readers for the pdfs.
            int i=1;
            while (iteratorPDFs.hasNext()) {
                InputStream pdf = iteratorPDFs.next();
                PdfReader pdfReader = new PdfReader(pdf);
                System.out.println("Page size is "+pdfReader.getPageSize(1));
                readers.add(pdfReader);
                totalPages += pdfReader.getNumberOfPages();
                i++;
            }
            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            writer.setCompressionLevel(9);
            document.open();
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
                    BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data

            PdfImportedPage page;
            int currentPageNumber = 0;
            int pageOfCurrentReaderPDF = 0;
            Iterator<PdfReader> iteratorPDFReader = readers.iterator();

            // Loop through the PDF files and add to the output.
            while (iteratorPDFReader.hasNext()) {
                PdfReader pdfReader = iteratorPDFReader.next();

                // Create a new page in the target for each source page.
                System.out.println("No. of pages "+pdfReader.getNumberOfPages());
               i=0;
                while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                    Rectangle r=pdfReader.getPageSize(pdfReader.getPageN(pageOfCurrentReaderPDF+1));
                    if(r.getWidth()==792.0 && r.getHeight()==612.0)
                        document.setPageSize(PageSize.A4.rotate());
                    else
                        document.setPageSize(PageSize.A4);
                    document.newPage();
                    pageOfCurrentReaderPDF++;
                    currentPageNumber++;
                    i++;

                    page = writer.getImportedPage(pdfReader,
                            pageOfCurrentReaderPDF);
                    System.out.println("Width is "+page.getWidth());
                    System.out.println("Height is "+page.getHeight());
                    cb.newlineText();
                    cb.addTemplate(page, 0, 0);

                    // Code for pagination.
                    if (paginate) {
                        cb.beginText();
                        cb.setFontAndSize(bf, 9);
                        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                                + currentPageNumber + " of " + totalPages, 520,
                                5, 0);
                        cb.endText();
                    }
                }
                pageOfCurrentReaderPDF = 0;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
            System.out.println("Merging of Pdfs is done.......");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen())
                document.close();
            try {
                if (outputStream != null)
                    outputStream.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:5)

DocumentPdfWriter类与addTemplate()方法结合使用来合并文档是一个坏主意。这不是addTemplate()方法的意思。您已明确或隐式地定义了您正在使用的Document的页面大小。使用addTemplate()方法,您可以添加PdfImportedPage个实例和

  • 当您添加具有相同页面大小和轮换的新页面时,您会丢弃该页面中存在的所有交互性,但一切都很顺利,
  • 当您添加具有不同页面大小和轮换的新页面时,您将获得您描述的结果。由于大小不同,导入的页面和新页面不匹配。零件被切断,出现额外的边距,旋转不同等等。

chapter 6 of my book解释了这一点。您应该使用PdfCopy代替PdfWriter。例如,参见FillFlattenMerge2示例:

Document document = new Document();
PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
PdfReader reader;
String line = br.readLine();
// loop over readers
    // add the PDF to PdfCopy
    reader = new PdfReader(baos.toByteArray());
    copy.addDocument(reader);
    reader.close();
// end loop
document.close();

在您的情况下,您还需要添加页码,您可以在一秒钟内完成此操作,如StampPageXofY示例中所述:

PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfContentByte pagecontent;
for (int i = 0; i < n; ) {
    pagecontent = stamper.getOverContent(++i);
    ColumnText.showTextAligned(pagecontent, Element.ALIGN_RIGHT,
            new Phrase(String.format("page %s of %s", i, n)), 559, 806, 0);
}
stamper.close();
reader.close();

或者您可以在合并时添加它们,如MergeWithToc示例中所做的那样。

Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
PageStamp stamp;
document.open();
int n;
int pageNo = 0;
PdfImportedPage page;
Chunk chunk;
for (Map.Entry<String, PdfReader> entry : filesToMerge.entrySet()) {
    n = entry.getValue().getNumberOfPages();
    for (int i = 0; i < n; ) {
        pageNo++;
        page = copy.getImportedPage(entry.getValue(), ++i);
        stamp = copy.createPageStamp(page);
        chunk = new Chunk(String.format("Page %d", pageNo));
        if (i == 1)
            chunk.setLocalDestination("p" + pageNo);
        ColumnText.showTextAligned(stamp.getUnderContent(),
                Element.ALIGN_RIGHT, new Phrase(chunk),
                559, 810, 0);
        stamp.alterContents();
        copy.addPage(page);
    }
}
document.close();
for (PdfReader r : filesToMerge.values()) {
    r.close();
}
reader.close();

我强烈反对使用PdfWriter合并文档!如果你在Document课程中更改页面大小和页面轮换,这并非不可能,但是你自己也会变得更难。此外:使用PdfWriter还会丢弃您正在合并的页面中存在的所有交互性(链接,注释......)。您的客户可能会将其视为错误。