Howdy all,
I need to take an existing pdf document and change the title. Since this will be
used dynamically on a website, some of the pdfs will have forms and some won't.
Some will have restrictions (no printing, no saving, etc.) that I need to
preserve and some won't.

I can create a new pdf based on the original using PdfCopy and that allows me to
set the title the way I want. But the process doesn't seem to automatically
preserve any additional metadata like author, subject and keywords. It doesn't
preserve permissions like printing/saving/etc. and it doesn't seem to preserve
tags. When used on a PDF with a form in it, the form is no longer usable as a
form (almost as if it got flattened). List below is basically what I'm currently
doing (without any error handling). 

Is there a better way to do this? Such that I can basically create an IDENTICAL
pdf where the only thing different is the title? Any advice would be greatly
appreciated.

---snip---
PdfReader reader = new PdfReader("/mypath/original.pdf");

OutputStream out = new BufferedOutputStream(new
FileOutputStream("/mypath/mycopy.pdf"));

Document doc = new Document(reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(doc, out);
copy.setEncryption(null, null, permissions, reader.getPermissions());

HashMap infoDict = reader.getInfo();
if (infoDict.containsKey("Author"))
{
  doc.addAuthor(infoDict.get("Author").toString());
}
if (infoDict.containsKey("Subject"))
{
  doc.addSubject(infoDict.get("Subject").toString());
}
if (infoDict.containsKey("Keywords"))
{
  doc.addKeywords(infoDict.get("Keywords").toString());
}
            
doc.addTitle("Testing Title");
doc.open();

int pageCount = reader.getNumberOfPages();
for (int pageIdx = 1; pageIdx <= pageCount; pageIdx++)
{
  PdfImportedPage page = copy.getImportedPage(reader, pageIdx);
  copy.addPage(page);
}
doc.close();
---snip---

-Sean


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to