来源:
writer = PdfWriter.getInstance(document, new FileOutputStream("D://MetaData.pdf"));
document.open();
StringBuffer sb = new StringBuffer(text);
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream("D:\\work\\iTextPDF\\sample1.html"));
PdfContentByte pcb = writer.getDirectContent();
ColumnText ct = new ColumnText(pcb);
ct.setAlignment(Element.ALIGN_JUSTIFIED);
ct.setText(new Phrase(sb.toString()));
float[] left = { 36, (PageSize.A4.getWidth() / 2) + 18 };
float[] right = { (PageSize.A4.getWidth() / 2) - 18, PageSize.A4.getWidth() - 36 };
int status = ColumnText.NO_MORE_COLUMN;
int column = 0;
while (ColumnText.hasMoreText(status)) {
ct.setSimpleColumn(left[column], 36, right[column], PageSize.A4.getHeight() - 36);
status = ct.go();
column++;
if (column > 1) {
column = 0;
document.newPage();
}
}
在我的源代码中我正在使用XMLWorkerHelper
和ColumnText
,但是XMLWorkerHelper
解析数据并写入文档,但我想从html解析数据并设置为{{1}构建文档后。请提前帮助我谢谢。