简体   繁体   English

使用itext调整添加到pdf文件的字符串的大小

[英]sizing a String added to a pdf file using itext

I have the following code : 我有以下代码:

PdfReader reader = new PdfReader("doc.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("AttestationTemp.pdf"));
PdfContentByte over = stamper.getOverContent(1);
BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, false);
over.setFontAndSize(bf, 50);
over.beginText();
ColumnText.showTextAligned(over, Element.ALIGN_LEFT, new Phrase("String"), 110, 384, 0);
over.endText();

try
{
    if (Desktop.isDesktopSupported())
    {
        File myPDF = new File("doc.pdf");
        Desktop.getDesktop().open(myPDF);
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

The problem is that when I change the size of my String using the setFontAndSize method, I get the same result (same small size), so I want to know how to set the size of the string added to the pdf file. 问题是,当我使用setFontAndSize方法更改String的大小时,会得到相同的结果(大小相同),因此我想知道如何设置添加到pdf文件中的字符串的大小。

iText offers APIs for PDF content generation at different levels. iText提供了用于在不同级别生成PDF内容的API。

There is a very low level API of PdfContentByte methods. PdfContentByte方法的API级别PdfContentByte At this level you manually add individual content stream operations one-by-one, eg: 在此级别上,您要手动手动添加单个内容流操作,例如:

PdfContentByte over = ...;
over.setFontAndSize(bf, 50);
over.beginText();
over.setTextMatrix(110, 384);
over.showText("Hello World!");
over.endText();

Then there is the higher level API of ColumnText methods. 然后是ColumnText方法的更高级别的API。 At this level you construct and style your text using Phrase and Chunk objects and the ColumnText methods create the corresponding content stream operations: 在此级别上,您将使用PhraseChunk对象构造文本并设置其样式, ColumnText方法将创建相应的内容流操作:

PdfContentByte over = ...;
Phrase phrase = new Phrase("Hello World!", new Font(bf, 50));
ColumnText.showTextAligned(over, Element.ALIGN_LEFT, phrase, 110, 384, 0);

The code of the OP mixes instructions at these levels which in his case results in his font setting call to later be overridden and, therefore, ignored. OP的代码在这些级别上混合了指令,在这种情况下,导致他的字体设置调用在以后被覆盖,因此被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM