完整示例
创建发票文档
java
import CPDFGeneration.layout.*;
public class InvoiceExample {
public static void generateInvoice() {
// 创建文档
CDocument document = new CDocument();
// 创建 A4 页面
CPage page = document.createPage(595, 842);
// 创建页面容器
CDiv container = page.createDivElement();
container.setPadding(40);
// 创建标题样式
CStyle titleStyle = document.createStyle();
titleStyle
.setFontSize(24)
.setFontWeight(CFontWeight.BOLD);
titleStyle.setMarginBottom(20)
.setTextAlignment(CTextAlignment.CENTER);
// 添加标题
CText title = container.createTextElement("销售发票");
title.addStyle(titleStyle);
// 添加发票信息
CDiv infoSection = container.createDivElement();
infoSection.setMarginBottom(20);
CParagraph invoiceNo = infoSection.createParagraphElement("发票编号: INV-2024-001");
invoiceNo.setFontSize(12);
CParagraph date = infoSection.createParagraphElement("日期: 2024-01-15");
date.setFontSize(12);
// 创建产品表格
CTable table = container.createTableElement(4);
table.useAllAvailableWidth();
table.setMarginBottom(20);
// 表头
table.startNewHeaderRow();
table.createHeaderCell().createTextElement("产品名称");
table.createHeaderCell().createTextElement("数量");
table.createHeaderCell().createTextElement("单价");
table.createHeaderCell().createTextElement("金额");
// 表格数据
String[] products = { "产品 A", "产品 B", "产品 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]));
}
// 计算总计
double total = 0;
for (int i = 0; i < products.length; i++) {
total += quantities[i] * prices[i];
}
// 表尾 - 总计
table.startNewFooterRow();
CCell totalLabelCell = table.createFooterCell(1, 3); // rowspan, colspan
totalLabelCell.createTextElement("总计");
totalLabelCell.setTextAlignment(CTextAlignment.RIGHT);
totalLabelCell.setFontWeight(CFontWeight.BOLD);
CCell totalAmountCell = table.createFooterCell();
totalAmountCell.createTextElement("¥" + total);
totalAmountCell.setFontWeight(CFontWeight.BOLD);
// 添加备注
CParagraph notes = container.createParagraphElement("备注:请在 30 天内付款。");
notes.setFontSize(10);
notes.setFontColor(new CColor(100, 100, 100, 255));
// 保存文档
document.saveToFile("invoice.pdf");
}
}创建报告文档
java
public class ReportExample {
public void generateReport() {
CDocument document = new CDocument();
CPage page = document.createPage(595, 842);
CDiv container = page.createDivElement();
container.setPadding(50);
// 标题
CText title = container.createTextElement("年度报告");
title.setFontSize(28);
title.setFontWeight(CFontWeight.BOLD);
title.setTextAlignment(CTextAlignment.CENTER);
// 添加章节
addSection(container, "1. 概述",
"本报告概述了公司在过去一年的主要成就和发展方向...");
// 添加图表
CDiv chartSection = container.createDivElement();
chartSection.setMarginTop(20);
chartSection.setMarginBottom(20);
CText chartTitle = chartSection.createTextElement("2. 销售数据");
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);
// 添加关键指标列表
CText metricsTitle = container.createTextElement("3. 关键指标");
metricsTitle.setFontSize(18);
metricsTitle.setFontWeight(CFontWeight.BOLD);
CList metricsList = container.createListElement();
metricsList.setListSymbol(CListSymbolType.DISC);
metricsList.createListItemElement("年度收入增长: 25%");
metricsList.createListItemElement("客户满意度: 92%");
metricsList.createListItemElement("市场份额提升: 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);
}
}