在Word Open SDK + dotnet核心中添加图像

问题描述

我正在尝试使用dotnet core和Open SDK库将图像添加到word文档中。遵循与MSDN文章完全相同的指示。该代码运行正常,但是在尝试打开文档时却说该文档已损坏。

Microsoft Doc

Error while Opening

使用OpenSDK生产力工具并验证了文档。显示错误

enter image description here

已确认图像类型相同。无法理解出了什么问题。

    private const string RefrenceKeyWord = "rId";
    static void Main(string[] args)
    {
        InsertAPicture(document,fileName);
    }

    public static void InsertAPicture(string document,string fileName)
    {
        Int64Value imageWidth = 5731510L;
        Int64Value imageHeight = 3820795L;
        //using (var image = new Bitmap(fileName))
        //{
        //    imageWidth = image.GetWidthInEMUs() / 10;
        //    imageHeight = image.GetHeightInEMUs() / 10;
        //}

        using (WordprocessingDocument wordprocessingDocument =
            WordprocessingDocument.Open(document,true))
        {
            MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
            
            ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);

            using (FileStream stream = new FileStream(fileName,FileMode.Open))
            {
                imagePart.FeedData(stream);
            }
            //## start of new Code ##
            var maxId = mainPart.Parts
                .Where(p => p.RelationshipId.StartsWith(RefrenceKeyWord))
                .Select(p => 
                Convert.ToInt32(p.RelationshipId.Replace(RefrenceKeyWord,"")))
                .Max();

            mainPart.ChangeIdOfPart(imagePart,$"{RefrenceKeyWord}{maxId + 1}");
            //## end of new Code ##
            AddImagetoBody(wordprocessingDocument,mainPart.GetIdOfPart(imagePart),imageWidth,imageHeight);
        }
    }

    private static void AddImagetoBody(WordprocessingDocument wordDoc,string relationshipId,Int64Value width,Int64Value height)
    {

        // Define the reference of the image.
        var element =
             new Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = width,Cy = height },new DW.EffectExtent()
                     {
                         LeftEdge = 0L,TopEdge = 0L,RightEdge = 2540L,BottomEdge = 8255L
                     },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 = "Picture 1"
                                     },new PIC.NonVisualPictureDrawingProperties(new A.PictureLocks() { NoChangeAspect = true,NoChangeArrowheads = true })),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 = width,Cy = height }),new A.PresetGeometry(
                                         new A.AdjustValueList()
                                     )
                                     { Preset = A.ShapeTypeValues.Rectangle }))
                         )
                         { Uri = "https://schemas.openxmlformats.org/drawingml/2006/picture" })
                 )
                 {
                     distanceFromTop = (UInt32Value)0U,distanceFromBottom = (UInt32Value)0U,distanceFromLeft = (UInt32Value)0U,distanceFromright = (UInt32Value)0U,EditId = "50D07946"
                 });

        SectionProperties sectPr = (SectionProperties)wordDoc.MainDocumentPart.Document.Body.ChildElements.Last();

        // var p = wordDoc.MainDocumentPart.RootElement;
        // var p1 = wordDoc.MainDocumentPart.Document.Body.FirstChild;

        var p1 =  new Paragaph(new Run(element));
        // Append the reference to body,the element should be in a Run.
        wordDoc.MainDocumentPart.Document.Body.InsertBefore(p1,sectPr);
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)