Removing PageBreaks

Is there a way to remove all PageBreaks right before saving the document?

Hi
Thanks for your request. You can use the following code to remove page breaks from document.

Document doc = new Document(@"252_98002_kimsokolow\in.doc");
// get runs of document
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
foreach (Run run in runs)
{
    if (run.Text.Contains("\f"))
    {
        // if run contains PageBreak then remove it
        run.Text = run.Text.Replace("\f", "");
    }
}
doc.Save(@"252_98002_kimsokolow\out.doc");

Also if pagebreak is SectionBreak New Page then you can use the following code.

Document doc = new Document(@"077_89509_skaal\in.doc");
ArrayList list = new ArrayList();
Section firstSection = doc.FirstSection;
foreach (Section section in doc.Sections)
{
    if (section.PageSetup.SectionStart == SectionStart.NewPage && section != firstSection)
    {
        list.Add(section);
        firstSection.AppendContent(section);
    }
    else
    {
        firstSection = section;
    }
}
foreach (Section section in list)
{
    doc.RemoveChild(section);
}
doc.Save(@"077_89509_skaal\out.doc");

I hope that it will help you.
Best regards.

Thanks so much for your response. I wonder if you could help me translate it to VB? I guess I should have specified that earlier.

Hi
Here is code in VB.

Dim doc As Document = New Document("in.doc")
'get runs of document
Dim runs As NodeCollection = doc.GetChildNodes(NodeType.Run, True)
Dim run As Run = Nothing
For Each run In runs
If run.Text.Contains("\f") Then
'if run contains PageBreak then remove it
run.Text = run.Text.Replace("\f", "")
End If
Next
doc.Save("out.doc")
Second code snippet.
Dim doc As Document = New Document("in.doc")
Dim list As System.Collections.ArrayList = New System.Collections.ArrayList()
Dim firstSection As Section = doc.FirstSection
Dim section As Section = Nothing
For Each section In doc.Sections
If section.PageSetup.SectionStart = SectionStart.NewPage And Not section = firstSection Then
list.Add(section)
firstSection.AppendContent(section)
Else
firstSection = section
End If
Next
section = Nothing
For Each section In list
doc.RemoveChild(section)
Next
doc.Save("out.doc")

Best regards.

Thanks for responding so quickly.
I tried the first snippet; it produced no errors, but it did not work for me.
The second snippet is producing the following error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC31080: Operator '=' is not defined for types 'Aspose.Words.Section' and 'Aspose.Words.Section'. Use 'Is' operator to compare two reference types.
Source Error:
Line 114:For Each section In doc.Sections
Line 115:
Line 116: If section.PageSetup.SectionStart = SectionStart.NewPage And Not section = firstSection Then
Line 117:
Line 118: list.Add(section)

If I do what it says and change the = to Is, I get the following error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30020: 'Is' requires operands that have reference types, but this operand has the value type 'Aspose.Words.SectionStart'.
Source Error:
Line 114:For Each section In doc.Sections
Line 115:
Line 116: If section.PageSetup.SectionStart Is SectionStart.NewPage And Not section = firstSection Then
Line 117:
Line 118: list.Add(section)

Hi
Sorry for this. Try to use the following code.

If (section.PageSetup.SectionStart = SectionStart.NewPage) And (Not section.Equals(firstSection)) Then

Best regards.

Thank you. That worked perfectly. So glad I renewed my license for a second year…I am very impressed with support.