无法将文字标记为横向PDF

时间:2018-06-23 19:00:22

标签: java pdf itext pdf-generation pdfstamper

我正在使用iText 5.0将静态文本添加到少量现有的PDF中。以下代码适用于纵向的PDF,但不适用于横向的PDF。我一直在努力使这个工作正常进行,将不胜感激我在这里可能缺少的任何提示。

以横向方式采样PDF:http://mirrors.ibiblio.org/CTAN/macros/latex/contrib/mwe/example-image-a4-landscape.pdf

代码块:

private static void  addTextToPDF(int pageNr, PdfReader reader, PdfStamper pdfStamper) {

    for (int i = 1; i <= reader.getNumberOfPages(); i++)
    {
        Rectangle pageSize = reader.getPageSizeWithRotation(i);

        PdfContentByte pdfPageContents = pdfStamper.getUnderContent(i);
        pdfPageContents.beginText();

        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pdfPageContents.setFontAndSize(baseFont, 40); 
        pdfPageContents.setRGBColorFill(255, 0, 0); 


        pdfPageContents.showTextAligned(PdfContentByte.ALIGN_CENTER, "Put this text on the page!!",
                pageSize.getWidth()/2,
                pageSize.getHeight()/2,
                0.0f);

        pdfPageContents.endText();

        System.out.println("Wriiten at:" + pageSize.getWidth()/2 + " " + pageSize.getHeight()/2);
    }
    pdfStamper.setFormFlattening(true);

}

0 个答案:

没有答案