Page break is getting inserted between sources

Home Forums Open-Xml-PowerTools Page break is getting inserted between sources

This topic contains 2 replies, has 2 voices, and was last updated by  Eric White 7 years, 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #4365

    sammyn
    Participant

    I’m trying to combine multiple documents. I need to preserve the formatting for sections.

    The code is pretty simple. Basically:

                Source s1 = new Source(doc1, true);
                sources.Add(s1);
    
                Source s2 = new Source(doc2, true);
                sources.Add(s2);
    
                var mergedDoc = DocumentBuilder.BuildDocument(sources);
    

    The problem is that there is a page break getting inserted between each section. Neither document contains a page break.

    How do I resolve this? I do NOT want the page break between sources, yet I need each source to preserve it’s sections.

    Thanks!

    Thanks!

    #4366

    sammyn
    Participant

    So, I found the code that is ‘inserting’ the page break, but, I don’t know docx well enough to understand what it is doing. any pointers? this is the code:

            private static void FixUpSectionProperties(WordprocessingDocument newDocument)
            {
                XDocument mainDocumentXDoc = newDocument.MainDocumentPart.GetXDocument();
                mainDocumentXDoc.Declaration.Standalone = "yes";
                mainDocumentXDoc.Declaration.Encoding = "UTF-8";
                XElement body = mainDocumentXDoc.Root.Element(W.body);
                var sectionPropertiesToMove = body
                    .Elements()
                    .Take(body.Elements().Count() - 1)
                    .Where(e => e.Name == W.sectPr)
                    .ToList();
                foreach (var s in sectionPropertiesToMove)
                {
                    var p = s.SiblingsBeforeSelfReverseDocumentOrder().First();
    
                    if (p.Element(W.pPr) == null)
                        p.AddFirst(new XElement(W.pPr));
                    p.Element(W.pPr).Add(s);
                }
                foreach (var s in sectionPropertiesToMove)
                    s.Remove();
            }
    
    #4368

    Eric White
    Keymaster

    Hi,

    This is not adding a page break, but a section break. When you keep sections, as your code does, it keeps the section for the document.

    You can use continuous sections for the document, which will keep the same formatting, but not include a page break. I am not sure of the details to change the section break for the entire document to be a ‘continuous’ section break, but it should be possible.

    If not possible, it would be easy to go into the document after the fact, and alter the section breaks to be ‘continuous’ section breaks. It is a matter of 20 lines of code to do this.

    In order to find out the changes you want to make, generate a document that includes the section breaks that force a page break, save it. Copy it, and alter the copy so that each section is a ‘continuous’ section break. Then use the Open XML SDK productivity tool to compare the two. This will identify the changes to the markup that you need to make.

    Then you can write code to make the same changes to your result document, after DocumentBuilder is done doing its thing.

    Look at screen-cast #13 in the following series:

    Introduction to Open XML

    -Eric

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.