mandar.jagtap,
Here is some code I wrote based on the iText examples.

I am pretty sure if you use PdfReader and PdfStamper you will keep all your 
bookmarks, etc.

but you have to extract them from the reader and add them to the stamper.

I have tested this with success on PDFs rendered from FOP, Word Perfect, and 
XSL Formatter.

All links and bookmarks are retained. (code below)

Anyway, I modified code I found in the iText examples and read the portions in 
the iText book.

My intention here was to add Metadata tags to the PDF.

All you need is to extract the dictionary from the reader [ reader.getInfo() ] 
then put it back into the stamper [stp.setMoreInfo() ] .

Sincerely,
-mike

@SuppressWarnings("unchecked")
private void improvePDF() {
    try {
      PdfReader reader = new PdfReader(url, ownerPassword.getBytes());
      HashMap<String, String> meta = reader.getInfo();
      PdfStamper stp = new PdfStamper(
          reader, 
          new FileOutputStream(outputFileName), 
          PdfWriter.VERSION_1_5
      );
      log.debug("Encrypt: " + Boolean.toString(encrypt));
      if(encrypt) {
            stp.setEncryption(
                "".getBytes(), //user password
                ownerPassword.getBytes(), 
                PdfWriter.ALLOW_PRINTING | 
                PdfWriter.ALLOW_COPY | 
                PdfWriter.ALLOW_SCREENREADERS,  
                PdfWriter.STANDARD_ENCRYPTION_128
            );
        } else {
        /*
            stp.setEncryption(
                "".getBytes(), //user password
                "".getBytes(), //main password
                PdfWriter.ALLOW_ASSEMBLY | 
                PdfWriter.ALLOW_COPY | 
                PdfWriter.ALLOW_FILL_IN | 
                PdfWriter.ALLOW_MODIFY_ANNOTATIONS | 
                PdfWriter.ALLOW_MODIFY_CONTENTS | 
                PdfWriter.ALLOW_PRINTING | 
                PdfWriter.ALLOW_SCREENREADERS,  
                PdfWriter.STANDARD_ENCRYPTION_128
            );
        */
        }//end if
      meta.put(PdfName.TITLE, bmt.getTitle());
      meta.put(PdfName.SUBJECT, bmt.getSubject());
      meta.put(PdfName.AUTHOR, bmt.getAuthor());
      meta.put(PdfName.KEYWORDS, bmt.getKeywords());
      meta.put(PdfName.PRODUCER, bmt.getProducer());
      meta.put(PdfName.CREATOR, bmt.getCreator());
      meta.put(ElementTags.CREATIONDATE, bmt.getCreationDate());
      meta.put(PdfName.MODDATE, bmt.getModDate());
      stp.setMoreInfo(meta); 
        stp.setXmpMetadata((xmp.getBaos()).toByteArray());
        stp.close();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    }//end try-catch
  }//end method




________________________________
From: 1T3XT info <[email protected]>
To: Post all your questions about iText here 
<[email protected]>
Sent: Thursday, February 19, 2009 10:21:27 AM
Subject: Re: [iText-questions] Need to importpage to target pdf alongwith all 
the cross-references in existing pdf file

mandar.jagtap wrote:
> Can we preserve the cross-references, bookmarks, toc if we use PdfCopy and
> Pdfwriter together?

No, but there are alternatives.

> If so could give a hint on that as per following code? I
> already tried with PdfStamper. It doesn't work for page-number other than 1.

It's a bug that could be easily fixed.
-- 
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php



      
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php

Reply via email to