【发布时间】:2014-01-28 22:39:26
【问题描述】:
从头开始创建 PDF 时,我正在尝试修改此代码
http://itextpdf.com/examples/iia.php?id=104
特别是那里显示的 onEndPage() 函数,用于使用页面事件设置页脚。问题出在我的应用程序中,有些页面是纵向的,有些是横向的,我不知道如何在该函数中实现查询以确定页面旋转。
首先,我让它在所有页面都是纵向的时候工作。然后我添加了一些横向页面并尝试修改它,如下所示。我最初认为一个快速而肮脏的解决方案是简单地通过添加来使页脚表居中,
table.setHorizontalAlignment(Element.ALIGN_CENTER);
但这似乎没有任何效果(在横向页面上,表格总是在纸张的长边左对齐)。然后我尝试通过查询页面旋转来做一个更好的解决方案,并根据其结果,将表格列设置为正确的宽度,使用,
if (???==90)
table.setTotalWidth(new float[]{2.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,2.25f*K.PPI,}); // add to 6.5"
else
table.setTotalWidth(new float[]{3.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,3.25f*K.PPI,}); // add to 6.5"
但我不确定如何查询页面旋转(如您所知)。任何帮助,将不胜感激。我的代码如下。
class HeaderFooter extends PdfPageEventHelper {
/** The header text. */
String footerLeft, footerRight;
/** The template with the total number of pages. */
PdfTemplate total;
/** Flag indicating true for first page */
Boolean firstPageFlag=true;
...
public void onEndPage(PdfWriter writer, Document document) {
if (firstPageFlag==false) {
/** The footer font */
FontFactory.register("/home/appFonts/Arial_Narrow.ttf", "arial_narrow");
Font styleFooter = FontFactory.getFont("arial_narrow", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, K.TEXT_FOOTER_FONT_SIZE, Font.UNDEFINED, BaseColor.BLACK);
PdfPTable table = new PdfPTable(4);
try {
//table.setHorizontalAlignment(Element.ALIGN_CENTER); // doesn't seem to have an effect for landscape pages
if (how to query page rotation, or other method to evaluate whether page is landscape or portrait?)
table.setTotalWidth(new float[]{2.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,2.25f*K.PPI,}); // add to 6.5"
else
table.setTotalWidth(new float[]{3.25f*K.PPI,1.195f*K.PPI,0.805f*K.PPI,3.25f*K.PPI,}); // add to 8.5"
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(14);
table.getDefaultCell().setBorder(Rectangle.TOP);
// col1, row1
table.addCell(new Phrase(footerLeft, styleFooter));
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
// col2, row1
table.addCell(new Phrase(String.format("page %d of", writer.getPageNumber()),styleFooter));
// col3, row1
PdfPCell cell = new PdfPCell(Image.getInstance(total));
cell.setBorder(Rectangle.TOP);
table.addCell(cell);
// col4, row1
table.addCell(new Phrase(footerRight, styleFooter));
table.writeSelectedRows(0,-1,document.left(),document.bottom()-0.35f*K.PPI,writer.getDirectContent());
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
} else
firstPageFlag=false;
}
...
}
【问题讨论】:
-
您正在混合页面事件(用于从头开始创建 PDF)和
PdfReader(用于处理现有 PDF)。这会让想要回答您问题的人感到困惑(并且可能会让您自己感到困惑)。请说明您是从头开始创建 PDF(在这种情况下不应使用PdfReader)还是基于现有 PDF(在这种情况下不应使用页面事件)。 -
@ggkmath 作为 Bruno 评论的附录:如果您想为 现有 PDF 添加页眉和页脚,您可能需要查看示例的第二遍@ 987654322@。在那里你有一个
PdfReader并且可以使用你的页面旋转测试。 -
感谢 cmets。这是从头开始创建 PDF(而不是修改现有 PDF)。我修改了原始帖子以删除对 PdfReader 的引用。
-
也许您仍然应该查看 TwoPasses.java 示例并分两次创建 PDF,第一次创建文档,第二次添加页眉和页脚。如果这很困难(例如,由于某些上下文信息添加到标题中),您可能希望在
onStartPage期间查看作者或文档,它们可能带有当前正在创建的新页面的大小信息。将这些信息存储在您的HeaderFooter成员中,并在onEndPage期间使用它们。 -
嗯,矩形不仅有宽度(
getWidth)和高度(getHeight),它也有旋转(getRotation)。如果旋转为 0 或 180,则页面为横向且仅当宽度 > 高度;否则,当高度 > 宽度时,页面是横向的。或者我假设。 (我很少从头开始创建文档,通常我只是对它们进行后处理,所以我在这里并没有真正了解这些细节。)
标签: itext