【发布时间】:2022-08-20 17:07:45
【问题描述】:
我正在使用 ColumnDocumentRenderer 在两列中绘制内容。
下面是代码。
public void ManipulatePdf(string dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.A4);
doc.SetMargins(55f, 55f, 45f, 55f);
var interval = 20f;
var columnWidth = (doc.GetPdfDocument().GetDefaultPageSize().GetWidth() - 110 - interval) / 2;
var pageHeight = doc.GetPdfDocument().GetDefaultPageSize().GetHeight() - 110;
var baseText = \"We have seen too many reports, too many words, too many good intentions, too many families torn apart, and too many excruciatingly painful deaths to see yet more delays in taking collective action.\";
var pTitle = new Paragraph(baseText);
pTitle.SetFontSize(20);
pTitle.SetTextAlignment(iText.Layout.Properties.TextAlignment.JUSTIFIED);
doc.Add(pTitle);
var currentYLine = doc.GetRenderer().GetCurrentArea().GetBBox().GetTop();
Rectangle[] columns = {
new Rectangle(55, 55,
columnWidth,
currentYLine - 55),
new Rectangle(55 + columnWidth + interval, 55,
columnWidth,
currentYLine - 55) };
doc.SetRenderer(new ColumnDocumentRenderer(doc, columns));
for (int i = 1; i <= 12; i++)
{
var text = baseText;
for (int j = 1; j <= i; j++)
{
text += \" Additional Text. \";
}
var p = new Paragraph(text);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.JUSTIFIED);
doc.Add(p);
if (i == 6)
{
var tp = new Paragraph(\"Introduction\");
tp.SetFontSize(20);
tp.SetMarginTop(50);
tp.SetTextAlignment(iText.Layout.Properties.TextAlignment.JUSTIFIED);
doc.Add(tp);
}
}
doc.Close();
}
以下是创建的PDF:
请看一下这个截图中的红线,我的问题是如何使两列的底部对齐成一条直线?
感谢和问候。
-
好吧,如果我们对齐两列的底部,那么我们需要移动一些内容,而其他行将不会对齐 - 那么你想从哪里获得额外的空间或者你想挤压哪些行来实现你的目标?
-
嗨 Alexey,实际上这只是一个简单的示例,pdf 页面可能包含标题、图像或表格,而不仅仅是文本段落。我想要的是如何计算底部的额外空间,以便我可以挤压某个地方(例如,从\'Introduction\'标题)来实现目标。
-
因此,您只想知道最后两行的垂直位置之间的差异,但您会“知道”哪些内容调整以及如何调整,对吧?你不指望自动算法来处理吗?
-
嗨 Alexey,我认为自动算法是必要的,因为我想在没有任何人工干预的情况下自动创建整个 pdf。根据我的理解,如果我可以提前计算出垂直位置的差异,那么可以通过调整一些行距或段落边距来明确差异。
-
假设您的字体大小为 10pt。两列中最后两行之间的差异为 9.5pt。该算法调整了一些两行的行距并在那里增加了 9.5pt。结果你能接受吗?我认为它看起来会很奇怪。