How to modify DocumentBuilder03 to both sides

Home Forums Open-Xml-PowerTools How to modify DocumentBuilder03 to both sides

This topic contains 1 reply, has 2 voices, and was last updated by  Eric White 7 years, 11 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #4011

    rogersb
    Participant
     WmlDocument doc1 = new WmlDocument(@"..\..\Template.docx");
            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(doc1.DocumentByteArray, 0, doc1.DocumentByteArray.Length);
                using (WordprocessingDocument doc = WordprocessingDocument.Open(mem, true))
                {
                    XDocument xDoc = doc.MainDocumentPart.GetXDocument();
                    XElement frontMatterPara = xDoc.Root.Descendants(W.txbxContent).Elements(W.p).FirstOrDefault();
                    frontMatterPara.ReplaceWith(
                        new XElement(PtOpenXml.Insert,
                            new XAttribute("Id", "Front")));
                    XElement tbl = xDoc.Root.Element(W.body).Elements(W.tbl).FirstOrDefault();
                    XElement firstCell = tbl.Descendants(W.tr).First().Descendants(W.p).First();
                    firstCell.ReplaceWith(
                        new XElement(PtOpenXml.Insert,
                            new XAttribute("Id", "Liz")));
                    XElement secondCell = tbl.Descendants(W.tr).Skip(1).First().Descendants(W.p).First();
                    secondCell.ReplaceWith(
                        new XElement(PtOpenXml.Insert,
                            new XAttribute("Id", "Eric")));
                    doc.MainDocumentPart.PutXDocument();
                }
                doc1.DocumentByteArray = mem.ToArray();
            }
    
            string outFileName = Path.Combine(tempDi.FullName, "Out.docx");
            List<Source> sources = new List<Source>()
                {
                    new Source(doc1, true),
                    new Source(new WmlDocument(@"..\..\Insert-01.docx"), "Liz"),
                    new Source(new WmlDocument(@"..\..\Insert-02.docx"), "Eric"),
                    new Source(new WmlDocument(@"..\..\FrontMatter.docx"), "Front"),
                };
            DocumentBuilder.BuildDocument(sources, outFileName);
        }

    Here the template doc becomes a wordprecessingdoc then searched for points to insert content into and these are tagged with ID’s Front, Liz, Eric. It then lets you build a List of type source where you can add multiple docx files and assign then the ID.

    But I have a need to apply selectivity to the SOURCE side too. Instead of open and insert the whole document, it should have logic to search the newly opened document for the section needed, in my case a table and then a barchart.

    There are 100 documents all generated from another program, so its the same table, same barchart, the rId and location are identical. Am hoping to open them all, then get those 2 items and add it into the new document.

    Has this been done? What is needed?
    thanks

    #4018

    Eric White
    Keymaster

    The correct approach is to first apply DocumentBuilder to your source document, extracting the desired content. DocumentBuilder works at the block-level, not run-level, so you can only extract complete paragraphs and tables from the source document – this is a built-in limitation to DocumentBuilder.

    You can do this in memory, of course.

    Then insert your extracted content at the correct location in the template document, as appropriate.

    I don’t have an examples that show directly how to do what you want to do, but this is all certainly doable – extract the content into a new WmlDocument, and then insert that content using the approach shown in the example that you are looking at.

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

You must be logged in to reply to this topic.