英文:
What is an alternate way to implement PdfContentByte and PdfTemplate in iText 7
问题
produce(com.itextpdf.kernel.pdf.PdfDocument pdfDocument, float width, float height, ...) {
com.itextpdf.kernel.pdf.canvas.PdfCanvas canvas = new com.itextpdf.kernel.pdf.canvas.PdfCanvas(pdfDocument.addNewPage());
try {
PdfFormXObject template = new PdfFormXObject(new Rectangle(width, height));
canvas.addXObject(template, left, pdfDocument.getDefaultPageSize().getHeight() - top - height);
PdfCanvasTemplate templateCanvas = new PdfCanvasTemplate(template);
PdfFont font = ...; // Define your font
float fontSize = ...; // Define your font size
templateCanvas.beginText()
.setFontAndSize(font, fontSize)
.setTextMatrix(new AffineTransform(1, 0, 0, 1, 0, 0))
.showTextAligned(value, width / 2, linePos, TextAlignment.CENTER)
.endText();
} catch(Exception e) {
// Handle exceptions
}
}
英文:
I am working on migration from iText 5 to iText 7. I have iText 5 code as below. I am not sure which alternative from iText 7(may be Canvas) should be used to implement PdfContentByte
and PdfTemplate
.
produce(com.itextpdf.text.pdf.PdfWriter writer, width, height, ...) {
com.itextpdf.text.pdf.PdfContentByte cb = writer.getDirectContent();
com.itextpdf.text.pdf.PdfTemplate template = cb.createTemplate(width, height);
try
{
template.beginText();
template.setFontAndSize(font, fontSize);
template.setTextMatrix(0, 0);
template.showTextAligned(com.itextpdf.text.pdf.PdfContentByte.ALIGN_CENTER, value, width/2, linePos, 0);
template.endText();
}
catch(Exception e)
{
}
cb.addTemplate(template, left, areaTop - top - height);
}
Can anyone please suggest the correct alternative to achieve this?
Thanks!
答案1
得分: 2
PdfWriter.getDirectContent()
方法返回的 PdfContentByte
实例,实质上是当前页面的内容,再加上一些用于添加更多内容的方法。
在 iText 5 中,PdfTemplate
实质上是一个 PDF 表单 XObject,具有其内容以及一些用于添加更多内容的方法。
在 iText 7 中,专门有用于页面和 PDF 表单 XObject 的类 PdfPage
和 PdfFormXObject
,还有用于向页面或表单 XObject 添加内容的低级和高级方法的类 PdfCanvas
和 Canvas
。
因此,以下代码大致对应于您的 iText 5 代码:
PdfDocument pdfDoc = ...
PdfPage page = ... // 例如:pdfDoc.addNewPage();
PdfFormXObject pdfFormXObject = new PdfFormXObject(new Rectangle(width, height));
try (Canvas canvas = new Canvas(pdfFormXObject, pdfDoc)) {
canvas.showTextAligned(value, width/2, linePos, TextAlignment.CENTER);
}
PdfCanvas pdfCanvas = new PdfCanvas(page);
pdfCanvas.addXObject(pdfFormXObject, left, bottom);
(来自 AddCanvasToDocument 测试中的 testAddCanvasForManjushaDC
)
我使用 "大致" 是因为 iText 5 和 iText 7 的架构有所不同,所以不一定存在完全的对应关系,特别是在 iText 5 中的最佳实践并不能直接转化为 iText 7 中的最佳实践。
英文:
The PdfContentByte
instance returned by iText 5 PdfWriter.getDirectContent()
essentially is the content of the current page plus a number of methods to add more content.
An iText 5 PdfTemplate
essentially is a PDF form XObject and its content plus a number of methods to add more content.
In iText 7 there are dedicated classes PdfPage
and PdfFormXObject
for pages and PDF form XObjects respectively, and there are classes PdfCanvas
and Canvas
providing low level and high level methods respectively to add more content to pages or form XObjects.
Thus, the following corresponds approximately to your iText 5 code:
PdfDocument pdfDoc = ...
PdfPage page = ... // e.g. pdfDoc.addNewPage();
PdfFormXObject pdfFormXObject = new PdfFormXObject(new Rectangle(width, height));
try (Canvas canvas = new Canvas(pdfFormXObject, pdfDoc)) {
canvas.showTextAligned(value, width/2, linePos, TextAlignment.CENTER);
}
PdfCanvas pdfCanvas = new PdfCanvas(page);
pdfCanvas.addXObject(pdfFormXObject, left, bottom);
(From AddCanvasToDocument test testAddCanvasForManjushaDC
)
I say "approximately" because the architecture of iText 5 and iText 7 differ, so there is not necessarily an exact correspondence, in particular best practices in iText 5 don't directly translate to best practices in iText 7.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论