添加绝对定位文本

col*_*ash 18 itextsharp

我正在尝试生成一个字母,留下一个空白点然后在其顶部粘贴地址,具体取决于信封窗口的位置.

所以我开始这样做:

Document doc = new Document(PageSize.LETTER, 72, 72, 72, 72);
var w = PdfWriter.GetInstance(doc, output);
Font font = FontFactory.GetFont("arial", 10);
doc.Open();
doc.Add(new Paragraph("date", font) { SpacingAfter = 5 });
doc.Add(new Paragraph("\n\n\n\n\n\n", font));//empty spot
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 });
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 });
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 });
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 });
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 });

float llx = 63f, lly = 450f, urx = 387f, ury = 531f;
?? Somehow add "name\n address line 1\n address line2\n city state zip"

doc.Close();
Run Code Online (Sandbox Code Playgroud)

我希望能够在这些坐标处添加一些文字,但我无法弄清楚如何...有人知道这样做的方法吗?

col*_*ash 24

找到答案" 这里 ".(以下是来自Yannick Smits的引用答案)

===============

试试这个:

ColumnText ct = new ColumnText(cb);
Phrase myText = new Phrase("TEST paragraph\nNewline");
ct.SetSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT);
ct.Go();
Run Code Online (Sandbox Code Playgroud)

SetSimpleColumn的参数是:

  1. 词组
  2. 左下-X
  3. 左下-Y
  4. 右上角x(llx +宽度)
  5. 右上y(lly +身高)
  6. 领先(打印行之间的空白量)
  7. 对准.


Mik*_*sky 18

您还可以将ContentByte与文本矩阵一起使用,以便随时随地绘制文本.

PdfContentByte cb = writer.DirectContent;
cb.BeginText();
BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetFontAndSize(f_cn, 6);
cb.SetTextMatrix(475, 15);  //(xPos, yPos)
cb.ShowText("Some text here and the Date: " + DateTime.Now.ToShortDateString());
cb.EndText();
Run Code Online (Sandbox Code Playgroud)

好处是,如果您不必绘制文本的完整大小,文本将进入.使用"简单列",您将在文档上绘制一个矩形,并将文本放在其中.使用ContentByte,您可以躲避矩形,并自行定位文本.