【发布时间】:2017-11-28 02:41:41
【问题描述】:
我正在使用 iTextSharp c# 创建一个 pdf,当一段文本运行到新页面时遇到问题。 以下工作正常,直到例如第 2 节运行到下一页。
private Font _smallFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
private Font _smallBoldFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
AddParagraph(doc, Element.ALIGN_LEFT, _smallBoldFont, new Paragraph("\nSection 1\n"));
AddParagraph(doc, Element.ALIGN_LEFT, _smallFont, new Paragraph(textBox2.Text));
AddParagraph(doc, Element.ALIGN_LEFT, _smallBoldFont, new Paragraph("\nSection 2\n"));
AddParagraph(doc, Element.ALIGN_LEFT, _smallFont, new Paragraph(textBox3.Text));
在覆盖的 base.OnStartPage 中,我添加了一个徽标和标题文本。
Image logoImage = Image.GetInstance(appPath + "\\logo.jpg");
logoImage.ScaleAbsolute(newWidth: 141f, newHeight: 65f);
logoImage.Alignment = Element.ALIGN_CENTER;
document.Add(logoImage);
var headerText = "\nReport Header " + "\n";
Paragraph subText = new Paragraph(headerText, new Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
document.Add(subText);
cb.AddTemplate(templateH, pageSize.GetLeft(document.LeftMargin), pageSize.GetTop(document.TopMargin - 5));
除了行距增加到大约 1.5(默认?)直到新的 AddParagraph 之外,这有效。即使标题文本的字体和大小相同,也会发生这种情况。我有一个非常肮脏的解决方法,我在标题中添加了第二行,字体大小为 Helvetica 8。这确保了与上一页相同的行距。为什么 8 我不知道这些部分是 Helvetica 10。我尝试在 AddParagraph 中设置前导,但这没有帮助。 我做错了什么?
【问题讨论】:
-
您是否尝试过使用不同的字体,列宽可能需要随着调整为较小的字体大小而减小。我过去也遇到过类似的问题
-
是的,尝试了不同的字体:-(我没有使用表格,只是输出段落。