我在javascript环境中工作。我可以导入jar文件并使用itext,但所有内容都必须用js编写。对于使用itext的基本PDF,我可以使用以下代码:
var byteArrayOutputStream = new java.io.ByteArrayOutputStream();
var document = new com.itextpdf.text.Document();
var paragraph;
document.setPageSize(com.itextpdf.text.PageSize.LETTER);
document.setMargins(13, 13, 8, 8);
var writer = com.itextpdf.text.pdf.PdfWriter.getInstance(document, new java.io.FileOutputStream(RESULT));
writer.setPageEvent(event);
var font1 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 10.5);
var font2 = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.TIMES_ROMAN, 10.5, com.itextpdf.text.Font.BOLD);
com.itextpdf.text.pdf.PdfWriter.getInstance(document, byteArrayOutputStream).setInitialLeading(10);
document.open();
paragraph = new com.itextpdf.text.Paragraph("Stuff\n", font1);
paragraph.add(new com.itextpdf.text.Chunk("more stuff\n", font1));
paragraph.add(new com.itextpdf.text.Chunk("another line\n", font2));
paragraph.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
document.add(paragraph);
// lines is a table of data
for (var i = 0; i < lines.length; i++) {
document.add(new com.itextpdf.text.Chunk(lines[i], font1));
document.add(com.itextpdf.text.Chunk.NEWLINE);
}
document.close();
我想要的是“段落”是每个页面的标题。我试图将这个例子从这个页面iText5.1.0 set header and footer in document转换为js,但内部的clas是什么让我搞砸了,因为它扩展了另一个类。我尝试使用JavaAdapter但是没有按预期工作。
任何人都可以帮我在js中使用页眉/页脚吗?
谢谢!
编辑:
以下是我要转换的java代码:
class TableHeader extends PdfPageEventHelper {
String header;
PdfTemplate total;
public void setHeader(String header) {
this.header = header;
}
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(30, 16);
}
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(3);
try {
table.setWidths(new int[]{24, 24, 2});
table.setTotalWidth(527);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.addCell(header);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(String.format("Page %d of", writer.getPageNumber()));
PdfPCell cell = new PdfPCell(Image.getInstance(total));
cell.setBorder(Rectangle.BOTTOM);
table.addCell(cell);
table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
}
catch(DocumentException de) {
throw new ExceptionConverter(de);
}
}
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber() - 1)),
2, 2, 0);
}
}
以下是我认为js 应该的样子:
var PageEvent = java.type(com.itextpdf.text.pdf.PdfPageEventHelper);
var PageEventExtender = java.extend(PageEvent);
var TableHeader = new PageEventExtender({
header: "[Default header]",
total: "",
onOpenDocument: function (writer, document) {
total = writer.getDirectContent().createTemplate(30, 16);
},
onEndPage: function (writer, document) {
var table = new com.itextpdf.text.pdf.PdfPTable(3);
try {
table.setWidths([24, 24, 2]);
table.setTotalWidth(527);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(com.itextpdf.text.Rectangle.BOTTOM);
table.addCell(header);
table.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
table.addCell("Page " + writer.getPageNumber() + "%d of");
var cell = new com.itextpdf.text.pdf.PdfPCell(com.itextpdf.text.Image.getInstance(total));
cell.setBorder(com.itextpdf.text.Rectangle.BOTTOM);
table.addCell(cell);
table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
} catch (de) {
throw de;
}
},
onCloseDocument: function (writer, document) {
com.itextpdf.text.pdf.ColumnText.showTextAligned(total,
com.itextpdf.text.Element.ALIGN_LEFT,
new com.itextpdf.text.Phrase(new java.lang.String.valueOf(writer.getPageNumber() - 1)), 2, 2, 0);
}
});
我经历过尝试扩展Java类的几种变体,但似乎没有按预期工作。以上只是我失败的尝试之一。我尝试过使用JavaAdapter,但也失败了。