我现在正在使用 iTextSharp (5.4.5) 几个星期。
本周,我在文档中的元素顺序方面遇到了一些奇怪的事情。
我正在制作一份包含主题和图像(图表)的 pdf 报告。
该文档的格式如下:
NR。主题 1 的主题标题
主题 1 的图表图像(来自 bytearray)
NR。主题 2 的主题标题
主题 2 的图表图像 ...
下面是一个代码示例。我知道代码并不完全正确,但它只是指出问题。
假设循环运行 10 次,因此我预计 10 个主题标题都紧跟在图像后面。
我注意到的是,如果到达页面末尾并且应添加新图像,则图像将移动到下一页,并且下一个主题标题将打印在上一页上。
所以在纸面上我们有:
page 1:
主题1
图片主题 1
主题2
图片主题 2
主题3
主题4
page 2:
图片主题 3
图片主题 4
主题5
图片主题 5
...
因此,纸上元素的顺序与我通过 Document.add 方法将元素放入文档中的顺序不同。
这实在是太奇怪了。有人有什么想法吗?
int currentQuestionNr = 0;
foreach (Topic currentTOPIC in Topics)
{
currentQuestionNr++;
//compose question (via table so all questions (with nr prefix) are aligned the same)
PdfPTable questionTable = new PdfPTable(2);
questionTable.WidthPercentage = 100;
questionTable.SetWidths(new int[] { 4, 96 });
PdfPCell QuestionNrCell = new PdfPCell();
QuestionNrCell.BorderWidth = 0;
QuestionNrCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
QuestionNrCell.VerticalAlignment = PdfPCell.ALIGN_TOP;
QuestionNrCell.AddElement(new Paragraph(String.Format("{0}. ", currentQuestionNr), PdfUtility.font_10_bold));
PdfPCell QuestionPhraseCell = new PdfPCell();
QuestionPhraseCell.BorderWidth = 0;
QuestionPhraseCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT;
QuestionPhraseCell.VerticalAlignment = PdfPCell.ALIGN_TOP;
QuestionPhraseCell.AddElement(new Paragraph(currentTOPIC.Title, PdfUtility.font_10_bold));
questionTable.addCell(QuestionNrCell);
questionTable.addCell(QuestionPhraseCell);
//add topic to document
Document.add(questionTable)
//compose image
Image itextImage = GetImageForTopic(currentTOPIC); //let's assume this function returns an image!
Paragraph chartParagraph = new Paragraph();
chartParagraph.IndentationLeft = indentionForQuestionInfo;
chartParagraph.Add(itextImage);
//add image to document
Document.Add(chartParagraph);
}
如果你有一个PdfWriter
实例(例如writer
),你需要强制 iText 使用严格的图像序列,如下所示:
writer.setStrictImageSequence(true);
否则,iText 将推迟添加图像,直到页面上有足够的空间来添加图像。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)