Apache PDFBox 表/行对齐与 Boxable
Posted
技术标签:
【中文标题】Apache PDFBox 表/行对齐与 Boxable【英文标题】:Apache PDFBox Table/Row Alignment with Boxable 【发布时间】:2021-12-16 10:54:59 【问题描述】:我在我的程序中导出一个 PDF,我想用 ApachePDF Box 创建一个表格,它应该是页面宽度的 50-60%。
但是,我没有找到任何关于居中行/表格本身的信息。
我找到了如何对齐行/单元格本身中的文本,但如果我创建一个不使用页面全宽的行,它总是左对齐,我不知道如何居中对齐行,因为该行或表没有setAlign
方法。
我在上面使用 Boxable (https://github.com/dhorions/boxable)
【问题讨论】:
Pdfbox 不包含显式表生成功能。您可能是指您在 pdfbox 之上使用的一些库。请给它命名。 啊是的我忘了,我正在使用github.com/dhorions/boxable 【参考方案1】: public void Test() throws IOException
//Set margins
float margin = 10;
//Initialize Document
PDDocument doc = new PDDocument();
PDPage page = addNewPage(doc);
//Initialize table
float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
boolean drawContent = true;
boolean drawLines = true;
float yStart = yStartNewPage;
float bottomMargin = 70;
BaseTable table = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, drawLines,
drawContent);
// set default line spacing for entire table
table.setLineSpacing(1.5f);
Row<PDPage> row = table.createRow(10);
// set single spacing for entire row
row.setLineSpacing(1f);
// my first 3x wider cell
Cell<PDPage> cell = row.createCell((3*100/15f), "1",
HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
cell.setFontSize(6);
// my other 12 equal cells
for(int i=2; i<14; i++)
Cell<PDPage> cell2 = row.createCell((100/15f), String.valueOf(i),
HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
cell2.setFontSize(6);
table.draw();
//Save the document
File file = new File("target/test.pdf");
System.out.println("Sample file saved at : " + file.getAbsolutePath());
Files.createParentDirs(file);
doc.save(file);
doc.close();
调整设置位置和表格宽度......
【讨论】:
【参考方案2】:调整设置位置和表格宽度:
public void Test() throws IOException
//Set margins
float margin = 10;
//Initialize Document
PDDocument doc = new PDDocument();
PDPage page = addNewPage(doc);
//Initialize table
float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
boolean drawContent = true;
boolean drawLines = true;
float yStart = yStartNewPage;
float bottomMargin = 70;
BaseTable table = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, drawLines, drawContent);
// set default line spacing for entire table
table.setLineSpacing(1.5f);
Row<PDPage> row = table.createRow(10);
// set single spacing for entire row
row.setLineSpacing(1f);
// my first 3x wider cell
Cell<PDPage> cell = row.createCell((3*100/15f), "1",
HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
cell.setFontSize(6);
// my other 12 equal cells
for(int i=2; i<14; i++)
Cell<PDPage> cell2 = row.createCell((100/15f), String.valueOf(i),
HorizontalAlignment.get("center"), VerticalAlignment.get("top"));
cell2.setFontSize(6);
table.draw();
//Save the document
File file = new File("target/test.pdf");
System.out.println("Sample file saved at : " + file.getAbsolutePath());
Files.createParentDirs(file);
doc.save(file);
doc.close();
【讨论】:
以上是关于Apache PDFBox 表/行对齐与 Boxable的主要内容,如果未能解决你的问题,请参考以下文章
NoSuchMethodError: org.apache.pdfbox.pdmodel.PDDocument.getPage(I)的解决方法
NoSuchMethodError: org.apache.pdfbox.pdmodel.PDDocument.getPage(I)的解决方法