Solved

Improve performance Itext pdf code

Posted on 2011-09-28
3
803 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Inserting page numbers in Portable Document Files not only enhances manageability but also makes them look professional. With numbered pages, the file appears more organized and it becomes easier to search for a particular page. The size and the vol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this third video of the Xpdf series, we discuss and demonstrate the PDFtoText utility, which converts PDF files into plain text files. Download and install the software.: You may have already downloaded and installed the Xpdf tools while watching…
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 …

860 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