-1

iText APIを使用して2つ以上のPDFドキュメントを1つにマージしてpdfファイルをマージしようとしています.しかし、結果として0バイトサイズのマージpdfを取得しています.以下に示すようにコードを投稿します.iText.jarファイルも試しましたしかし、同じ0サイズのpdfを提供します。

そして、これを取得しました:-「メソッドcom.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapesから参照されたクラス 'com.itextpdf.text.pdf.PdfPrinterGraphics2D'が見つかりませんでした」. それでも私は成功していません。

コード:

public class ItextMerge {
        public static void main() {
            List<InputStream> list = new ArrayList<InputStream>();
            try {
                // Source pdfs
                list.add(new FileInputStream(new File("mnt/sdcard/nocturia.pdf")));
                list.add(new FileInputStream(new File("mnt/sdcard/Professional Android Application Development.pdf")));

                // Resulting pdf
                OutputStream out = new FileOutputStream(new File("mnt/sdcard/newmerge.pdf"));

                doMerge(list, out);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * Merge multiple pdf into one pdf
         * 
         * @param list
         *            of pdf input stream
         * @param outputStream
         *            output file output stream
         * @throws DocumentException
         * @throws IOException
         */
        public static void doMerge(List<InputStream> list, OutputStream outputStream)
                throws DocumentException, IOException {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            PdfContentByte cb = writer.getDirectContent();

            for (InputStream in : list) {
                PdfReader reader = new PdfReader(in);
                for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                    document.newPage();
                    //import the page from source pdf
                    PdfImportedPage page = writer.getImportedPage(reader, i);
                    //add the page to the destination pdf
    //                cb.addTemplate(page, 0, 0);
    //                cb.addTemplate(page, 0, 0);
                }
            }

            outputStream.flush();
            document.close();
            outputStream.close();
        }
    }

何か案が?

ありがとうございました

4

3 に答える 3

3

あなたの質問に対する正しい答えであるため、私はマイケルの答えを支持しました。

しかし、あなたのコードを読むと、あなたが気付いていない別の問題があります: PDF をマージするために間違ったコードを使用しています。PdfCopyまたはPdfSmartCopyを使用する必要がありますPdfWriter

これは以前に何度も説明されています:

PdfWriter を使用しているという事実は、ドキュメントを読んでいないことを示しています。

さらに、あなたの質問は、ロワジーが人の名前であることを知らないように聞こえます。実際、それは私の名前であり、誰かがLowagie iText API が機能していないと言うのは非常に厄介です。まず第一に、私は古いバージョンの iText の使用をやめるよう求めてきましたが、それは個人的な非難のように聞こえ、製品を人間と混同しているためです。lowgie と iText の違いは何ですか? を参照してください。

于 2013-05-03T08:46:57.490 に答える
1

以下は、2 つの pdf ファイルをマージするための簡単なコードです。

    try{
        String doc1 = FOLDER_PATH + "Doc1.pdf";
        String doc2 = FOLDER_PATH + "Doc2.pdf";
        String resultDocFile = FOLDER_PATH + "ResultDoc.pdf";

        PdfReader reader1 = new PdfReader(doc1);
        Document resultDoc = new Document();
        PdfCopy copy = new PdfCopy(resultDoc, new FileOutputStream(resultDocFile));
        resultDoc.open();
        //Copying First Document
        for(int i = 1; i <= reader1.getNumberOfPages(); i++)    {
            copy.addPage(copy.getImportedPage(reader1, i));
        }
        PdfReader reader2 = new PdfReader(doc2);
        //Copying Second Document
        for(int i = 1; i <= reader2.getNumberOfPages(); i++)    {
            copy.addPage(copy.getImportedPage(reader2, i));
        }
        resultDoc.close();
    } catch (Exception e){
        e.printStackTrace(); 
    }
于 2013-09-05T06:14:45.047 に答える