Complete Examples
Creating an Invoice Document
csharp
public void GenerateInvoice()
{
// Create document
using (CDocument document = new CDocument())
{
// Create A4 page
CPage page = document.CreatePage(595, 842);
// Create page container
CDiv container = page.CreateDiv();
container.SetPadding(40);
// Create title style
CStyle titleStyle = document.CreateStyle();
titleStyle.SetFontSize(24);
titleStyle.SetFontWeight(CFontWeight.Bold);
titleStyle.SetMarginBottom(20);
titleStyle.SetTextAlignment(CTextAlignment.Center);
// Add title
CText title = container.CreateText("Sales Invoice");
title.AddStyle(titleStyle);
// Add invoice information
CDiv infoSection = container.CreateDiv();
infoSection.SetMarginBottom(20);
CParagraph invoiceNo = infoSection.CreateParagraph("Invoice No: INV-2024-001");
invoiceNo.SetFontSize(12);
CParagraph date = infoSection.CreateParagraph("Date: 2024-01-15");
date.SetFontSize(12);
// Create product table
CTable table = container.CreateTable(4);
table.UseAllAvailableWidth();
table.SetMarginBottom(20);
// Table header
table.StartNewHeaderRow();
table.CreateHeaderCell().CreateText("Product Name");
table.CreateHeaderCell().CreateText("Quantity");
table.CreateHeaderCell().CreateText("Unit Price");
table.CreateHeaderCell().CreateText("Amount");
// Table data
string[] products = { "Product A", "Product B", "Product C" };
int[] quantities = { 2, 5, 3 };
decimal[] prices = { 100m, 50m, 75m };
for (int i = 0; i < products.Length; i++)
{
table.StartNewRow();
table.CreateCell().CreateText(products[i]);
table.CreateCell().CreateText(quantities[i].ToString());
table.CreateCell().CreateText($"${prices[i]}");
table.CreateCell().CreateText($"${quantities[i] * prices[i]}");
}
// Calculate total
decimal total = 0;
for (int i = 0; i < products.Length; i++)
{
total += quantities[i] * prices[i];
}
// Table footer - Total
table.StartNewFooterRow();
CCell totalLabelCell = table.CreateFooterCell(colspan: 3);
totalLabelCell.CreateText("Total");
totalLabelCell.SetTextAlignment(CTextAlignment.Right);
totalLabelCell.SetFontWeight(CFontWeight.Bold);
CCell totalAmountCell = table.CreateFooterCell();
totalAmountCell.CreateText($"${total}");
totalAmountCell.SetFontWeight(CFontWeight.Bold);
// Add notes
CParagraph notes = container.CreateParagraph("Note: Payment due within 30 days.");
notes.SetFontSize(10);
notes.SetFontColor(new CColor(100, 100, 100, 255));
// Save document
document.SaveToFile("invoice.pdf");
}
}Creating a Report Document
csharp
public void GenerateReport()
{
using (CDocument document = new CDocument())
{
CPage page = document.CreatePage(595, 842);
CDiv container = page.CreateDiv();
container.SetPadding(50);
// Title
CText title = container.CreateText("Annual Report");
title.SetFontSize(28);
title.SetFontWeight(CFontWeight.Bold);
title.SetTextAlignment(CTextAlignment.Center);
// Add section
AddSection(container, "1. Overview",
"This report outlines the company's major achievements and development direction over the past year...");
// Add chart
CDiv chartSection = container.CreateDiv();
chartSection.SetMarginTop(20);
chartSection.SetMarginBottom(20);
CText chartTitle = chartSection.CreateText("2. Sales Data");
chartTitle.SetFontSize(18);
chartTitle.SetFontWeight(CFontWeight.Bold);
CImage chart = chartSection.CreateImage("path/to/chart.png");
chart.SetWidth(400);
chart.SetHeight(300);
chart.SetHorizontalAlignment(CHorizontalAlignment.Center);
// Add key metrics list
CText metricsTitle = container.CreateText("3. Key Metrics");
metricsTitle.SetFontSize(18);
metricsTitle.SetFontWeight(CFontWeight.Bold);
CList metricsList = container.CreateList();
metricsList.SetListSymbol(CListSymbolType.Disc);
metricsList.CreateListItem("Annual revenue growth: 25%");
metricsList.CreateListItem("Customer satisfaction: 92%");
metricsList.CreateListItem("Market share increase: 3%");
document.SaveToFile("report.pdf");
}
}
private void AddSection(CDiv container, string heading, string content)
{
CParagraph headingPara = container.CreateParagraph(heading);
headingPara.SetFontSize(18);
headingPara.SetFontWeight(CFontWeight.Bold);
headingPara.SetMarginTop(20);
headingPara.SetMarginBottom(10);
CParagraph contentPara = container.CreateParagraph(content);
contentPara.SetFontSize(12);
contentPara.SetTextAlignment(CTextAlignment.Justify);
contentPara.SetMultipliedLeading(1.5f);
}