kernel,io,layout,forms
try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()){
PdfWriter writer = new PdfWriter(outputStream, new WriterProperties().setFullCompressionMode(true));
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc, PageSize.LETTER);
document.setMargins(5, 15, 36, 15);
// 添加内容 ...
Table table = .... ; // 创建Table对象
document.add(table);
//
document.close();
return outputStream.toByteArray() ;
}
int numberOfPages = pdfDoc.getNumberOfPages();
LocalDate now = LocalDate.now();
String dateContent = "Date : " + now.getYear()+ "/"+ now.getMonthValue() +"/"+ now.getDayOfMonth() ;
Paragraph dateParagraph = new Paragraph(dateContent)
.setFontSize(7)
.setFontColor(ColorConstants.BLACK);
for (int i = 1; i <= numberOfPages; i++) {
String pageContent = "Page: "+ i +" of " + numberOfPages ;
Paragraph pageParagraph = new Paragraph( pageContent)
.setFontSize(7)
.setFontColor(ColorConstants.BLACK);
Rectangle pageSize = pdfDoc.getPage(i).getPageSize();
float x = pageSize.getWidth() * 0.6f -2;
float y = pageSize.getTop() - 12;
doc.showTextAligned(pageParagraph, x, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
doc.showTextAligned(dateParagraph, x + 70, y, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// part1Bytes、part2Bytes 为两个pdf文件的字节数组
try(ByteArrayInputStream part1InputSteam = new ByteArrayInputStream(part1Bytes);
ByteArrayInputStream part2InputStream = new ByteArrayInputStream(part2Bytes)){
// Create PdfDocument object of each pdf
PdfDocument part1Doc = new PdfDocument(new PdfReader(part1InputSteam));
PdfDocument part2Doc = new PdfDocument(new PdfReader(part2InputStream));
//
merger.merge(part1Doc, 1, part1Doc.getNumberOfPages());
merger.merge(part2Doc, 1, part2Doc.getNumberOfPages());
// Source documents can be closed implicitly after merging,
// by passing true to the setCloseSourceDocuments(boolean) method
part1Doc.close();
part2Doc.close();
}
Cell cell = ... ;
// columnWidth 会使得表格的列宽度按照比例分配
float[] columnWidth = {1, 2, 1};
Table table = new Table(UnitValue.createPercentArray(columnWidth))
table.addCell(cell);
table.useAllAvailableWidth();
// table.setWidth(UnitValue.createPercentValue(100)); // 也可以设置宽度
Cell cell = new Cell() ;
// 填充单元格内容
Paragraph paragraph = new Paragraph(content).setFontSize(7) ;
cell.add(paragraph);
Cell cell = new Cell() ;
cell.setTextAlignment(TextAlignment.CENTER) ;
cell.setVerticalAlignment(VerticalAlignment.MIDDLE)
cell.setHorizontalAlignment(HorizontalAlignment.CENTER) ;
ClassPathResource resource = new ClassPathResource("banner.jpg");
byte[] bannerData = resource.getContentAsByteArray();
ImageData imageData = ImageDataFactory.create(bannerData);
Image banner = new Image(imageData);
// 设置图片宽度
banner.setWidth(80) ;
Cell cell = new Cell().add(banner);
table.addCell(new Cell(1, 3).add(new Paragraph("Cell with colspan 3")));
table.addCell(new Cell(2, 1).add(new Paragraph("Cell with rowspan 2")));
// 这个设置不生效 table.setBorder(Border.NO_BORDER);
Cell cell = new Cell() ;
// ...
cell.setBorder(Border.NO_BORDER) ;
Paragraph divHeader = new Paragraph("Hello : 1" )
.setFontSize(7).setMargins(0,0,0,0).setPaddings(0,0,0,0);
Paragraph divText = new Paragraph("World : 2")
.setFontSize(7).setMargins(0,0,0,0).setPaddings(0,0,0,0);
Div div = new Div().add(divHeader).add(divText)
.setWidth(160).setPaddings(8, 0, 0, 0)
.setHorizontalAlignment(HorizontalAlignment.LEFT) ;
public abstract class HelloHeaderEventHandler implements IEventHandler {
private final Document doc;
private final Table header;
private final float tableHeight;
protected HelloHeaderEventHandler(Document doc, Table header){
this.doc = doc ;
this.header = header ;
this.tableHeight = this.calTableHeight(header) ;
}
@Override
public void handleEvent(Event currentEvent) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) currentEvent;
PdfDocument pdfDoc = docEvent.getDocument();
PdfPage page = docEvent.getPage();
PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
PageSize pageSize = pdfDoc.getDefaultPageSize();
float coordX = pageSize.getX() + doc.getLeftMargin();
float coordY = pageSize.getTop() - doc.getTopMargin();
float width = pageSize.getWidth() - doc.getRightMargin() - doc.getLeftMargin();
float height = getHeaderHeight();
Rectangle rect = new Rectangle(coordX, coordY, width, height);
this.doHandleEvent(canvas, rect);
}
private void doHandleEvent(PdfCanvas canvas, Rectangle rect) {
new Canvas(canvas, rect)
.add(header)
.close();
}
private float getHeaderHeight() {
return tableHeight;
}
private float calTableHeight(Table table){
TableRenderer renderer = (TableRenderer) table.createRendererSubTree();
renderer.setParent(new DocumentRenderer(doc));
// Simulate the positioning of the renderer to find out how much space the header table will occupy.
LayoutResult result = renderer.layout(new LayoutContext(new LayoutArea(0, PageSize.A4)));
return result.getOccupiedArea().getBBox().getHeight();
}
}
PdfWriter writer = new PdfWriter(outputStream, new WriterProperties().setFullCompressionMode(true));
PdfDocument pdfDoc = new PdfDocument(writer);
Document document = new Document(pdfDoc, PageSize.LETTER);
//
Table header = ...; // 创建Table对象
IEventHandler eventHandler = new HelloHeaderEventHandler(document, header)
pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, eventHandler);
// 设置页面边距
float topMargin = 3 + eventHandler.getHeaderHeight();
document.setMargins(topMargin, 15, 36, 15);