【发布时间】:2014-03-31 15:22:05
【问题描述】:
我现在正在使用 iTextSharp (5.4.5) 几个星期。 这周,我在文档中的元素顺序方面遇到了一些奇怪的事情。
我正在编写包含主题和图像(图表)的 pdf 报告。
文档格式如下:
NR。主题 1 的主题标题
主题 1 的图表图像(来自字节数组)
NR。主题 2 的主题标题
主题 2 的图表图像 ...
以下是代码示例。我知道代码并不完全正确,但这只是指出问题所在。 假设循环运行 10 次,所以我预计 10 个主题标题都直接跟在图片后面。
我注意到的是,如果到达页面末尾并应添加新的图像,则图像将移动到下一页,并且下一个主题标题将打印在前一页上。
所以在纸面上我们有:
第 1 页:
主题 1 图片主题1
主题 2 图片主题2
主题 3 话题四
第 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);
}
【问题讨论】:
标签: itextsharp itext