1

我使用 Adob​​e LiveCycle 创建了一个 pdf 文档。我面临的问题是我需要在文档的特定位置添加一些格式化文本。

我怎样才能做到这一点?

    overContent.BeginText();
    overContent.SetTextMatrix(10, 400);
    overContent.ShowText("test");

这只会在指定的位置添加基本文本,我真的需要在文档中添加换行符、项目符号等。

4

1 回答 1

2

我在 Java 中使用 iText,也遇到了类似的情况。我只是想用非格式化文本插入一个简单的换行符。换行符 (\n) 不会飞。我想出的解决方案(它并不漂亮)是:

// read in the sourcefile
reader = new PdfReader(path + sourcefile);

// create a stamper instance with the result file for output
stamper = new PdfStamper(reader, new FileOutputStream(path + resultfile));

// get under content (page)
cb = stamper.getUnderContent(page);
BaseFont bf = BaseFont.createFont();

// createTemplate returns a PdfTemplate (subclass of PdfContentByte)
// (width, height)
template = cb.createTemplate(500, 350);

Stack linelist = new Stack();
linelist.push("line 1 r");
linelist.push("line 2 r");
linelist.push("line 3 r");
int lineheight = 15;
int top = linelist.size() * lineheight;

for (int i = 0; i < linelist.size(); i++) {
    String line = (String) linelist.get(i);
    template.beginText();
    template.setFontAndSize(bf, 12);
    template.setTextMatrix(0, top - (lineheight * i));
    template.showText(line);
    template.endText();
}

cb.addTemplate(template, posx, (posy-top));
stamper.close();
  1. 我将我的行分成一个数组/堆栈/列表
  2. 我循环遍历该列表和 setText 一次一行

必须有更好的方法,但这适用于我的情况(暂时)。

于 2010-02-25T20:19:48.413 回答