与TJ运营商合作

时间:2013-07-16 20:51:32

标签: java java-ee itextsharp itext

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

任何回答的答案:)

致以最诚挚的问候,

3 个答案:

答案 0 :(得分: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();
}

此示例代码创建了这个:

letters A, B, C, and D positioned as per the sample code

您还谈到了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

由于ContentWriter助手类只需要PdfContentByte个实例,因此它也可以与PdfStamper.

中某些页面的UnderContent或OverContent一起使用

答案 1 :(得分: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
    }


}

答案 2 :(得分:0)

public void ManipulatePdf(String src,String dest){ PdfReader read = new PdfReader(“document.pdf”);         PdfStamper压模=新的PdfStamper(读取,新的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();

}