使用SDK 2.0包含.docx文件时Altchunks的问题 [英] Problem with Altchunks when including .docx file using SDK 2.0

查看:142
本文介绍了使用SDK 2.0包含.docx文件时Altchunks的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用OpenXML SDK 2.0使用altchunks将两个.docx文档合并在一起。我的第一个文档包含一个或多个书签,我想将第二个文档插入书签后的第一个文档。这似乎不是一个非常复杂的任务,但是当我在生成的文档上运行验证器时,我总是会收到以下错误:

错误1
描述:关系'http://由属性'id'引用的schemas.openxmlformats.org/officeDocument/2006
/ relationships / aFChunk'的类型不正确。它的类型应该是'http://schemas.openxmlformats.org/officeDocument/2006/afChunk'。
路径:/ w:document [1] / w:body [1] / w: altChunk [1]
部分:/word/document.xml
------------------------------ -------------

任何人都可以帮忙吗?以下是我的测试代码:


Hello,

I am trying to use the OpenXML SDK 2.0 to merge two .docx documents together using altchunks.  My first document contains one or more bookmarks, I would like to insert the second document into the first right after the bookmark.  This doesn't seem like a very complicated task, but when I run the validator on the generated document I always get the following error:

Error 1
Description: Relationship 'http://schemas.openxmlformats.org/officeDocument/2006
/relationships/aFChunk' referenced by attribute 'id' has incorrect type. Its typ
e should be 'http://schemas.openxmlformats.org/officeDocument/2006/afChunk'.
Path: /w:document[1]/w:body[1]/w:altChunk[1]
Part: /word/document.xml
-------------------------------------------

Can anyone help?  Below is my test code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace Test_Word_Console_App
{
    class Program
    {
        static void Main(string[] args)
        {
            string docxTemplateFile = "C:\\Users\\fjaggi\\Desktop\\tmp\\a.docx";
            string docxObservationFile = "C:\\Users\\fjaggi\\Desktop\\tmp\\b.docx";
            string docxOutputFile = "C:\\Users\\fjaggi\\Desktop\\tmp\\c.docx";
            string sBmkId;
            int iChunkId = 1;

            File.Copy(docxTemplateFile, docxOutputFile, true);

            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(docxOutputFile, true))
            {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                var foo = mainPart.Document.Descendants().Where(s => s.Name.ToString().StartsWith("Observations_"));

                foreach (BookmarkStart bs in foo)
                {
                    sBmkId = bs.Id;

                    // might be a better way to do this
                    var bar = mainPart.Document.Descendants().Where(s => s.Id.ToString().Equals(sBmkId));

                    BookmarkEnd be = bar.First();

                    var parent = be.Parent;

                    string sChunkId = "AltChunkId" + iChunkId.ToString();
                    iChunkId++;

                    AlternativeFormatImportPart chunk = null;
                    chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, sChunkId);
                    using (FileStream fileStream = File.Open(docxObservationFile, FileMode.Open))
                    {
                        chunk.FeedData(fileStream);
                        AltChunk altChunk = new AltChunk();
                        altChunk.Id = sChunkId;
                        parent.InsertAfterSelf(altChunk);
                    }
                }
                mainPart.Document.Save();
                myDoc.Close();
            }

            try
            {
                OpenXmlValidator validator = new OpenXmlValidator();
                int count = 0;
                foreach (ValidationErrorInfo error in validator.Validate(WordprocessingDocument.Open(docxOutputFile, true)))
                {
                    count++;
                    Console.WriteLine("Error " + count);
                    Console.WriteLine("Description: " + error.Description);
                    Console.WriteLine("Path: " + error.Path.XPath);
                    Console.WriteLine("Part: " + error.Part.Uri);
                    Console.WriteLine("-------------------------------------------");
                }
                if (count == 0)
                    Console.WriteLine("IT WORKED!");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }

        }
    }
}

推荐答案

可以忽略该验证错误,因为Word能够处理aFChunk或afChunk。运行代码并打开文档时会发生什么? Word是否适当地合并了altChunk?
That validation error can be ignored, since Word is able to handle either aFChunk or afChunk. What happens when you run your code and open your document? Does Word merge the altChunk appropriately?


这篇关于使用SDK 2.0包含.docx文件时Altchunks的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆