4

私たちが使用しているレガシー ライブラリによってオンザフライで生成され、ディスクに書き込まれる 3 つの PDF ドキュメントがあります。私の Java サーバー コードがこれら 3 つのドキュメントを取得して、ドキュメント #1 のすべてのページとドキュメント #2 のすべてのページが続く 1 つの長い PDF ドキュメントに変換する最も簡単な方法は何ですか?

理想的には、これをメモリ内で発生させて、クライアントにストリームとして返すことができるようにしたいのですが、ディスクに書き込むこともオプションです。

4

6 に答える 6

4

@JD OConal、ヒントをありがとう、あなたが私に送った記事は非常に時代遅れでしたが、それは私をiTextに向けさせました. 必要なことを正確に行う方法を説明するこのページを見つけました: http://java-x.blogspot.com/2006/11/merge-pdf-files-with-itext.html

他の回答に感謝しますが、回避できる場合は他のプロセスを生成する必要はありません。また、プロジェクトには既に itext.jar があるため、外部依存関係を追加していません。

最終的に書いたコードは次のとおりです。

public class PdfMergeHelper {

    /**
     * Merges the passed in PDFs, in the order that they are listed in the java.util.List.
     * Writes the resulting PDF out to the OutputStream provided.
     * 
     * Sample Usage:
     * List<InputStream> pdfs = new ArrayList<InputStream>();
     * pdfs.add(new FileInputStream("/location/of/pdf/OQS_FRSv1.5.pdf"));
     * pdfs.add(new FileInputStream("/location/of/pdf/PPFP-Contract_Genericv0.5.pdf"));
     * pdfs.add(new FileInputStream("/location/of/pdf/PPFP-Quotev0.6.pdf"));
     * FileOutputStream output = new FileOutputStream("/location/to/write/to/merge.pdf");
     * PdfMergeHelper.concatPDFs(pdfs, output, true);
     * 
     * @param streamOfPDFFiles the list of files to merge, in the order that they should be merged
     * @param outputStream the output stream to write the merged PDF to
     * @param paginate true if you want page numbers to appear at the bottom of each page, false otherwise
     */
    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.
            while (iteratorPDFs.hasNext()) {
                InputStream pdf = iteratorPDFs.next();
                PdfReader pdfReader = new PdfReader(pdf);
                readers.add(pdfReader);
                totalPages += pdfReader.getNumberOfPages();
            }
            // Create a writer for the outputstream
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);

            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.
                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;
            }
            outputStream.flush();
            document.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (document.isOpen()) {
                document.close();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
于 2008-09-18T06:31:43.837 に答える
2

私はpdftkを使って大きな効果を上げました。これは、Java アプリから実行する必要がある外部アプリケーションです。

于 2008-09-18T05:51:40.503 に答える
2

iText は変更されたようで、現在は商用ライセンスの要件があり、あまり役に立ちません (ドキュメントが必要ですか? 私たちの本を購入してください!)。

最終的に PDFSharp http://www.pdfsharp.net/を見つけて使用しました。複数の PDF ドキュメントを連結するためのサンプルは、シンプルでわかりやすいものです: http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx

ランダムに楽しむ

于 2012-05-22T06:02:33.637 に答える
1

iText PdfCopy

于 2008-09-18T08:12:28.293 に答える
1

Java オープン ソース PDF ライブラリのリストをご覧ください。

こちらの記事もご覧ください。

[編集: 使いやすい Ghostscript は常にありますが、より多くの依存関係が必要な人はいますか?]

于 2008-09-18T05:56:33.703 に答える
0

PDFBoxは、これを実現するための最も簡単な方法です。コード内にPDFMergerというユーティリティがあり、非常に簡単です。forループと2行のコードだけで、すべて完了しました:)

于 2012-08-20T02:52:25.713 に答える