none
Merge Documents alignment issue & font issue RRS feed

  • Question

  • Hi ,

            I am merging the two documents using open xml. In Appended document alignment and font are different when compared to original document. Is there is any solution to retain the font and alignment?

     

    Thanks & Regards

    Jayakumar.C

     

    Wednesday, May 14, 2014 5:04 AM

Answers

  • Hi Jayakumar,

    Please follow this blog to merge two Word documents using Open XML SDK.

    How to Use altChunk for Document Assembly

    I have tested the sample code:

    XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
            XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
    
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(@"D:\111.docx", true))
            {
                string altChunkId = "AltChunkId1";
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                  "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
                  altChunkId);
                using (FileStream fileStream =
                    File.Open(@"D:\Doc3.docx", FileMode.Open))
                        chunk.FeedData(fileStream);
                XElement altChunk = new XElement(w + "altChunk",
                    new XAttribute(r + "id", altChunkId)
                );
                XDocument mainDocumentXDoc = GetXDocument(myDoc);
                // Add the altChunk element after the last paragraph.
                mainDocumentXDoc.Root
                    .Element(w + "body")
                    .Elements(w + "p")
                    .Last()
                    .AddAfterSelf(altChunk);
                SaveXDocument(myDoc, mainDocumentXDoc);
            }
    

    It works fine for me and alignment and font are same.


    If I have any misunderstanding, please let me know.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 15, 2014 5:33 AM
    Moderator

All replies

  • Please provide more information on HOW (the code) you're "merging the two documents using open xml".

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, May 14, 2014 2:46 PM
    Moderator
  • Hi Jayakumar,

    Please follow this blog to merge two Word documents using Open XML SDK.

    How to Use altChunk for Document Assembly

    I have tested the sample code:

    XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
            XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
    
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(@"D:\111.docx", true))
            {
                string altChunkId = "AltChunkId1";
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                  "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
                  altChunkId);
                using (FileStream fileStream =
                    File.Open(@"D:\Doc3.docx", FileMode.Open))
                        chunk.FeedData(fileStream);
                XElement altChunk = new XElement(w + "altChunk",
                    new XAttribute(r + "id", altChunkId)
                );
                XDocument mainDocumentXDoc = GetXDocument(myDoc);
                // Add the altChunk element after the last paragraph.
                mainDocumentXDoc.Root
                    .Element(w + "body")
                    .Elements(w + "p")
                    .Last()
                    .AddAfterSelf(altChunk);
                SaveXDocument(myDoc, mainDocumentXDoc);
            }
    

    It works fine for me and alignment and font are same.


    If I have any misunderstanding, please let me know.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 15, 2014 5:33 AM
    Moderator