Tech Off Post

Single Post Permalink

View Thread: ASP.NET and iTextSharp (free PDF Generator for .NET)
  • User profile image

    sbc wrote:
    Thanks, that worked out fine. Pity the documentation is not that good, but as a side effect I found out about generating a document from Xml or via document.Add.

    Only difference is I used Server.MapPath to get the file, and saved the 'stamped' document to a MemoryStream that is then streamed to the user. Didn't use FormFlattening either - what is the advantage of using it?

    Also, how do you change author, subject, keywords etc using PdfStamper/Reader?

    Form Flattening flettens the actual form fields so that they can no longer be changed in the new document created by the stamper. If you have not set that, anyone can change the contents of the form fields... If you do specify Form Flattening, then the form fields will be converted into text and cannot be further modified.

    To add the document meta-data, you would normally use the following methods of the Document object (before calling document.Open()):

    private void createPDF()
       string newFile = @"N:\.NET\New Document.pdf";
       Document doc = new Document();
       PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(newFile, FileMode.Create));
       // Metadata
       doc.AddCreator("PDF Printer by Joachim Tesznar");
       doc.AddTitle("New PDF Document");
       // Add content
       doc.Add(new Paragraph("Hello World"));

    I don't know how you can do this using the PdfStamper object; there must be a way of accessing the document object in-directly. If you find out how, let me know... Wink If you can't, I know a way around this but it requires copying the ContentByte of that document and importing a page into a new document where you can set the meta-data.

    Another option is to create your document in XML (see the tutorial), then use the XmlParser object to convert it to PDF. You can set your meta-data in the XML document and the parser will set these in the PDF for you.

    Have fun