我在PDF文档的左下角渲染了3个字符。我这样做是通过创建一个Column然后相应地使用正确的尺寸来定位列以匹配Chunk(或者在这种情况下,段落)
如果你在LINQPad中运行以下代码(或者在任何地方)你会看到输出中缺少'Z'字符,我无法解决原因。 X轴设置为与块的宽度相同的宽度。
var pdfDocument = new Document(PageSize.A4);
using (var ms = new MemoryStream())
{
using (pdfDocument)
{
using (var writer = PdfWriter.GetInstance(pdfDocument, ms))
{
pdfDocument.Open();
pdfDocument.NewPage();
var cb = writer.DirectContent;
var font = FontFactory.GetFont("Arial", 16);
var chunk = new Chunk("XYZ", font);
float textWidth = chunk.GetWidthPoint();
var p = new Paragraph(chunk);
var ct = new ColumnText(cb);
ct.SetSimpleColumn(0, 0, textWidth, p.TotalLeading);
ct.AddElement(p);
ct.Go();
pdfDocument.Close();
}
}
var tmpPdfPath = System.IO.Path.GetTempFileName() + ".pdf";
File.WriteAllBytes(tmpPdfPath, ms.ToArray());
Process.Start(tmpPdfPath);
}