Solved

Improve performance Itext pdf code

Posted on 2011-09-28
3
753 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Power PDF (http://www.nuance.com/for-business/document-imaging-and-scanning/power-pdf-converter/index.htm) is the newest product from the Document Imaging division of Nuance Communications (http://www.nuance.com/). It is available in two editions — …
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
We often encounter PDF files that are pure images, that is, they do not have text characters, but instead contain only raster graphics. The most common causes of this are document scanning software and faxing software/services that create image-only…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now