【发布时间】:2017-09-11 18:06:45
【问题描述】:
这就是我得到的。我如何也可以旋转表格 - 而不仅仅是文档。如果表格旋转,列可能会更宽。
下面的代码仅用 1 列在较小的范围内重现了该问题。
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();
}
【问题讨论】:
-
请向我们展示一个重现此问题的最小代码示例。
-
@JorisSchellekens 我已根据您的要求添加了代码。
-
所以你本质上想要一个横向填充的页面?
-
@mkl 这听起来是对的,是的。表格应顺时针旋转 90°,以便用户可以在保持横向页面方向的同时从左到右阅读表格。
-
我刚试了一下,拿了你的代码,用
new Document(PageSize.A4.rotate())替换了new Document(),并删除了页面事件监听器,表格确实利用了额外的空间.你可能用你的多列表测试过吗?您是否可能在该代码中设置了绝对列宽?
标签: itext