Tuesday, August 12, 2014

Merge word documents using OPEN XML SDK

Merge word documents using OPEN XML SDK

  public XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";

        public XNamespace r = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
#region Merge documents

        private void MergeDoc(string MainDoc, string Annexure)
        {
            try
            {
                AppendPageBreak(Annexure);
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(MainDoc, true))
                {
                    string altChunkId = GetUniqueXmlItemID();
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                        AlternativeFormatImportPartType.WordprocessingML,
                        //"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml",
                      altChunkId);
                    using (FileStream fileStream =
                        File.Open(Annexure, 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);
                }
            }
            catch (Exception ex)
            {
                ULSLogService.DisplayError(this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name, ex);
            }
        }
        private void AppendPageBreak(string sourceDoc)
        {
            try
            {
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(sourceDoc, true))
                {
                    RevisionAccepter.AcceptRevisions(myDoc);
                    //   AppendPageBreak(myDoc);
                    //
                    Body body = myDoc.MainDocumentPart.Document.Body;
                    Paragraph para = new Paragraph();
                    Run run = para.AppendChild(new Run());
                    run.AppendChild(new Text(""));
                    if (para.ParagraphProperties == null)
                    {
                        para.ParagraphProperties = new ParagraphProperties();
                    }

                    para.ParagraphProperties.PageBreakBefore = new PageBreakBefore();
                    body.InsertBeforeSelf(para);
                    //body.AppendChild(p);
                    //
                }
            }
            catch (Exception ex)
            {
                ULSLogService.DisplayError(this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name, ex);
            }
        }
        private void SaveXDocument(WordprocessingDocument myDoc,
       XDocument mainDocumentXDoc)
        {
            try
            {
                // Serialize the XDocument back into the part
                using (Stream str = myDoc.MainDocumentPart.GetStream(
                    FileMode.Create, FileAccess.Write))
                using (XmlWriter xw = XmlWriter.Create(str))
                    mainDocumentXDoc.Save(xw);
            }
            catch (Exception ex)
            {
                ULSLogService.DisplayError(this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name, ex);
            }
        }
        private XDocument GetXDocument(WordprocessingDocument myDoc)
        {
            XDocument mainDocumentXDoc = null;
            try
            {
                // Load the main document part into an XDocument          
                using (Stream str = myDoc.MainDocumentPart.GetStream())
                using (XmlReader xr = XmlReader.Create(str))
                    mainDocumentXDoc = XDocument.Load(xr);
            }
            catch (Exception ex)
            {
                ULSLogService.DisplayError(this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name, ex);
            }
            return mainDocumentXDoc;

        }

        #endregion

No comments:

Post a Comment