简体   繁体   English

使用GemBox从格式正确的文本文件获取PDF文档时遇到问题

[英]Having problems getting PDF document from text file formatted correctly with GemBox

I am attempting to convert a text file to a pdf using GemBox. 我正在尝试使用GemBox将文本文件转换为pdf。 I can get the text imported correctly but the font type and size aren't being applied and the sentence spacing seems to be doubled. 我可以正确导入文本,但是未应用字体类型和大小,并且句子间距似乎增加了一倍。

This is what I have so far: 这是我到目前为止的内容:

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");
      }
    }

This text file uses spaces to format the text correctly so I need to set the font to Courier New. 该文本文件使用空格来正确设置文本格式,因此我需要将字体设置为Courier New。

This is an example of what the text file looks like with correct formatting: 这是正确格式的文本文件的示例:

在此处输入图片说明

And this is what it comes out to look like in pdf form: 这就是pdf格式的结果:

在此处输入图片说明

Each line seems to be doubled and the font isn't being applied. 每行似乎都加倍了,并且没有应用字体。

Any suggestions? 有什么建议么?

Try this: 尝试这个:

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");
}

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM