如何编辑PdfTemplate宽度(用Java编写)

时间:2016-01-27 07:40:18

标签: itextsharp itext itextpdf

是否可以在关闭文档之前编辑PdfTemplate对象的宽度? 在我的pdf过程中,我创建了一个文档,并为写入总页数设置了一个模板。我设置宽度,但它不起作用:

// onOpenDocument
PdfTemplate pageNumTemplate = writer.getDirectContent().createTemplate(10, 20);
globalDataMap.put("myTemplate", pageNumTemplate)

// onCloseDocument
Font font = (Font) globalDataMap.get("myFont");
String pagenum = String.valueOf(writer.getPageNumber());

float widthPoint = font.getBaseFont().getWidthPoint(pagenum, font.getSize());
PdfTemplate pdfTemplate = (PdfTemplate) globalDataMap.get("myTemplate");
pdfTemplate.setWidth(widthPoint);
ColumnText.showTextAligned(pdfTemplate, Element.ALIGN_LEFT, new Phrase(pagenum), 0, 1, 0);

1 个答案:

答案 0 :(得分:1)

OP初始代码中的错误

您使用ColumnText.showTextAligned作为第三个参数致电String

String pagenum = String.valueOf(writer.getPageNumber());
[...]
ColumnText.showTextAligned(pdfTemplate, Element.ALIGN_LEFT, pagenum, 0, 1, 0);

但是所有ColumnText.showTextAligned重载都需要Phrase

public static void showTextAligned(final PdfContentByte canvas, final int alignment, final Phrase phrase, final float x, final float y, final float rotation)

public static void showTextAligned(final PdfContentByte canvas, int alignment, final Phrase phrase, final float x, final float y, final float rotation, final int runDirection, final int arabicOptions)

(至少达到目前的5.5.9-SNAPSHOT开发版本)。

因此,您可能想要使用

ColumnText.showTextAligned(pdfTemplate, Element.ALIGN_LEFT, new Phrase(pagenum), 0, 1, 0);

代替。

但现在可行了

基于OP的代码,我构建了这个概念验证:

try (   FileOutputStream stream = new FileOutputStream(new File(RESULT_FOLDER, "dynamicTemplateWidths.pdf"))    )
{
    Document document = new Document(PageSize.A6);
    PdfWriter writer = PdfWriter.getInstance(document, stream);
    writer.setPageEvent(new PdfPageEventHelper()
    {
        PdfTemplate dynamicTemplate = null;
        Font font = new Font(BaseFont.createFont(), 12);
        String postfix = "0123456789";

        @Override
        public void onOpenDocument(PdfWriter writer, Document document)
        {
            super.onOpenDocument(writer, document);
            dynamicTemplate = writer.getDirectContent().createTemplate(10, 20);
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document)
        {
            writer.getDirectContent().addTemplate(dynamicTemplate, 100, 300);
        }

        @Override
        public void onCloseDocument(PdfWriter writer, Document document)
        {
            float widthPoint = font.getBaseFont().getWidthPoint(postfix, font.getSize());
            dynamicTemplate.setWidth(widthPoint / 2.0f);
            ColumnText.showTextAligned(dynamicTemplate, Element.ALIGN_LEFT, new Phrase(String.valueOf(writer.getPageNumber()) + "-" + postfix), 0, 1, 0);
        }

    });
    document.open();

    document.add(new Paragraph("PAGE 1"));
    document.newPage();
    document.add(new Paragraph("PAGE 2"));

    document.close();
}

输出:

dynamicTemplateWidths.pdf

考虑到我将模板宽度设置为0123456789中后缀onCloseDocument宽度的一半,这与预期完全一样。