Incorporate altchunks into docx document

Home Forums WordprocessingML Incorporate altchunks into docx document

This topic contains 2 replies, has 2 voices, and was last updated by  Anonymous 3 years, 6 months ago.

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

    Chunk
    Participant

    Hi,

    Importing large number of AltChunks into docx, makes Word cannot open it. When I add about 30 AltChunks it’s fine, but a couple of more makes it impossible to open it by Word. I’m importing about 150 AltChunks – htmls containing images encoded with Base64.
    It works when I add a batch of AltChunks then open docx and save with Word and then add another batch of AltChunks and save, and so on… Then afchunkxxx.htm are rearranged as media/imagexxx.png. Some text from AltChunk is then incorporated into document.xml and AltChunk references/nodes are changed to references/nodes from the catalog “Media”.
    That “solution” is time consuming. Is there any way to do such a thing programatically? Some OpenXML trick? “OpenXmlPackage.saveToFlatOpc()” does not work. Opening the file and saving as a new file keeps AltChunks and is not creating images in “Media” catalog. Interop fails the same way Word fails. I’d be very thankful for any solution.

    I’ve found very similar issue on StackOverflow:
    https://stackoverflow.com/questions/10067669/merge-altchunk-content-into-docx-source
    That problem seems to be very old and there’s no programmatic solution.

    Thank you in advance,
    Richard

    • This topic was modified 3 years, 11 months ago by  Chunk. Reason: Corrected spelling and added detailed info on how I'm dealing with that issue right now
    #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

    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 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.