Generating PDF Files With iTextSharp
- by Ricardo Peres
I recently had the need to generate a PDF file containing a table where some of the cells included images. Of course, I used iTextSharp to do it. Because it has some obscure parts, I decided to publish a simplified version of the code I used.
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
//...
protected void OnGeneratePdfClick()
{
String text = "Multi\nline\ntext";
String name = "Some Name";
String number = "12345";
Int32 rows = 7;
Int32 cols = 3;
Single headerHeight = 47f;
Single footerHeight = 45f;
Single rowHeight = 107.4f;
String pdfName = String.Format("Labels - {0}", name);
PdfPTable table = new PdfPTable(3) { WidthPercentage = 100, HeaderRows = 1 };
PdfPCell headerCell = new PdfPCell(new Phrase("Header")) { Colspan = cols, FixedHeight = headerHeight, HorizontalAlignment = Element.ALIGN_CENTER, BorderWidth = 0f };
table.AddCell(headerCell);
FontFactory.RegisterDirectory(@"C:\WINDOWS\Fonts"); //required for the Verdana font
Font cellFont = FontFactory.GetFont("Verdana", 6f, Font.NORMAL);
for (Int32 r = 0; r
SyntaxHighlighter.config.clipboardSwf = 'http://alexgorbatchev.com/pub/sh/2.0.320/scripts/clipboard.swf';
SyntaxHighlighter.brushes.CSharp.aliases = ['c#', 'c-sharp', 'csharp'];
SyntaxHighlighter.all();