C# 防止iText7中的换行文本重叠

C# 防止iText7中的换行文本重叠,c#,itext7,C#,Itext7,我正在制作一个小程序,只需要一个.txt文件并将其转换为PDF,我希望PDF看起来与使用Microsoft的Print to PDF时文本文件的外观相似。我已经非常接近了,但是当一行文本超过页面宽度时,它会换行,换行的文本与上面的文本重叠。如何使包装的文本表现为向文档中添加新段落,而不将包装的文本拆分为新段落 这是我的旧代码: string dest = @"..\TXT2PDF\Test.pdf"; string source = @"..\TXT2PDF\test2.txt"

我正在制作一个小程序,只需要一个.txt文件并将其转换为PDF,我希望PDF看起来与使用Microsoft的Print to PDF时文本文件的外观相似。我已经非常接近了,但是当一行文本超过页面宽度时,它会换行,换行的文本与上面的文本重叠。如何使包装的文本表现为向文档中添加新段落,而不将包装的文本拆分为新段落

这是我的旧代码:

    string dest = @"..\TXT2PDF\Test.pdf";
    string source = @"..\TXT2PDF\test2.txt";
    string fpath = @"..\TXT2PDF\consola.ttf";
    string line;

    FileInfo destFile = new FileInfo(dest);
    destFile.Directory.Create();
    PdfWriter writer = new PdfWriter(dest);
    PdfDocument pdf = new PdfDocument(writer);
    PageSize ps = new PageSize(612, 792);
    pdf.SetDefaultPageSize(ps);
    Document document = new Document(pdf);
    PdfFont font = PdfFontFactory.CreateFont(fpath);
    StreamReader file =  new StreamReader(source);
    Console.WriteLine("Beginning Conversion");
    document.SetLeftMargin(54);
    document.SetRightMargin(54);
    document.SetTopMargin(72);
    document.SetBottomMargin(72);
    while ((line = file.ReadLine()) != null)
    {
        Paragraph p = new Paragraph();
        p.SetFixedLeading(4.8f);
        p.SetFont(font).SetFontSize(10.8f);
        p.SetPaddingTop(4.8f);
        p.Add("\u00A0");
        p.Add(line);                
         document.Add(p);
    }
    document.Close();
    file.Close();
    Console.WriteLine("Conversion Finished");
    Console.ReadLine();
这是我的新代码:

    string dest = @"..\TXT2PDF\Test.pdf";
    string source = @"..\TXT2PDF\test2.txt";
    string fpath = @"..\TXT2PDF\consola.ttf";
    string line;

    FileInfo destFile = new FileInfo(dest);
    destFile.Directory.Create();
    PdfWriter writer = new PdfWriter(dest);
    PdfDocument pdf = new PdfDocument(writer);
    PageSize ps = new PageSize(612, 792);
    pdf.SetDefaultPageSize(ps);
    Document document = new Document(pdf);
    PdfFont font = PdfFontFactory.CreateFont(fpath, "cp1250", true);
    StreamReader file =  new StreamReader(source);
    Console.WriteLine("Beginning Conversion");
    document.SetLeftMargin(54);
    document.SetRightMargin(54);
    document.SetTopMargin(68);
    document.SetBottomMargin(72);
    document.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, 1.018f));
    Paragraph p = new Paragraph();
    p.SetFont(font).SetFontSize(10.8f);
    p.SetCharacterSpacing(0.065f);
    string nl = "";
    while ((line = file.ReadLine()) != null)
    {
        Text t = new Text(nl + "\u0000" + line);
        p.Add(t);
        nl = "\n";
    }
    document.Add(p);
    document.Close();
    file.Close();
    Console.WriteLine("Conversion Finished");
    Console.ReadLine();
下面是一个输出的示例:

编辑

根据mkl的建议,我将p.SetFixedLeading(4.8f)替换为document.SetProperty(Property.LEADING,new LEADING(LEADING.multipled,1.018f))。这修复了包装文本的间距问题,但它导致段落之间的间距增加超过了我的期望。为了解决这个问题,我决定只使用一个段落对象,并将每一行作为新的文本对象添加到段落中。我以前试过一次,但是文本对象没有出现新的行。我必须在每个文本对象的开头添加新行字符,以便它们位于自己的行上

这就是现在输出的外观:

我没有将txt文件中的每一行都变成一个新段落,而是将每一行都变成一个新的文本对象,然后将每个文本对象添加到一个段落中。然后,我停止在段落上使用固定前导,并开始使用document.SetProperty(Property.leading,new leading(leading.multipled,1.018f))在文档本身上使用相乘前导。不过,我也可以使用p.setmultipledLeading(1.018f)。这实现了我在文档中所需的间距。

您使用的前导字号小于字体大小。这会使行重叠。@mkl感谢您的主要解释!我已经修改了我的代码,现在它达到了我想要的间距。谢谢你的帮助!