更改iText PdfPTable的方向
这就是我得到的。我怎样才能旋转桌子——不仅仅是文件。如果表格旋转,列可能会更宽 下面的代码以较小的规模重现了这个问题,只有一列更改iText PdfPTable的方向,itext,Itext,这就是我得到的。我怎样才能旋转桌子——不仅仅是文件。如果表格旋转,列可能会更宽 下面的代码以较小的规模重现了这个问题,只有一列 private void exportTableAsPDF(File outputFile) { // PDF document Document pdfDocument = new Document(); try { PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument
private void exportTableAsPDF(File outputFile) {
// PDF document
Document pdfDocument = new Document();
try {
PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(outputFile));
// Used to rotate the page - iText recommended this approach in an answer to a question referenced below
// https://developers.itextpdf.com/question/how-rotate-page-while-creating-pdf-document
class RotateEvent extends PdfPageEventHelper {
public void onStartPage(PdfWriter writer, Document document) {
writer.addPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
}
}
// Rotates each page to landscape
pdfWriter.setPageEvent(new RotateEvent());
} catch (Exception e) {
e.printStackTrace();
}
pdfDocument.open();
// PDF table
PdfPTable pdfPTable = new PdfPTable(1);
// Add column header cell
PdfPCell dateCell = new PdfPCell(new Phrase("Date"));
pdfPTable.addCell(dateCell);
// Gets cell data
LogEntryMapper logEntryMapper = new LogEntryMapper();
List<LogEntry> logEntries = logEntryMapper.readAll();
// Adds a cell to the table with "date" data
for (LogEntry logEntry : logEntries) {
dateCell = new PdfPCell(new Phrase(logEntry.getLogEntryDate()));
pdfPTable.addCell(dateCell);
}
// Adds the table to the pdf document
try {
pdfDocument.add(pdfPTable);
} catch (DocumentException e) {
e.printStackTrace();
}
pdfDocument.close();
}
private void exportTableAsPDF(文件输出文件){
//PDF文件
文档pdfDocument=新文档();
试一试{
PdfWriter PdfWriter=PdfWriter.getInstance(pdfDocument,新文件outputstream(outputFile));
//用于旋转页面-iText在回答下面提到的问题时推荐了这种方法
// https://developers.itextpdf.com/question/how-rotate-page-while-creating-pdf-document
类RotateEvent扩展了PdfPageEventHelper{
开始页上的公共无效(PDF编写器、文档){
writer.addPageDictEntry(PdfName.ROTATE、PdfPage.SEASCAPE);
}
}
//将每个页面旋转到横向
setPageEvent(新的rotateeEvent());
}捕获(例外e){
e、 printStackTrace();
}
pdfDocument.open();
//PDF表格
PdfPTable PdfPTable=新的PdfPTable(1);
//添加列标题单元格
PdfPCell dateCell=新PdfPCell(新短语(“日期”));
pdfPTable.addCell(dateCell);
//获取单元格数据
LogEntryMapper LogEntryMapper=新LogEntryMapper();
List logEntries=logEntryMapper.readAll();
//将包含“日期”数据的单元格添加到表中
用于(日志条目:日志条目){
dateCell=newpdfpcell(新短语(logEntry.getLogEntryDate());
pdfPTable.addCell(dateCell);
}
//将表添加到pdf文档中
试一试{
pdfDocument.add(pdfPTable);
}捕获(文档异常){
e、 printStackTrace();
}
pdfDocument.close();
}
此代码块产生以下结果。您找到的解决方案(使用页面事件侦听器)针对的是另一个问题:它用于垂直打印文档纸张大小,然后旋转包含内容的页面。对于您的问题(在旋转的纸张上垂直打印),您只需使用旋转的纸张大小初始化文档:
Document pdfDocument = new Document(PageSize.A4.rotate());
这样,表就利用了额外的页面大小
不过,您会注意到,左侧和右侧仍然有一些可用空间。原因有两个:
- 表格考虑为文档配置的页边距李>
- 默认情况下,表格仅使用可用宽度的80%
- 减少页边距,例如通过使用另一个
构造函数文档
或者在创建相关页面之前使用Document pdfDocument = new Document(PageSize.A4.rotate(), marginLeft, marginRight, marginTop, marginBottom);
pdfDocument.setMargins(marginLeft、marginRight、marginTop、marginBottom)
- 增加表使用的可用宽度的百分比
使用pdfPTable.setWidthPercentage(widthPercentage);
值,例如widthPercentage
100
Document pdfDocument = new Document(PageSize.A4.rotate());
这样,表就利用了额外的页面大小
不过,您会注意到,左侧和右侧仍然有一些可用空间。原因有两个:
- 表格考虑为文档配置的页边距李>
- 默认情况下,表格仅使用可用宽度的80%
- 减少页边距,例如通过使用另一个
构造函数文档
或者在创建相关页面之前使用Document pdfDocument = new Document(PageSize.A4.rotate(), marginLeft, marginRight, marginTop, marginBottom);
pdfDocument.setMargins(marginLeft、marginRight、marginTop、marginBottom)
- 增加表使用的可用宽度的百分比
使用pdfPTable.setWidthPercentage(widthPercentage);
值,例如widthPercentage
100
新文档()
替换为新文档(PageSize.A4.rotate())
并删除页面事件侦听器,桌子确实利用了额外的空间。您是否可能使用多列表进行了测试?你可能会在代码中设置绝对列宽吗?请给我们一个最小的代码示例来重现这个问题。@JorisSchellekens我已经根据你的请求添加了我的代码。所以你基本上想要一个垂直填充的横向页面?@mkl听起来不错,是的。表格应顺时针旋转90°,以便用户可以在保持横向页面方向的同时从左到右读取表格。我刚刚尝试过,使用您的代码,将新文档()
替换为新文档(PageSize.A4.rotate())
并删除页面事件侦听器,桌子确实利用了额外的空间。您是否可能使用多列表进行了测试?您可能会在代码中设置绝对列宽吗?