我使用ITextSharp生成PDF,并且需要通过ColumnText使用DirectContent。

问题是,在通过ColumnText对象写入文本后,PdfDocument.GetVerticalPosition还没有更新?请参阅以下测试失败:

    public void TestGetVerticalPositionIsUpdated()
    {
        PdfContentByte cb = Writer.DirectContent;
        var columnText = new ColumnText(cb);

        float position1 = Writer.GetVerticalPosition(true);
        columnText.SetSimpleColumn(Document.Left,
                                   Document.Bottom, 
                                   Document.Right, 
                                   position1, 
                                   0, 
                                   Element.ALIGN_JUSTIFIED);

        columnText.AddText(new Phrase("Test test test test test\nTest test test test test"));
        columnText.Go();

        float position2 = Writer.GetVerticalPosition(true);
        Assert.AreEqual(position1, position2);
    }

无论如何都要告诉作者或文档更新文件currentHeight。

obvoius解决方案是使用PdfDocument.SetVerticalPosition(如果它只存在): - )

或者我误解了使用DirectContent的整个概念?

在我看来,如果文档中的当前Y位置无法更新或未自动更新,则在向DirectContent添加内容后无法使用PdfDocument.Add。

有帮助吗?

解决方案

不幸的是,无法操纵文档的currentHeight字段。因此,当您使用DirectContent插入绝对定位的对象时,您不能“强制”使用DirectContent。下一个内容添加到要在绝对定位内容之后插入的文档。

似乎唯一的方法是自己跟踪垂直位置并绝对添加所有内容。

其他提示

您可以使用multicolumntext对象关注列文本对象。然后,您将所有剩余的文档对象放入mct对象。

var mct = new MultiColumnText(yBottomOfColumnText, MultiColumnText.AUTOMATIC);
mct.AddSimpleColumn(doc.Left, doc.Right); //creates one column
for (int i = 0; i < 100; i++)
{
    mct.AddElement(new Paragraph("Test Paragaph Goes HEREEEEEEEE")); //repeats 100 times for test purposes
}

doc.Add(mct);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top