Solved

Improve performance Itext pdf code

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

In a previous article published here at Experts Exchange, Signature Image with Transparent Background (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_12380-Signature-Image-with-Transparent-Background.html), I explained how to cre…
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…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
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…

623 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