使用Open xml sdk将文本替换为word文档中的图像 [英] Replace a text with an image in a word document using Open xml sdk

查看:62
本文介绍了使用Open xml sdk将文本替换为word文档中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Open Xml sdk用word文档中的图像替换文本。我的代码错误地出现在这一行:  DocumentFormat.OpenXml.Office.Drawing.Drawing元素

I am trying to replace a text with an image in a word document using Open Xml sdk. My code is erroring out on this line: DocumentFormat.OpenXml.Office.Drawing.Drawing element

 using (WordprocessingDocument myDoc =  WordprocessingDocument.Open("C:\\Users\\Desktop\\TestPdfs\\ModifiedDocs\\Test1.docx", true))
            {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;
                DocumentFormat.OpenXml.Wordprocessing.PageNumber test = null ;

                Text textPlaceHolder = myDoc.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "##Signature##").First();

                if (textPlaceHolder == null)
                {
                    Console.WriteLine("Text holder not found!");
                }
                else
                {
                    var parent = textPlaceHolder.Parent;

                    try
                    {
                        DocumentFormat.OpenXml.Office.Drawing.Drawing element = new DocumentFormat.OpenXml.Office.Drawing.Drawing("C:\\Users\\Desktop\\TestPdfs\\Signature1.png");

                        // Insert image (the image created with your function) after text place holder.        
                        textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Office.Drawing.Drawing>(element, textPlaceHolder);
                        // Remove text place holder.
                        textPlaceHolder.Remove();
                    }
                    catch(Exception ex)
                    {
                        Console.Write(ex);
                    }                       
                    
                }

            }

推荐答案

您好,

绘图 元素应该在DocumentFormat.OpenXml.Wordprocessing中。

Drawing element should be in DocumentFormat.OpenXml.Wordprocessing instead.

我建议您下载Open XML SDK Tool并创建两个文档。一个是带文字的原始文件。另一个是您的目标文件。然后,您可以使用该工具比较两个文件并生成代码。

I suggest you download Open XML SDK Tool and create two documents. One is the original document with text. The other is your target document. Then you could use the tool to compare the two files and generate code.

例如 

using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using Wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
using A14 = DocumentFormat.OpenXml.Office2010.Drawing;

namespace GeneratedCode
{
    public class GeneratedClass
    {
        public  void ChangeMainDocumentPart(MainDocumentPart mainDocumentPart1)
        {
            Document document1 = mainDocumentPart1.Document;

            Body body1=document1.GetFirstChild<Body>();

            Paragraph paragraph1=body1.GetFirstChild<Paragraph>();
            SectionProperties sectionProperties1=body1.GetFirstChild<SectionProperties>();

            Paragraph paragraph2 = new Paragraph(){ RsidParagraphMarkRevision = "00A76B44", RsidParagraphAddition = "00E42D53", RsidParagraphProperties = "00A76B44", RsidRunAdditionDefault = "00A76B44" };
            BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };

            Run run1 = new Run();

            RunProperties runProperties1 = new RunProperties();
            NoProof noProof1 = new NoProof();

            runProperties1.Append(noProof1);

            Drawing drawing1 = new Drawing();

            Wp.Inline inline1 = new Wp.Inline(){ DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U };
            Wp.Extent extent1 = new Wp.Extent(){ Cx = 5943600L, Cy = 3714750L };
            Wp.EffectExtent effectExtent1 = new Wp.EffectExtent(){ LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L };
            Wp.DocProperties docProperties1 = new Wp.DocProperties(){ Id = (UInt32Value)1U, Name = "Picture 1" };

            Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();

            A.GraphicFrameLocks graphicFrameLocks1 = new A.GraphicFrameLocks(){ NoChangeAspect = true };
            graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

            nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);

            A.Graphic graphic1 = new A.Graphic();
            graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");

            A.GraphicData graphicData1 = new A.GraphicData(){ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };

            Pic.Picture picture1 = new Pic.Picture();
            picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");

            Pic.NonVisualPictureProperties nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
            Pic.NonVisualDrawingProperties nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties(){ Id = (UInt32Value)1U, Name = "test.jpg" };
            Pic.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();

            nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
            nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);

            Pic.BlipFill blipFill1 = new Pic.BlipFill();

            A.Blip blip1 = new A.Blip(){ Embed = "rId4", CompressionState = A.BlipCompressionValues.Print };

            A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();

            A.BlipExtension blipExtension1 = new A.BlipExtension(){ Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" };

            A14.UseLocalDpi useLocalDpi1 = new A14.UseLocalDpi(){ Val = false };
            useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");

            blipExtension1.Append(useLocalDpi1);

            blipExtensionList1.Append(blipExtension1);

            blip1.Append(blipExtensionList1);

            A.Stretch stretch1 = new A.Stretch();
            A.FillRectangle fillRectangle1 = new A.FillRectangle();

            stretch1.Append(fillRectangle1);

            blipFill1.Append(blip1);
            blipFill1.Append(stretch1);

            Pic.ShapeProperties shapeProperties1 = new Pic.ShapeProperties();

            A.Transform2D transform2D1 = new A.Transform2D();
            A.Offset offset1 = new A.Offset(){ X = 0L, Y = 0L };
            A.Extents extents1 = new A.Extents(){ Cx = 5943600L, Cy = 3714750L };

            transform2D1.Append(offset1);
            transform2D1.Append(extents1);

            A.PresetGeometry presetGeometry1 = new A.PresetGeometry(){ Preset = A.ShapeTypeValues.Rectangle };
            A.AdjustValueList adjustValueList1 = new A.AdjustValueList();

            presetGeometry1.Append(adjustValueList1);

            shapeProperties1.Append(transform2D1);
            shapeProperties1.Append(presetGeometry1);

            picture1.Append(nonVisualPictureProperties1);
            picture1.Append(blipFill1);
            picture1.Append(shapeProperties1);

            graphicData1.Append(picture1);

            graphic1.Append(graphicData1);

            inline1.Append(extent1);
            inline1.Append(effectExtent1);
            inline1.Append(docProperties1);
            inline1.Append(nonVisualGraphicFrameDrawingProperties1);
            inline1.Append(graphic1);

            drawing1.Append(inline1);

            run1.Append(runProperties1);
            run1.Append(drawing1);
            BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };

            paragraph2.Append(bookmarkStart1);
            paragraph2.Append(run1);
            paragraph2.Append(bookmarkEnd1);
            body1.InsertBefore(paragraph2,paragraph1);

            paragraph1.Remove();
            sectionProperties1.RsidR = "00E42D53";
            sectionProperties1.RsidRPr = "00A76B44";
        }


    }
}

问候,

Celeste


这篇关于使用Open xml sdk将文本替换为word文档中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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