問題は、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();
}
このサンプル コードはこれを作成します。

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.