Tech Off Post

Single Post Permalink

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

    Once you created the PDF file using the PdfStamper object, you can set the document properties by importing the file into a new document - where you can set properties such as Author and Title.

    Note, the following code is just a workaround (not a solution):

    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    public static void CopyDocumentTest()
    {
       int pageNumber = 1;
       PdfReader reader = new PdfReader("New Document.pdf");
       Rectangle size = reader.GetPageSizeWithRotation(pageNumber);
       Document document = new Document(size);
       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Copy of New Document.pdf", FileMode.Create, FileAccess.Write));

       //set document info
       document.AddTitle("Document copied using iTextSharp");
       document.AddAuthor("Joachim Tesznar");
       document.AddSubject("Dynamic Content");
       document.AddCreator("PDF Form Tool by Joachim Tesznar");

       document.Open();

       PdfContentByte cb = writer.DirectContent;
       document.NewPage();
       PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
       cb.AddTemplate(page, 0, 0);
       document.Close();
    }