0

PDFドキュメントを作成して操作するために、iTextライブラリを使用しています。「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

ContentWriterヘルパー クラスはインスタンスのみを必要とするため、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 に答える