Appending a data in itext in existing pdf

2019-03-31 09:40发布

问题:

I am working with itext pdf library. I want to add a content at the end of the existing pdf.

Say for example the existing pdf(say Original.pdf) is having say 4 pages, so I want to add another page i.e. page no 5 with content Hello World I am added content and save it in the same pdf i.e. Original.pdf

So after closing my Original.pdf will contain 5 pages i.e 4 pages(with default content they already have) + 1 page with content Hello World I am added content

I am using this code but showing an exception

        String in="Original.pdf";
        String out="Original.pdf";        

        PdfReader reader = new PdfReader(in);
        PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(out));

        int totalPages=reader.getNumberOfPages();
        stamper.insertPage(totalPages+1, PageSize.A4);
        stamper.addAnnotation(
                                PdfAnnotation.createText(
                                                            stamper.getWriter(),
                                                            new Rectangle(30f, 750f, 80f, 800f),
                                                            "inserted page", "This page is the title page.",
                                                            true,
                                                            null)
                                ,
                                reader.getNumberOfPages()
                             );
        stamper.close();

java.io.EOFException

Thanks in advance.

回答1:

I think the problem comes from the fact that you are using a FileOutputStream and a FileInputStream on the same file.

I would recommand to save on ByteArrayOutputStream the pdf, close the stamper, and then save the ByteArrayOutputStream in your file.

I've used IOUtils.write(byte[] data, OutputStream output) method to save the ByteArrayOutputStream in the FileOutputStream .

I've tested this and it works:

    String in = "Original.pdf";
    String out = "Original.pdf";

    PdfReader reader = new PdfReader(in);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfStamper stamper = new PdfStamper(reader, baos );

    int totalPages = reader.getNumberOfPages();
    stamper.insertPage(totalPages + 1, PageSize.A4);
    stamper.addAnnotation(PdfAnnotation.createText(stamper.getWriter(), new Rectangle(30f, 750f, 80f, 800f), "inserted page", "This page is the title page.", true, null),
            reader.getNumberOfPages());
    stamper.close();

    FileOutputStream fileOutputStream = new FileOutputStream(out);
    IOUtils.write(baos.toByteArray(), fileOutputStream);


回答2:

Well You can do something like this.

            String out="Original.pdf";
            File oldFile = new File(out);
            try {
                Document document = new Document();
                PdfCopy filePdfCopy = new PdfCopy(document,
                        new FileOutputStream(oldFile, true));
                document.open();
                PdfReader reader = new PdfReader(newFile.getAbsolutePath());
                PdfReader reader_old = new PdfReader(
                        oldFile.getAbsolutePath());
                filePdfCopy.addDocument(reader);
                filePdfCopy.addDocument(reader_old);
                filePdfCopy.close();
                reader.close();
                reader_old.close();
                document.close();
                stats.addMergedPdf();
            } catch (FileNotFoundException e) {
                logger.error("FileNotFoundException: ", e);
                stats.addError();
            } catch (DocumentException e) {
                logger.error("DocumentException: ", e);
                stats.addError();
            } catch (IOException e) {
                logger.error("IOException: ", e);
                stats.addError();
            }


标签: java itext