private void AddCell(PdfPTable table, string text, bool isHeader) { PdfPCell cell = new PdfPCell(new Phrase(text)); if (isHeader) { cell.BackgroundColor = BaseColor.LIGHT_GRAY; cell.HorizontalAlignment = Element.ALIGN_CENTER; Font boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD); cell.Phrase = new Phrase(text, boldFont); } table.AddCell(cell); } } public class AdvancedReport { public void GenerateInvoice(string outputPath) { Document document = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputPath, FileMode.Create)); document.Open(); // Add company logo // Image logo = Image.GetInstance("logo.png"); // logo.ScaleToFit(100, 100); // document.Add(logo); // Add border and background PdfContentByte cb = writer.DirectContent; cb.SetColorFill(BaseColor.WHITE); cb.Rectangle(document.Left, document.Bottom, document.Right - document.Left, document.Top - document.Bottom); cb.Fill(); // Add header with custom styling Font headerFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 24, BaseColor.BLUE); Paragraph header = new Paragraph("INVOICE", headerFont); header.Alignment = Element.ALIGN_CENTER; document.Add(header); document.Close(); } } Usage Example: class Program { static void Main() { ReportGenerator report = new ReportGenerator(); report.CreateReport("SalesReport.pdf"); Console.WriteLine("Report generated successfully!"); } } Important Notes: ⚠️ Licensing : iTextSharp 5.5.13 uses AGPL license - free for open source, paid for commercial use.
document.Open(); // Add title Font titleFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 18); Paragraph title = new Paragraph("Sales Report", titleFont); title.Alignment = Element.ALIGN_CENTER; document.Add(title); document.Add(new Paragraph(" ")); // Empty line document.Add(new Paragraph($"Generated: {DateTime.Now:yyyy-MM-dd HH:mm:ss}")); document.Add(new Paragraph(" ")); // Create table PdfPTable table = new PdfPTable(3); table.WidthPercentage = 100; table.SetWidths(new float[] { 1, 2, 2 }); // Add headers AddCell(table, "ID", true); AddCell(table, "Product", true); AddCell(table, "Amount", true); // Add data AddCell(table, "1", false); AddCell(table, "Laptop", false); AddCell(table, "$999.99", false); AddCell(table, "2", false); AddCell(table, "Mouse", false); AddCell(table, "$29.99", false); document.Add(table); document.Close(); } itextsharp 5.5.13 dll download
Select at least 2 products
to compare