PdfContentByte和PdfTemplate在iText 7中的替代实现方式是什么?

huangapple go评论80阅读模式
英文:

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 的类 PdfPagePdfFormXObject,还有用于向页面或表单 XObject 添加内容的低级和高级方法的类 PdfCanvasCanvas

因此,以下代码大致对应于您的 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.

huangapple
  • 本文由 发表于 2020年7月23日 21:50:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定