Complete Examples
Creating an Invoice Document
java
import CPDFGeneration.layout.*;
public class InvoiceExample {
public static void generateInvoice() {
// Create a document
CDocument document = new CDocument();
// Create an A4 page
CPage page = document.createPage(595, 842);
// Create a page container
CDiv container = page.createDivElement();
container.setPadding(40);
// Create a title style
CStyle titleStyle = document.createStyle();
titleStyle
.setFontSize(24)
.setFontWeight(CFontWeight.BOLD);
titleStyle.setMarginBottom(20)
.setTextAlignment(CTextAlignment.CENTER);
// Add the title
CText title = container.createTextElement("Sales Invoice");
title.addStyle(titleStyle);
// Add invoice information
CDiv infoSection = container.createDivElement();
infoSection.setMarginBottom(20);
CParagraph invoiceNo = infoSection.createParagraphElement("Invoice No.: INV-2024-001");
invoiceNo.setFontSize(12);
CParagraph date = infoSection.createParagraphElement("Date: 2024-01-15");
date.setFontSize(12);
// Create the product table
CTable table = container.createTableElement(4);
table.useAllAvailableWidth();
table.setMarginBottom(20);
// Table header
table.startNewHeaderRow();
table.createHeaderCell().createTextElement("Product Name");
table.createHeaderCell().createTextElement("Quantity");
table.createHeaderCell().createTextElement("Unit Price");
table.createHeaderCell().createTextElement("Amount");
// Table data
String[] products = { "Product A", "Product B", "Product C" };
int[] quantities = { 2, 5, 3 };
double[] prices = { 100.0, 50.0, 75.0 };
for (int i = 0; i < products.length; i++) {
table.startNewRow();
table.createCell().createTextElement(products[i]);
table.createCell().createTextElement(String.valueOf(quantities[i]));
table.createCell().createTextElement("¥" + prices[i]);
table.createCell().createTextElement("¥" + (quantities[i] * prices[i]));
}
// Calculate the total
double total = 0;
for (int i = 0; i < products.length; i++) {
total += quantities[i] * prices[i];
}
// Table footer - Total
table.startNewFooterRow();
CCell totalLabelCell = table.createFooterCell(1, 3); // rowspan, colspan
totalLabelCell.createTextElement("Total");
totalLabelCell.setTextAlignment(CTextAlignment.RIGHT);
totalLabelCell.setFontWeight(CFontWeight.BOLD);
CCell totalAmountCell = table.createFooterCell();
totalAmountCell.createTextElement("¥" + total);
totalAmountCell.setFontWeight(CFontWeight.BOLD);
// Add notes
CParagraph notes = container.createParagraphElement("Notes: Please make payment within 30 days.");
notes.setFontSize(10);
notes.setFontColor(new CColor(100, 100, 100, 255));
// Save the document
document.saveToFile("invoice.pdf");
}
}Creating a Report Document
java
public class ReportExample {
public void generateReport() {
CDocument document = new CDocument();
CPage page = document.createPage(595, 842);
CDiv container = page.createDivElement();
container.setPadding(50);
// Title
CText title = container.createTextElement("Annual Report");
title.setFontSize(28);
title.setFontWeight(CFontWeight.BOLD);
title.setTextAlignment(CTextAlignment.CENTER);
// Add sections
addSection(
container,
"1. Overview",
"This report summarizes the company's major achievements and development direction over the past year..."
);
// Add a chart
CDiv chartSection = container.createDivElement();
chartSection.setMarginTop(20);
chartSection.setMarginBottom(20);
CText chartTitle = chartSection.createTextElement("2. Sales Data");
chartTitle.setFontSize(18);
chartTitle.setFontWeight(CFontWeight.BOLD);
CImage chart = chartSection.createImageElement("path/to/chart.png");
chart.setWidth(400);
chart.setHeight(300);
chart.setHorizontalAlignment(CHorizontalAlignment.CENTER);
// Add a key metrics list
CText metricsTitle = container.createTextElement("3. Key Metrics");
metricsTitle.setFontSize(18);
metricsTitle.setFontWeight(CFontWeight.BOLD);
CList metricsList = container.createListElement();
metricsList.setListSymbol(CListSymbolType.DISC);
metricsList.createListItemElement("Annual revenue growth: 25%");
metricsList.createListItemElement("Customer satisfaction: 92%");
metricsList.createListItemElement("Market share increase: 3%");
document.saveToFile("report.pdf");
}
private void addSection(CDiv container, String heading, String content) {
CParagraph headingPara = container.createParagraphElement(heading);
headingPara.setFontSize(18);
headingPara.setFontWeight(CFontWeight.BOLD);
headingPara.setMarginTop(20);
headingPara.setMarginBottom(10);
CParagraph contentPara = container.createParagraphElement(content);
contentPara.setFontSize(12);
contentPara.setTextAlignment(CTextAlignment.JUSTIFIED);
contentPara.setMultipliedLeading(1.5f);
}
}