Free Support Forum - aspose.com

How to insert page break when converting text-file?

I have a text-file with page-breaks char(12)

How do I get the same page breaks when using the example?

Using the Generator i version 17.2, it worked by replacing char(12) with “#$NP”

Best regards

@kirkholt

We need to investigate the feasibility of this feature in DOM approach. Would you kindly share your sample TXT file with us. We will test the scenario in our environment and address it accordingly.

The textfile is here:
https://microtech.dk/behold.txt

This is my code for conversion:

    Document doc = new Document();

    doc.PageInfo.Width = PageSize.A4.Height;
    doc.PageInfo.Height = PageSize.A4.Width;

    doc.PageInfo.Margin = new MarginInfo { Left = 28F, Right = 28F, Top = 28F, Bottom = 28F };



    // Add a new page in Pages collection of Document
    Page page = doc.Pages.Add();


    // Create font and mark it to be embedded
    Font font = FontRepository.FindFont("Courier New");
    font.IsEmbedded = true;
    // Create an instance of TextFragmet and pass the text from reader object to its constructor as argument

    string indhold = "";
    using (FileStream fsR = new FileStream(@"..\..\behold.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read))
    {
        using (StreamReader sr = new StreamReader(fsR, Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage)))
        {
            // indlæs hele filen i en string
            indhold = sr.ReadToEnd();
        }
    }

    TextFragment text = new TextFragment(indhold);
    text.TextState.Font = font;
    text.TextState.FontSize = 8F;
    text.TextState.LineSpacing = 4.5F;

    // Add a new text paragraph in paragraphs collection and pass the TextFragment object
    page.Paragraphs.Add(text);
    doc.Save(@"..\..\behold.pdf");

I solved the problem by splitting the file content at pagebreaks myself:

        Document doc = new Document();

        doc.PageInfo.Width = PageSize.A4.Height;
        doc.PageInfo.Height = PageSize.A4.Width;

        doc.PageInfo.Margin = new MarginInfo { Left = 28F, Right = 28F, Top = 28F, Bottom = 28F };

        // Create font and mark it to be embedded
        Font font = FontRepository.FindFont("Courier New");
        font.IsEmbedded = true;
        // Create an instance of TextFragmet and pass the text from reader object to its constructor as argument

        string indhold = "";
        using (FileStream fsR = new FileStream(@"..\..\behold.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read))
        {
            using (StreamReader sr = new StreamReader(fsR, Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage)))
            {
                // indlæs hele filen i en string
                indhold = sr.ReadToEnd();
            }
        }

        foreach (string side in indhold.Split((char)12))
        {
            // Add a new page in Pages collection of Document
            Page page = doc.Pages.Add();

            TextFragment text = new TextFragment(side);
            text.TextState.Font = font;
            text.TextState.FontSize = 8F;
            text.TextState.LineSpacing = 4.5F;

            // Add a new text paragraph in paragraphs collection and pass the TextFragment object
            page.Paragraphs.Add(text);
        }
        doc.Save(@"..\..\behold.pdf");
    }

@kirkholt

It is good to know that you managed to achieve your requirements. Please keep using our API and in case you need any assistance, please let us know.