C#采用OpenXml给word里面插入图片
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)));
}
}
}
首先图片300*300对应的图片大小为:7.935cm*7.035cm,至于如何计算可以参考下面的计算换算规则:
然而Open xml中的度量单位为EMU(全称为 English Metric Unit),1cm =360000EMUS,所以7.935cm=7.935*360000=2857500, 更多内容你可以参考下面的文章介绍:http://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/
C#采用OpenXml给word里面插入图片的更多相关文章
- OpenXml入门---word里面插入图片
下面介绍如何在word里面插入图片,顺便小弟发现MSDN官网有完整的OpenXML教程,虽然是全英文的不过还是很有帮助的. Tips,原来摘抄代码里面没有模板,在copy过来发现插入word中的图片大 ...
- 在word里插入图片,并设置图片的格式
由于公司业务需要,需要在生成的word里插入图片(公司印章),仔细想了下,还是在word模板里添加一个书签,然后再该书签的位置插入图片,并设置图片的格式方便些: 代码如下: using System; ...
- 如何在Microsoft Word里面插入图片作为背景/封面?
Stay hungry, Stay foolish. 如何在Word里面插入图片作为背景?其实很简单,开门见山,我们只需要这几步即可! 1.第一步,打开要插入图片的Word 2.第二步,插入图 ...
- Java利用poi生成word(包含插入图片,动态表格,行合并)
转(小改): Java利用poi生成word(包含插入图片,动态表格,行合并) 2018年12月20日 09:06:51 wjw_11093010 阅读数:70 Java利用poi生成word(包含插 ...
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
调用word的com组件将400条数据导入word表格中耗时10分钟简直不能忍受,使用NPOI组件耗时4秒钟.但是NPOI中替换书签内容的功能不知道是不支持还是没找到. 辅助类 Excel表格数据与D ...
- C#中按模板操作Word —— 如何向Word中插入图片
一.Word对象模型的重叠性分析 本文主要介绍通过书签Bookmark向Word文档中插入图片的方法.在此之前我们先简单讨论下Word对象模型的重叠性.如果你对Word对象模型还不熟悉,请参考本专栏第 ...
- Aspose.Words进行Word替换(插入图片和水印)
由于最近一直在忙着做着Word打印模板的一些工作,就整理一些Asponse.Words对Word文档进行操作的资料. using System; using System.Collections.Ge ...
- C#操作word之插入图片
假如我们导出一份简历到word文档,那势必可能要同时导出我们包含的简历,下面就来试一下如何和通过C#代码,将图片插入到word文档中. 为了简便起见,就简单一点.类似下面这样的 姓名 张三 照片 ...
- Office WORD里插入图片,嵌入型只能显示一半怎么办
如下图所示,公式编辑器插入的图片如果用嵌入型只能显示一半,但是改成其他方式即可全部显示 选中有问题的段落,点击设置为单倍行距即可
随机推荐
- JQuery-常用小组件
常用的小组件记录 1. 单选框.复选框重置样式效果 参考: http://www.cnblogs.com/sanshi/p/4369375.html 三生石上 参考: http://www.jq22. ...
- Vrui 增加自定义事件
#include <Vrui/Application.h> #include <GL/GLObject.h> #include <Vrui/Vrui.h> #inc ...
- Fedora25
Fedora默认情况是没有装flash的,首先输入https://get.adobe.com/flashplayer/?loc=cn,选择.rpm包,点击立即下载,下载完成后进到download目录, ...
- Java 代码性能优化
代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...
- Webdriver常用的元素定位
Webdriver常用定位元素的方法有以下八种: id.name.class name.tag name.link text.partial link text.xpath.class selecto ...
- rune is alias of int32
I think chendesheng's quote gets at the root cause best: Go uses a lot of signed values, not just fo ...
- 前端好的工具集推荐 lodash
原来用 backbone 的时候, 感觉 underscore 是一个好的工具集,顿时感觉不错,不用自己一直去封装一些组件. 直到我遇到了 lodash,一只发现 web 项目中有依赖这个库的,但是一 ...
- 安装linux工作环境
1,介绍Vagrant 我们做web开发的时候经常要安装各种本地测试环境,比如apache,php,mysql,redis等等.出于个人使用习惯,可能我们还是比较习惯用windows.虽然说在wind ...
- java中异常处理机制的简单原理
以上是自认为的java异常处理的简单原理,如有不妥之处还请各位大神帮忙指点,谢谢!
- Debian系Linux的dpkg命令
dpkg "是"Debian Packager "的简写.为 "Debian" 专门开发的套件管理系统,方便软件的安装.更新及移除.所有源自" ...