繁体   English   中英

使用OpenXML创建包含与Word 2007兼容的图像的.DOCX

[英]Use OpenXML to create .DOCX containing an image that works with Word 2007

我正在尝试将OpenXML与C#一起使用来创建DOCX Word文档。 文档上的文本和格式效果很好。 但是,当我将图像添加到文档时,Word 2007将不会打开文档。 详细的错误是“未指定的错误”。

下面是演示该错误的程序。 生成的.DOCX在Word 2010中可以很好地打开,但是在Word 2007中不能。我已经尝试了SDK的2.0和2.5版本。 任何想法将不胜感激。

using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;

namespace ImageTest2007a
{
    class Program
    {
        static void Main(string[] args)
        {
            string imagefilename = @"c:\work\temp\ANI.jpg";

            // Create the Word document
            using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(@"C:\temp\t1.docx", WordprocessingDocumentType.Document))
            {
                // Create the "Main Part" of the document.  Not really used much externally here.
                MainDocumentPart theDoc = wordDocument.AddMainDocumentPart(); // Not directly referenced as 'theDOC'.  The reference wordDocument.MainDocumentPart is used.
                theDoc.Document = new Document();

                // Create and attach the body to the document.  The body of the document is where the document content is placed.
                Body theBody = theDoc.Document.AppendChild(new Body());  // Not directly referenced as 'body'.  The reference within wordDocument.MainDocumentPart.Document.Body is used.

                // attach the image

                ImagePart imagePart = theDoc.AddImagePart(ImagePartType.Png);

                using (FileStream stream = new FileStream(imagefilename, FileMode.Open))
                {
                    imagePart.FeedData(stream);
                }

                AddImageToBody(wordDocument, theDoc.GetIdOfPart(imagePart));
                theDoc.Document.Save();
            }
        }

        private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
        {
            // Define the reference of the image.
            var element =
                 new Drawing(
                     new DW.Inline(
                         new DW.Extent() { Cx = 990000L, Cy = 792000L },
                         new DW.EffectExtent()
                         {
                             LeftEdge = 0L,
                             TopEdge = 0L,
                             RightEdge = 0L,
                             BottomEdge = 0L
                         },
                         new DW.DocProperties()
                         {
                             Id = (UInt32Value)1U,
                             Name = "Picture 1"
                         },
                         new DW.NonVisualGraphicFrameDrawingProperties(
                             new A.GraphicFrameLocks() { NoChangeAspect = true }),
                         new A.Graphic(
                             new A.GraphicData(
                                 new PIC.Picture(
                                     new PIC.NonVisualPictureProperties(
                                         new PIC.NonVisualDrawingProperties()
                                         {
                                             Id = (UInt32Value)0U,
                                             Name = "New Bitmap Image.jpg"
                                         },
                                         new PIC.NonVisualPictureDrawingProperties()),
                                     new PIC.BlipFill(
                                         new A.Blip(
                                             new A.BlipExtensionList(
                                                 new A.BlipExtension()
                                                 {
                                                     Uri =
                                                        "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                                 })
                                         )
                                         {
                                             Embed = relationshipId,
                                             CompressionState =
                                             A.BlipCompressionValues.Print
                                         },
                                         new A.Stretch(
                                             new A.FillRectangle())),
                                     new PIC.ShapeProperties(
                                         new A.Transform2D(
                                             new A.Offset() { X = 0L, Y = 0L },
                                             new A.Extents() { Cx = 990000L, Cy = 792000L }),
                                         new A.PresetGeometry(
                                             new A.AdjustValueList()
                                         )
                                         { Preset = A.ShapeTypeValues.Rectangle }))
                             )
                             { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                     )
                     {
                         DistanceFromTop = (UInt32Value)0U,
                         DistanceFromBottom = (UInt32Value)0U,
                         DistanceFromLeft = (UInt32Value)0U,
                         DistanceFromRight = (UInt32Value)0U,
                         EditId = "50D07946"
                     });

            // Append the reference to body, the element should be in a Run.
            wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
        }



    }
}

我以前创建的文档(在Word 2007中创建,后来在代码中插入图片)遇到了类似的问题。 我不知道是否可以通过代码创建文档使其与Word 2007兼容。如果可以,那么问题可能出在图像插入上。

图像插入的问题是Word 2010的架构中存在的EditId属性,而Word 2007中不存在。如果在代码中注释此分配,则文档在Word 2007中打开时不会出现问题(并且图像显示正常) :

                 {
                     DistanceFromTop = (UInt32Value)0U,
                     DistanceFromBottom = (UInt32Value)0U,
                     DistanceFromLeft = (UInt32Value)0U,
                     DistanceFromRight = (UInt32Value)0U,
                     //EditId = "50D07946"
                 });

更新

我已经测试过为代码做所有事情(创建文档并插入图像),并且可以在Word 2007中打开而没有问题,因此,兼容性的问题肯定是EditId属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM