c# - 从使用 GemBox 正确格式化的文本文件中获取 PDF 文档时遇到问题
问题描述
我正在尝试使用 GemBox 将文本文件转换为 pdf。我可以正确导入文本,但没有应用字体类型和大小,句子间距似乎加倍。
这是我到目前为止所拥有的:
public static void CreateDoc(string ebillpath)
{
using (var sr = new StreamReader(ebillpath))
{
var doc = new DocumentModel();
doc.DefaultCharacterFormat.Size = 10;
doc.DefaultCharacterFormat.FontName = "Courier New";
var section = new Section(doc);
doc.Sections.Add(section);
string line;
var clearedtop = false;
while ((line = sr.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line) && !clearedtop)
{
continue;
}
clearedtop = true;
Paragraph paragraph2 = new Paragraph(doc, new Run(doc, line));
section.Blocks.Add(paragraph2);
}
PageSetup pageSetup = new PageSetup(); // section.PageSetup;
var pm = new PageMargins();
pm.Bottom = 36;
pm.Top = 36;
pm.Right = 36;
pm.Left = 36;
pageSetup.PageMargins = pm;
doc.Save(@"d:\temp\test.pdf");
}
}
此文本文件使用空格来正确格式化文本,因此我需要将字体设置为 Courier New。
这是具有正确格式的文本文件的示例:
这就是它以 pdf 形式出现的样子:
每行似乎都加倍并且没有应用字体。
有什么建议么?
解决方案
尝试这个:
public static void CreateDoc(string ebillpath)
{
DocumentModel doc = new DocumentModel();
CharacterFormat charFormat = doc.DefaultCharacterFormat;
charFormat.Size = 10;
charFormat.FontName = "Courier New";
ParagraphFormat parFormat = doc.DefaultParagraphFormat;
parFormat.SpaceAfter = 0;
parFormat.LineSpacing = 1;
// It seems you want to skip first line with 'clearedtop'.
// So maybe you could just use this instead.
string text = string.Concat(File.ReadLines(ebillpath).Skip(1));
doc.Content.LoadText(text);
Section section = doc.Sections[0];
PageMargins margins = section.PageSetup.PageMargins;
margins.Bottom = 36;
margins.Top = 36;
margins.Right = 36;
margins.Left = 36;
doc.Save(@"d:\temp\test.pdf");
}
我希望它有帮助。
推荐阅读
- python - 使用 Python 熊猫的嵌套 JSON 到 CSV 转换
- php - 更新json替换php中表单中的变量输入
- javascript - 如何在 PrimeNG 表上使用“分页符之前或之后”?
- linq - LINQ 从字符串中连续查找 3 次以上
- angular - 在 Angular 中显示来自服务的值时出错
- jsf - 在 p:dataTable 中输入文本的 ajax 调用需要很长时间才能呈现响应
- javascript - TypeError: Object(...) is not a function with React Table and moment.js
- node.js - 如何使用多个条件更新 Google Cloud Datastore 中的实体?
- widget - 带有 extension.xml 的 XPage 小部件
- postgresql - How do you JSON Marshall a pq.NullTime in UTC rather than the local timezone?