0
votes

I'm working on making a little program that just takes a .txt file and converts it into a PDF, and I want the PDF to look similar to how the text file would look if I used Microsoft's Print to PDF. I have it pretty close, but when a line of text exceeds the width of the page it wraps to a new line and the wrapped text overlaps the text above it. How do I get the wrapped text to behave as if I'm adding a new paragraph to the document without splitting the wrapped text into a new paragraph?

Here's my old code:

    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();

Here's my new code:

    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();

Here's an example of what the output looks like: output

Edit

With mkl's recommendation I replaced p.SetFixedLeading(4.8f) with document.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, 1.018f)). That fixed the spacing issue for the wrapped text, but it caused the space between the paragraphs to increase more than I wanted. In order to get around that, I decided to only use one paragraph object and add each line as a new text object to the paragraph. I had tried that once before, but the text objects weren't going on new lines. I had to add the new line character to the beginning of each text object in order for them to be on their own line.

This is how the output looks now: newoutput

1
You use a leading smaller than the font size. This makes lines overlap. - mkl
@mkl Thank you for the leading explanation! I have modified my code and it now achieves my desired spacing. Thank you for your help! - JR_M

1 Answers

0
votes

Rather than making each line from the txt file be a new paragraph, I changed each line to be a new text object, then I added each text object to a single paragraph. I then stopped using fixed leading on the paragraph and started using multiplied leading on the document itself using document.SetProperty(Property.LEADING, new Leading(Leading.MULTIPLIED, 1.018f)). I could've also used p.SetMultipliedLeading(1.018f) though. This achieved my desired spacing in the document.