Solved

Improve performance Itext pdf code

Posted on 2011-09-28
3
831 Views
Last Modified: 2012-05-12
Hi

This is my first time working with Itextshap for pdf generation. I will like to know if the following code is good enough or if I can improve performace and get rid of duplicate lines that can be coded in a different way.

The code is working and it generates the pdf file.

Thanks

 static void Main(string[] args)
        {
            Console.WriteLine("Chapter 1 example 1: Hello World");

            // step 1: creation of a document-object
            Document document = new Document(PageSize.LETTER);

            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));

            //Metadata
            document.AddTitle("Notification Example");
            document.AddSubject("Notification Example");
            document.AddKeywords("Notification");
            document.AddCreator("Test");
            document.AddAuthor("Test");
            document.AddHeader("Expires", "0");

            // step 3: we open the document
            document.Open();

            PdfPTable table = new PdfPTable(3);
            table.DefaultCell.Border = PdfPCell.NO_BORDER;

            Font font = new Font(Font.FontFamily.COURIER, 10, Font.BOLD);
            
            Chunk Assurant = new Chunk("Assurant",font);
            Chunk ATTN = new Chunk("Danielle McGrath",font);
            Chunk AddressLn1 = new Chunk("413 Marlon Pike East",font);
            Chunk AddressLn2 = new Chunk("Suite 100",font);
            Chunk AddressLn3 = new Chunk("Cherry Hill, NJ 08034",font);
            Chunk Fax = new Chunk("(888) 225-7060",font);
            Chunk Phone = new Chunk("(856) 761-1833",font);
            Chunk Email = new Chunk("test@test.com",font);
            Chunk Date = new Chunk(DateTime.Today.ToShortDateString(), font);
            Chunk Client = new Chunk("Dianas Test Group", font);
            Chunk ClientDivision = new Chunk("Dianas Test Group", font);
            Chunk EIN = new Chunk("99-99999999", font);
            Chunk PlanName = new Chunk("dental", font);
            Chunk NotificationType = new Chunk(
                "Termination \n The participats below should be terminated on their continuation" + 
                " coverage as of the effective date shown", font); 
            
            
            Paragraph headerLeft = new Paragraph();
            headerLeft.Add(Assurant);
            headerLeft.Add(Chunk.NEWLINE);
            headerLeft.Add(ATTN);
            headerLeft.Add(Chunk.NEWLINE);
            headerLeft.Add(AddressLn1);
            headerLeft.Add(Chunk.NEWLINE);
            headerLeft.Add(AddressLn2);
            headerLeft.Add(Chunk.NEWLINE);
            headerLeft.Add(AddressLn3);
            headerLeft.Add(Chunk.NEWLINE);
            
            Paragraph headerCenter = new Paragraph();
            headerCenter.Add(Fax);
            headerCenter.Add(Chunk.NEWLINE);
            headerCenter.Add(Phone);
            headerCenter.Add(Chunk.NEWLINE);
            headerCenter.Add(Email);
            headerCenter.Add(Chunk.NEWLINE);

            Paragraph headerRight = new Paragraph();            
            headerRight.Add(Date);
            headerRight.Add(Chunk.NEWLINE);
            
            Paragraph clientInfo = new Paragraph();
            clientInfo.Add(Client);
            clientInfo.Add(Chunk.NEWLINE);
            clientInfo.Add(ClientDivision);
            clientInfo.Add(Chunk.NEWLINE);
            clientInfo.Add(EIN);
            clientInfo.Add(Chunk.NEWLINE);

            Paragraph pPlanName = new Paragraph();
            pPlanName.Add(PlanName);

            Paragraph pNotificationType = new Paragraph();
            pNotificationType.Add(NotificationType);
                        
            PdfPCell dateCell = new PdfPCell(headerRight);
            dateCell.HorizontalAlignment = Element.ALIGN_RIGHT;
            dateCell.Border = PdfPCell.NO_BORDER;
            
            PdfPCell rowcell2 = new PdfPCell(
                                new Paragraph(
                                    "\n\nDear Danielle McGrath: \n\nBelow are the coverage continuation notifications" + 
                                    "for participants for the Clients and Plans shown. Please adjust your records" +
                                    "to reflect these coverage continuation notifications."));
            rowcell2.Border = PdfPCell.NO_BORDER;
            rowcell2.Colspan = 3;
            rowcell2.PaddingBottom = 3;

            PdfPCell clientInfoCell = new PdfPCell(clientInfo);            
            clientInfoCell.HorizontalAlignment = Element.ALIGN_LEFT;
            clientInfoCell.Colspan = 3;
            clientInfoCell.PaddingBottom = 3;
            clientInfoCell.BorderWidthTop = 2f;
            clientInfoCell.BorderWidthLeft = 0;
            clientInfoCell.BorderWidthBottom = 2f;
            clientInfoCell.BorderWidthRight = 0;

            PdfPCell planNameCell = new PdfPCell(pPlanName);
            planNameCell.HorizontalAlignment = Element.ALIGN_LEFT;
            planNameCell.Colspan = 3;
            planNameCell.PaddingBottom = 3;
            planNameCell.BorderWidthTop = 0;
            planNameCell.BorderWidthLeft = 0;
            planNameCell.BorderWidthBottom = 0;
            planNameCell.BorderWidthRight = 0;

            PdfPCell notificationTypeCell = new PdfPCell(pNotificationType);
            notificationTypeCell.HorizontalAlignment = Element.ALIGN_LEFT;
            notificationTypeCell.Colspan = 3;
            notificationTypeCell.PaddingBottom = 3;
            notificationTypeCell.BorderWidthTop = 2f;
            notificationTypeCell.BorderWidthLeft = 0;
            notificationTypeCell.BorderWidthBottom = 2f;
            notificationTypeCell.BorderWidthRight = 0;            

            table.AddCell(headerLeft);
            table.AddCell(headerCenter);
            table.AddCell(dateCell);
            table.AddCell(rowcell2);
            table.AddCell(clientInfoCell);
            table.AddCell(planNameCell);
            table.AddCell(notificationTypeCell);
            
            document.Add(table);            

            // step 5: we close the document
            document.Close();
        }

Open in new window

0
Comment
Question by:JoseHidalgo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36797139
I think, there is no any other way to code it. This is the way you can code for it.

Thanks
0
 
LVL 13

Accepted Solution

by:
jonnidip earned 500 total points
ID: 36812853
I would strongly suggest you to take a look to PdfSharp instead of iText.
This library is simpler to use if compared to iText (its project started from a branch code of iText), but is much more optimized, and the objects implement the IDisposable interface.
0
 

Author Closing Comment

by:JoseHidalgo
ID: 36901794
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question