This is a migrated thread and some comments may be shown as answers.

Page break - HtmlFormatProvider + PdfFormatProvider

8 Answers 212 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
william
Top achievements
Rank 1
william asked on 03 Feb 2017, 01:59 PM

Hello,
I'm having trouble breaking a new page using HtmlFormatProvider and PdfFormatProvider

My code

var html = "<h1>firstpage</h1><?hard-pagebreak?><h1>secondpage</h1>";
HtmlFormatProvider prov = new HtmlFormatProvider();
RadFlowDocument document = prov.Import(html);
 
var provider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
var ouput = provider.Export(document);
File.WriteAllBytes(@"D:\a.pdf", ouput);

 

What am I doing wrong ?

 

 

8 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 08 Feb 2017, 01:59 PM
Hello William,

The page breaks are not supported in the model of the HTML documents. If you would like, you can import the desired document with the HtmlFormatProvider and add a page break to the content using the Break element.

Hope this is helpful.

Regards,
Tanya
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
william
Top achievements
Rank 1
answered on 08 Mar 2017, 11:51 AM
Do you have any examples of how I can do this?
I need to replace <?hard-pagebreak?> with a new page
0
william
Top achievements
Rank 1
answered on 09 Mar 2017, 11:35 AM

I solved my problem using with the following code

 

private void PrepareHtml()
{
    editor.Content = editor.Content.Replace("<?hard-pagebreak?>", "<b>[PAGEBREAK]</b>");
}
 
private void InsertPageBreak(RadFlowDocument document)
{
    foreach (var fieldCharacter in document.EnumerateChildrenOfType<Paragraph>().ToArray())
    {
        foreach (var inline in fieldCharacter.Inlines.ToList())
        {
            if (((inline is InlineBase)) && ((inline as Run) != null) && (((Run) inline).Text == "[PAGEBREAK]"))
            {
                var index = fieldCharacter.Inlines.IndexOf(inline);
                var breakPage = new Break(document);
                breakPage.BreakType = BreakType.PageBreak;
 
                fieldCharacter.Inlines.Insert(index, breakPage);
                fieldCharacter.Inlines.RemoveAt(index + 1);
            }
        }
    }
}
0
Tanya
Telerik team
answered on 13 Mar 2017, 09:48 AM
Hi William,

Yes, replacing the HTML tags with keywords which are later replaced by the Break element should do the trick. You can take a look at the Break help article to check the different types of breaks you can use and their properties.

Regards,
Tanya
Telerik by Progress

0
kalpa
Top achievements
Rank 1
answered on 26 Dec 2017, 10:14 AM

Hi Tanya, 

Can i add a section break like this way?? if there is a another way please tell it here.

thank you.

0
Tanya
Telerik team
answered on 28 Dec 2017, 01:20 PM
Hi Kalpa,

The Break element corresponds only to breaks of type Page, Line, and Column. These elements are inserted as inline elements in the document content. Inserting a section break is currently done automatically when inserting a new Section. However, while testing the scenario, I found that there is an issue with the InsertSection() method. I logged that issue and you can find the related public item in our feedback portal.

An option that you could consider is to clone the elements after the position of the break, delete them and move the clonings to a new section element. When all the elements are moved, you could add the section to the document using the document.Sections.Insert() method. However, you should be aware that this won't be an easy task and might require much time to be implemented. I am afraid that I am currently unable to suggest you an easy approach to insert a section break in a document. 

Regards,
Tanya
Progress Telerik

0
kalpa
Top achievements
Rank 1
answered on 29 Dec 2017, 07:11 AM

Hi Tanya,

can you attach a sample project in here? our company already bought this product and we are mainly focusing on word generation process. our users need to add section break. They said they need it soon.

Thanks,

Kalpa.

0
Tanya
Telerik team
answered on 29 Dec 2017, 03:26 PM
Hi Kalpa,

Please, find below a sample snippet demonstrating the main idea of the approach:
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
 
Section newSection = null;
 
for (int i = 0; i < document.Sections.Count; i++)
{
    var section = document.Sections[i];
 
    foreach (var paragraph in section.EnumerateChildrenOfType<Paragraph>().ToArray())
    {
        if (newSection != null)
        {
            section.Blocks.Remove(paragraph);
            newSection.Blocks.Add(paragraph);
        }
 
        foreach (var inline in paragraph.Inlines.ToList())
        {
            if (((inline is InlineBase)) && ((inline as Run) != null) && (((Run) inline).Text == "[BREAK]"))
            {
                paragraph.Inlines.Remove(inline);
                newSection = new Section(document);
                break;
            }
        }
    }
 
    if (newSection != null)
    {
        document.Sections.Insert(i + 1, newSection);
        newSection = null;
    }
}


The document variable is the imported into RadFlowDocument content.

Regards,
Tanya
Progress Telerik

Tags
WordsProcessing
Asked by
william
Top achievements
Rank 1
Answers by
Tanya
Telerik team
william
Top achievements
Rank 1
kalpa
Top achievements
Rank 1
Share this question
or