Adding data to itext to existing pdf

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

Let's say for example an existing pdf (say Original.pdf) says 4 pages, so I want to add another page, i.e. page # 5 with Hello World content I added content and saved it in the same pdf format i.e. Original.pdf

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

I am using this code but showing 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.

+3


source to share


2 answers


I think the problem is with what you are using FileOutputStream

and FileInputStream

in the same file.

I would recommend saving to ByteArrayOutputStream pdf, close the template and then saving ByteArrayOutputStream

to your file.

I used IOUtils.write (byte data [], OutputStream output) to save ByteArrayOutputStream

to FileOutputStream

.



I 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);

      

+4


source


Good. 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();
            }

      

0


source







All Articles