0

2つのPDFを1つにマージしようとしています。マージは正常に機能していますが、PDFページからコンテンツがオーバーフローします。添付ファイルに示されています。元のドキュメントのpdfは次のとおりです。

マージ前の元のドキュメント

マージドキュメントがこのようになったら ここに画像の説明を入力してください

次のようなJavaコード:

BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
        //BaseFont bf= BaseFont.createFont();
        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.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader,
                        pageOfCurrentReaderPDF);
                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;
        }

助けてください。

4

2 に答える 2

2

私の本の第6章をダウンロードして、表6.1を見てください。PdfWriter文書化されているように使用する代わりに、を使用して2つの文書をマージするのを間違えてPdfCopyいます。を使用するときにページ番号を追加する方法については、リスト6.22を参照してくださいPdfCopy

于 2013-02-08T11:34:44.000 に答える
1

次のように「PdfCopyFields」スニペットを使用しました。

public static boolean concatPDFFiles(List<String> listOfFiles,
        String outputfilepath) throws FileNotFoundException, DocumentException {
    PdfCopyFields copy = null;
    try {
        copy = new PdfCopyFields(new FileOutputStream(outputfilepath));
    } catch (DocumentException ex) {
        Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        for (String fileName : listOfFiles) {
            PdfReader reader1 = new PdfReader(fileName);
            copy.addDocument(reader1);
        }
    } catch (IOException ex) {
        Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        copy.close();
    }
    if (new File(outputfilepath).exists()) {
        double bytes = new File(outputfilepath).length();
        //double kilobytes = (bytes / 1024);
        if (bytes != 0) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
于 2013-02-14T05:49:30.340 に答える