![]() Here, the total number of cells should be 2 or in multiples of 2 (that is - 2,4,6 and so on.) The reason is - I have declared PdfPTable() for 2 columns and am inserting only 1 cell into it. Var pdfWriter = PdfWriter.GetInstance(doc, stream) Ĭell.HorizontalAlignment = PdfPCell.ALIGN_LEFT Ĭell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE The following program will throw an exception -ĭocument doc = new Document(PageSize.A4) Where N = Number of columns declared at the time of creating instance of PdfPTable() (This is the most frequent error that I have experienced in my coding!)įor PdfPTable(int numColumns) overload of PdfPTable() constructor, the effective total number of cells in the PdfPTable() must be N or in multiples of N,.Similarly, don't forget to add doc.Add(tbl) statement which places an instance of PdfPTable() in the document. Do not forget to create an instance of PdfWriter() because otherwise we can't write anything in the PDF.You can download the sample PDF file by clicking on the link "Download Sample PDF"! And that's it! Now run the application. ![]() PCell.UseAscender = true //Setting property to "true" along with setting to "PdfPCell.ALIGN_MIDDLE" helps to align the text in the cell at vertically middle position. PCell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE PCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT Private void ApplyPrintSettingsToCell(PdfPCell pCell) Return File(byteArray, "application/pdf", "Simple Tense.pdf") PdfPCell cell = new PdfPCell(new Phrase("Simple Present Tense")) Ĭell = new PdfPCell(new Phrase("I write a letter.")) Ĭell = new PdfPCell(new Phrase("Simple Past Tense")) Ĭell = new PdfPCell(new Phrase("I wrote a letter.")) Ĭell = new PdfPCell(new Phrase("Simple Future Tense")) Ĭell = new PdfPCell(new Phrase("I will write a letter.")) Tbl.WidthPercentage = 100.0f //Allow the table to take up the full width available (after applying default margins for A4 sized paper - Because I haven't set any margins explicitly!) Here, Width of 2nd column = 3 * (Width of 1st column) Execute this command : Install-Package iTextSharp -Version ) //Set relative widths of the columns.Tools → NuGet Package Manager → Package Manager Console.Install iTextSharp using package manager console.→ Select “Web” as a type of project under “C#” → Enter name for your web application (PDFDemo) → Click “OK” to navigate to next window → Select “Empty” as a template type with check marked checkbox for “MVC” → Click “OK”) Open Visual Studio and create a new empty MVC application.(I am using VS 2015 Express edition for Web.) Let's create an MVC application for demonstration purpose. Download the complete zip file from web (), extract it to get itextsharp.dll, copy this DLL in your project and add its reference in "References".In this blog, I would like to show some basics of creating tables in PDF using 'iTextSharp' along with the common errors that I have faced. ![]() Generally, we need to export/create the documents such as reports or invoices in PDF format. Here, 'iTextSharp' (a third party DLL) can help us to do the job. Many a times, we need to generate PDF files at runtime in our application.
0 Comments
Leave a Reply. |