Incorporate altchunks into docx document
Home › Forums › WordprocessingML › Incorporate altchunks into docx document
Tagged: AltChunk docx conversion images
- This topic has 2 replies, 2 voices, and was last updated 5 years, 4 months ago by
Anonymous.
-
AuthorPosts
-
May 27, 2020 at 8:09 pm #8687
Chunk
ParticipantHi,
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.
October 12, 2020 at 2:27 pm #9780Anonymous
InactiveOk, 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 insertedsources.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);
} -
AuthorPosts
- You must be logged in to reply to this topic.