OpenXml入门---word里面插入图片

下面介绍如何在word里面插入图片,顺便小弟发现MSDN官网有完整的OpenXML教程,虽然是全英文的不过还是很有帮助的。

Tips,原来摘抄代码里面没有模板,在copy过来发现插入word中的图片大小不一样,我们如何查找设置图片大小带代码的那一块,建议自己用在word里面插入一张图片,通过OpenXml Tools 反编译出C#代码,然后改变图片的大小,再次反编译。

使用byeond compare 【http://www.scootersoftware.com/ 下载地址】比较C#代码,就会发现是因为new DW.Extent() { Cx = 990000L, Cy = 792000L}  是因为这段设置照成的。 以后其实很多地方都可以借助OpenXml Tools反编译来进行对比。查看设置样式的属性位置。

MSDN openxml学习链接:  http://msdn.microsoft.com/en-us/library/office/bb491088(v=office.15).aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
using A = DocumentFormat.OpenXml.Drawing;

namespace AddPictureIntoWord
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string picPath = "u=639047729,3872612606&fm=11&gp=0.bmp";
            string filePath = "Test.docx";
            AddPictureIntoWord(filePath, picPath);
        }

        public static void AddPictureIntoWord(string filePath, string picturePath)
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(filePath, true))
            {
                string picType = picturePath.Split(‘.‘).Last();
                ImagePartType imagePartType;
                ImagePart imagePart = null;
                // 通过后缀名判断图片类型, true 表示忽视大小写
                if (Enum.TryParse<ImagePartType>(picType, true, out imagePartType))
                {
                    imagePart = doc.MainDocumentPart.AddImagePart(imagePartType);
                }

                imagePart.FeedData(File.Open(picturePath, FileMode.Open)); // 读取图片二进制流
                AddImageToBody(doc, doc.MainDocumentPart.GetIdOfPart(imagePart));
            }
        }

        // 摘抄自http://msdn.microsoft.com/EN-US/library/office/bb497430(v=office.15).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-5             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)));
        }

    }
}

时间: 2025-02-01 19:57:02

OpenXml入门---word里面插入图片的相关文章

Java利用poi生成word(包含插入图片,动态表格,行合并)

转: Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插入图片,动态表格,行合并) 测试模板样式: Word生成结果: 图表 2需要的jar包:(具体jar可自行去maven下载) 注意:需要严格按照上面版本下载jar包,否则可能出现jar包之间不能匹配的导致代码报错 各种 jar包都可以在这里下载: https://mvnrepository.com/ Tes

Word中插入图片,嵌入式,图片显示不能全部显示出来。

解决办法:原来,是由于图片所在的段落属性的行距设置成了“固定值”,设置不要为固定值即可. 1.选中你要插入图片的上下文,单击“右键”,选择“段落”修改行距,比如为修改“1.5倍行距””(实际上,只要不是“固定值”均可),然后确认. 2.再插入图片即可OK. 3.或者,有的图片已经插入到word中了.那就直接选中已经插入文中的图片,单击“右键”,选择“段落”修改行距,比如为修改“1.5倍行距”(实际上,只要不是“固定值”均可),然后确认就可以了.

导出word之插入图片

最近做了个项目,要根据数据库内容生成word,内容包括表格,图片,段落等等.其中图片碰到如下的问题: 如何使图片按照生成内容的顺序输出? 上网查相关资料,一般都是设置 var s = word.Application.Selection.InlineShapes.AddPicture(strPicPath, Nothing, Nothing, Nothing);ss.WrapFormat.Type = WdWrapType.wdWrapSquare; 那么这个wdWrapSquare代表的是什么

word中更改图片和标题之间的垂直距离

word中插入图片后,往往需要给图片加上标题. 你插入图片和给图片插入标题时,word用的是默认的格式给你插入的图片和标题. 假如原来的paragraph是2倍行距,你的图片和标题之间的距离也是2倍行距,这样显的你的图片和标题之间的距离特别大. 插入完成后,你想调节图片和标题的距离,就不可能了.因为行距是前一行和后一行直接的距离,但是图片并没有行距. 解决方法如下: 1.删除你的图片和图片标题. 2. 在你插入图片的上一行之下插入两个回车 3. 选中你插入的回车,邮件点击"段落",把行

C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)

Microsoft.Office.Interop.Word.Application wordApp = new ApplicationClass() word对象 2. Microsoft.Office.Interop.Word.Document wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing) 文档对象 3. Object Nothing = System.Reflection

c# 操作word中在右下角插入图片

需求:需要对现有文档在右下角插入图片 /// <summary> /// 将图片插入到word中 /// </summary> /// <param name="wordPath">被操作的源word文档</param> /// <param name="picturePath">要插入的图片地址</param> /// <param name="toWordPath"

office 2007 word 插入图片没有显示

office 2007 word 插入图片没有显示 插入后如下,在word中显示 然后我们选中照片,双击后,发现菜单栏就改变了,如下 然后选择点击 文字环绕就可以了,自己再调整.

word插入图片显示不完整的解决的方法

有时在编写word文档,插入图片时,会出现图不完整的情况. 解决方法是:选中图片,段落格式设置为单位行距(不是22磅),图片格式为嵌入式.问题解决.

C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容

序 在前面的文章C# 如何插入.修改.删除Word批注一文中介绍了如何操作Word批注的一些方法,在本篇文章中将继续介绍操作Word批注的方法.分以下三种情况来介绍: 插入图片到Word批注 读取Word批注 回复Word批注 所需工具 Spire.Doc for .NET 6.3(社区版) Visual Stuido 示例代码 1. 插入图片到Word批注 [C#] using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fiel