0

我使用 iText 库来创建和操作 PDF 文档。让我们有一个文档,其中包含一个简单的字符串,例如“Hello world”。所以在pdf文件结构中,我们必须有(Hello world)Tj。问题是如何使用java代码为每个字符设置位置为每个字符设置位置(我们也可以讨论 TJ 运算符)。我保证他/她帮助我并给我想法的人,我会将他/她的名字作为我的项目的参考:)

任何答案都值得赞赏:)

此致,

4

3 回答 3

1

问题是如何使用java代码为每个字符设置位置

使用 iText,您可以使用文本定位和显示方法轻松定位任何文本片段(包括单个字符)PdfContentByte.如果要包装该功能,可以使用如下帮助类:

public class ContentWriter
{
    public ContentWriter(PdfContentByte content) throws DocumentException, IOException
    {
        this.content = content;
        BaseFont bf = BaseFont.createFont();
        content.beginText();
        content.setFontAndSize(bf, 12);
    }

    // x and y are offsets relative to the start coordinates of the most recent write call
    public ContentWriter write(float x, float y, String text)
    {
        if (finished)
            throw new IllegalStateException("ContentWritr session already finished.");
        content.moveText(x, y);
        content.showText(text);
        return this;
    }

    public void finish()
    {
        if (!finished)
        {
            content.endText();
            finished = true;
        }
    }

    final PdfContentByte content;
    boolean finished = false;
}

它可以这样使用:

public void testShowSomePositionedContent() throws DocumentException, IOException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("positionedContent.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    new ContentWriter(cb).
        write(100, 400, "A").
        write(20, 0, "B").
        write(18, 2, "C").
        write(10, 7, "D").
        finish();
    document.close();
}

这个示例代码创建了这个:

根据示例代码定位的字母 A、B、C 和 D

正如您还谈到了 PDF 运算符,您可能对 PDF 本身的样子感兴趣:

BT
/F1 12 Tf
100 400 Td
(A)Tj
20 0 Td
(B)Tj
18 2 Td
(C)Tj
10 7 Td
(D)Tj
ET

由于ContentWriterhelper 类只需要一个PdfContentByte实例,因此它也可以与某个页面的 UnderContent 或 OverContent 一起使用PdfStamper.

于 2013-07-17T07:52:40.230 回答
0
public static void CreatePdf(String src){
    Rectangle rec= new Rectangle(400,400);
    Document doc= new Document(rec);
    PdfWriter writer= PdfWriter.getInstance(doc,nweFileOutputStream("doc.pdf"));
    PdfContentByte content=writer.getDirectContent();
    doc.open();
    BaseFont bf=BaseFont.createFont();
    String texte="hello";
    content.setCharacterSpacing((float)2.5);
    content.setFontAndSize(bf,12);
    content.beginText();
    content.showText(texte);
    content.endText();
    document.close();
    }
    public static void ManipulatePdf(String src, String dest){
    PdfReader read= new PdfReader("doc.pdf");
    PdfStamper stamper= new PdfStamper(read,new FileOutPutStream("doc_modifie.pdf"));
    PdfContentByte canvas= stamper.getUnderContent(1);
    canvas.setFontAndSize(bf,12);
    canvas.setCharacterSpacing((float)6);
    canvas.beginText();
    canvas.showText(texte);
    canvas.endText();
    stamper.close();

    //now how to modify the character spacing to 6 for example and then replace the modified //string instead of the old string in the document
    }


}
于 2013-07-17T19:33:32.923 回答
0

public void ManipulatePdf(String src, String dest){ PdfReader read= new PdfReader("document.pdf"); PdfStamper stamper= new PdfStamper(read, new FileOutputStream("document_modifié.pdf"));

    PdfContentByte content= stamper.getUnderContent(1);

    LocationTextExtractionStrategy lteStrategy = new LocationTextExtractionStrategy();
    String texte= PdfTextExtractor.getTextFromPage(read, 1, lteStrategy);
    pdflayer= new PdfLayer("Overrite", stamper.getWriter());
    content.setColorFill(BaseColor.BLACK);
    content.beginLayer(pdflayer);
    content.fill();
    PdfGState pgState = new PdfGState();
    content.setGState(pgState);
    content.setColorFill(BaseColor.WHITE);
    content.setCharacterSpacing((float)6);
    content.beginText();
    content.setTextMatrix(15, 385);
    content.showText("hello");
    content.endText();
    content.endLayer();
    stamper.close();
     read.close();

}

于 2013-07-17T22:34:20.977 回答