Incorporate altchunks into docx document

Home Forums WordprocessingML Incorporate altchunks into docx document

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #8687
    Chunk
    Participant

    Hi,

    Actually I’ve found semi-solution with the use of Interop. I was able to open, tidy and update TOC for that document with the following code:

    var wordApplication = new Application();
    var document = wordApplication.Documents.Open(FileName: filePath, OpenAndRepair: true);

    document.TablesOfContents[1].Update();
    document.SaveAs(filePath);

    document.Close();
    wordApplication.Quit();

    Anyway if there’s some neat way to do it without the Interop I’d still be grateful.

    #9780
    Anonymous
    Inactive

    Ok, so I managed to come up with a solution. In order to insert a document at a certain position I split the original document into two sources for the DocumentBuilder, then I created a source from the document to be inserted. In the end I built a new document with these 3 sources and it seems to be working just fine.

    I am looking for the paragraph to split the original document by a placeholder, for example “@@insert@@”.

    Bellow is the code if anyone needs it.

    var paragraph = DestinationDocument.MainDocumentPart.Document.Descendants<OpenXmlParagraph>().FirstOrDefault(item => item.InnerText.Contains(placeHolder));

    if (paragraph != null)
    {
    var idOfParagraph =
    DestinationDocument.MainDocumentPart.Document.Descendants<OpenXmlParagraph>()
    .ToList()
    .IndexOf(paragraph);

    //save and close current destination document
    SaveChanges(destinationFilePath, false);

    var sources = new List<Source>();

    var originalDocument = new WmlDocument(destinationFilePath);

    sources.Add(new Source(originalDocument, 0, idOfParagraph, true)); // add first part of initial document

    var documentToBeInserted = new WmlDocument(docFilePath);
    sources.Add(new Source(documentToBeInserted, true)); // add document to be inserted

    sources.Add(new Source(originalDocument, idOfParagraph + 1, true)); // add rest of initial document

    var newDestinationDocument = DocumentBuilder.BuildDocument(sources); // build new document
    newDestinationDocument.SaveAs(destinationFilePath); // save

    // re-open destination document
    DestinationDocument = WordprocessingDocument.Open(Path.GetFullPath(destinationFilePath), true);
    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.