Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Aug 24, 2021 8:54 pm

Hi,

I have text placeholders like [[Page_Break]] all over my document. And I would like to replace all these occurrences with real page breaks.

I came up with this extension method so far:

Code: Select all
public static Document ReplaceWithPageBreak(this Document me, string placeholder)
        {
            if (me == null)
                return null;

            if (string.IsNullOrEmpty(placeholder))
                return me;

            var paragraphs = me.GetParagraphs(searchTag: placeholder);
            foreach (var paragraph in paragraphs)
            {
                paragraph.AppendBreak(breakType: BreakType.PageBreak);
            }

            return me;
        }

        // in my main code
       myDocument.ReplaceWithPageBreak("[[Page_Break]]")


This code sets the page breaks perfectly, but the [[Page_Break]] placeholders are still shown. What do I need to do, to delete these placeholders?

Thx,
Ingmar

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Wed Aug 25, 2021 11:17 am

Hello,

Thank you for your inquiry.
Please refer to the following code to achieve your requirement. If there are any questions, please feel free to contact us.
Code: Select all
            Document myDocument = new Document();
            myDocument.LoadFromFile("inputFile");
            //Find the specified text content in the document
            TextSelection[] selections = myDocument.FindAllString("[[Page_Break]]", false, true);
            int index = 0;
            TextRange range = null;
            //Traverse the document, remove the text content, insert a page break
            foreach (TextSelection textSelection in selections)
            {
                //Create a page break 
                Break bk = new Break(myDocument,BreakType.PageBreak);
                range = textSelection.GetAsOneRange();
                var paragraph = range.OwnerParagraph;
                index = paragraph.ChildObjects.IndexOf(range);
                range.OwnerParagraph.ChildObjects.Insert(index, bk);               
                range.OwnerParagraph.ChildObjects.Remove(range);
            }
            myDocument.SaveToFile("out.docx");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Thu Aug 26, 2021 6:03 am

Good morning Annika,

wow, super. Works like a charm. And I learned a lot by inspecting your code.

So, TextSelections lead to Ranges, Ranges lead to Paragraphs. Is the opposite true as well? Paragraphs consist of Ranges? I guess.

May I ask why you don't publish your object model on your Program Guide pages?
For example, I know you are having Sections, Paragraphs, TextSelections etc. But I am not quite sure what those concepts mean exactly. It would be awesome if you guys would explain all such concepts with a real word document (maybe by highlighting certain parts and saying "this is a paragragh" or "the difference between Paragraphs and Ranges is ...").
Personally it would help me a lot - and I wouldn't need to ask so many questions ;)

Anyways: thank you again!! I am probably
Ingmar

softwarea
 
Posts: 8
Joined: Sun Apr 17, 2016 1:59 pm

Thu Aug 26, 2021 10:21 am

Hello,

Thanks for your feedback.
Yes, one paragraph can be composed of textrange, image and other elements. We will consider your suggestion to improve the introduction of Word objects in the future. Thanks again for your kind suggestion.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc