C# 在iTextSharp中,我们可以设置pdfwriter的垂直位置吗?

C# 在iTextSharp中,我们可以设置pdfwriter的垂直位置吗?,c#,.net,pdf,pdf-generation,itextsharp,C#,.net,Pdf,Pdf Generation,Itextsharp,我最近开始使用iTextSharp从数据生成PDF报告。它工作得很好 在一个特定的报告中,我需要一个部分始终显示在页面底部。我正在使用PdfContentByte从底部创建一条虚线200f: cb.MoveTo(0f, 200f); cb.SetLineDash(8, 4, 0); cb.LineTo(doc.PageSize.Width, 200f); cb.Stroke(); 现在我想在该行下面插入内容。但是,(正如预期的那样)PdfContentByte方法不会更改PdfWriter的垂

我最近开始使用iTextSharp从数据生成PDF报告。它工作得很好

在一个特定的报告中,我需要一个部分始终显示在页面底部。我正在使用PdfContentByte从底部创建一条虚线200f:

cb.MoveTo(0f, 200f);
cb.SetLineDash(8, 4, 0);
cb.LineTo(doc.PageSize.Width, 200f);
cb.Stroke();
现在我想在该行下面插入内容。但是,(正如预期的那样)PdfContentByte方法不会更改PdfWriter的垂直位置。例如,新段落会出现在页面的前面

// appears wherever my last content was, NOT below the dashed line
doc.Add(new Paragraph("test", _myFont));
是否有某种方法可以指示pdfwriter我现在要将垂直位置提升到虚线下方,并继续在那里插入内容?有一个GetVerticalPosition()方法——如果有一个相应的Setter:-)就好了


那么,有没有办法手动设置作者的位置?谢谢

好吧,我想答案有点明显,但我在寻找一种具体的方法。垂直位置没有设置器,但您可以轻松地使用writer.GetVerticalPosition()和paragration.SpacingBefore的组合来实现此结果

我的解决方案:

cb.MoveTo(0f, 225f);
cb.SetLineDash(8, 4, 0);
cb.LineTo(doc.PageSize.Width, 225f);
cb.Stroke();

var pos = writer.GetVerticalPosition(false);

var p = new Paragraph("test", _myFont) { SpacingBefore = pos - 225f };
doc.add(p);

除了前面的空格外,通常的方法是使用
PdfContentByte
添加文本,而不是直接添加到
文档中

// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1002.pdf", FileMode.Create));
document.Open();

// we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;

// we tell the ContentByte we're ready to draw text
cb.beginText();

// we draw some text on a certain position
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");

// we tell the contentByte, we've finished drawing text
cb.endText();

是的,我们可以这样做,但这仍然不能改变作者的垂直位置。所以,如果我添加一个新的段落、短语、区块、表格或其他项目,它仍然会出现在其他地方。我想。
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap1002.pdf", FileMode.Create));
document.Open();

// we grab the ContentByte and do some stuff with it
PdfContentByte cb = writer.DirectContent;

// we tell the ContentByte we're ready to draw text
cb.beginText();

// we draw some text on a certain position
cb.setTextMatrix(100, 400);
cb.showText("Text at position 100,400.");

// we tell the contentByte, we've finished drawing text
cb.endText();