Solved

Improve performance Itext pdf code

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

685 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