4

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.

Ankur Verma
  • 5,793
  • 12
  • 57
  • 93
  • http://stackoverflow.com/questions/3335126/itext-add-content-to-existing-pdf-file Very good suggestions present! – S.P. Mar 07 '12 at 07:32
  • I saw this page but didn't get anything prior to my requirement it says creating new pdf rather that updating...... – Ankur Verma Mar 07 '12 at 07:35
  • Did you see Mark's suggestion where he says that annotations etc cannot be supported in this way? Also, in the above case I have a feeling that 2 streams to the same file one reading and the other writing does create a problem. – S.P. Mar 07 '12 at 07:37
  • I read that, I don't want to go that deep in annotations,bookmarks and all that I want to add a simple content, say "Hello how are you".Thanks. – Ankur Verma Mar 07 '12 at 07:54

2 Answers2

4

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);
Xavier Delamotte
  • 3,519
  • 19
  • 30
0

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();
            }
Tunde Pizzle
  • 787
  • 1
  • 9
  • 18