Change to allow a partial importation of KeepSection

Feb 28, 2013 at 7:41 AM
Edited Feb 28, 2013 at 7:42 AM
Hello,

I have tried to merge some documents but by keeping the informations of the first document. So, when I added a source for the first document I had to add True to KeepSection. For the other documents as I needed to keep the header and footer of the first document I had to put False. The problem was that a custom margin was only allowed for the content of the first document and the other documents had lost the custom margins.
I have made the following change (in case someone is interested :
In the file
DocumentBuilder.cs
Function BuildDocument
In the end of the function there is

else
FixUpSectionProperties(output);
I have put the two lines in comment so the new code is

//else
            //    FixUpSectionProperties(output);
the end of the function looks like that

if (!sources.Any(s => s.KeepSections))
            {
                using (OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(sources[0].WmlDocument))
                using (WordprocessingDocument doc = streamDoc.GetWordprocessingDocument())
                {
                    var sectPr = doc.MainDocumentPart.GetXDocument().Root.Element(W.body)
                        .Elements().Last();
                    if (sectPr.Name == W.sectPr)
                    {
                        AddSectionAndDependencies(doc, output, sectPr, images);
                        output.MainDocumentPart.GetXDocument().Root.Element(W.body).Add(sectPr);
                    }
                }
            }
            //else
            //    FixUpSectionProperties(output);
        }
        foreach (var part in output.GetAllParts())
            if (part.Annotation<XDocument>() != null)
                part.PutXDocument();
    }
With this change I can keep the header / footer / margins of the first documents without losing parameters from the other ones.

Marc