.Net如何在word中插入图片
使用代码向word插入图片,首先要了解图片在word是以什么样的结构存在,我们一起来解析下Word中的图片结构。
图片在主文件中如何存在
在document.xml中文件以为如下图1-1所示节点,以xml节点属性显示图片。
图1-1
图片物理结构
Word的图片物理文件以什么的结构存在呢,图片存在word/media路径下。即word自身会保存word的物理文件。如下图1-2所示。
图片怎么和文档主题结构怎么关联
Word加载时,是不会寻找物理文件,而是寻找和主文件关联的关系文件(document.xml.rels),如下图1-3所示,这里的Id=””
Target="/media/image.jpg" 是指需要关联的目标路径。
Id="R791d0621ac094c72" 是图片的唯一主键,即和主键关联的主键,在文件中会有一个 Id= "R791d0621ac094c72"的节点属性。
主键节点属性为:
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:embed="R791d0621ac094c72" cstate="print">
这样就能将我物理路径和主文件关联到一起。
图1-3
Word加载时图片加载顺序为:document.xml →document.xml.rels→media
先加载document.xml的段落属性,加载连续文本,加载
代码向word插入图片
知道了word中的图片结构,就能按照word定义的规则插入图片。示例代码如下所示
string fileName = @"HelloWord.docx";
using (WordprocessingDocument wd = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
var mainDocx = wd.AddMainDocumentPart();
var docx = new DocumentFormat.OpenXml.Wordprocessing.Document();
mainDocx.Document = docx;
var body = mainDocx.Document.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Body());
var imgPathList = new List
imgPathList.Add(@"Test_1.jpg");//换成你自己的本地图片路径
imgPathList.Add(@"Test_2.jpg");//换成你自己的本地图片路径
imgPathList.Add(@"Test_3.jpg");//换成你自己的本地图片路径
foreach (var imgFileName in imgPathList)
{
ImagePart imagePart = mainDocx.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(imgFileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
#region 添加图片
var element =
new Drawing(
new DocumentFormat.OpenXml.Drawing.Wordprocessing.Inline(
new DocumentFormat.OpenXml.Drawing.Wordprocessing.Extent() { Cx = 3672000, Cy = 2134800 },
new DocumentFormat.OpenXml.Drawing.Wordprocessing.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture1",
Title = "投标图片"
},
new DocumentFormat.OpenXml.Drawing.Wordprocessing.NonVisualGraphicFrameDrawingProperties(
new DocumentFormat.OpenXml.Drawing.GraphicFrameLocks() { NoChangeAspect = true }),
new DocumentFormat.OpenXml.Drawing.Graphic(
new DocumentFormat.OpenXml.Drawing.GraphicData(
new DocumentFormat.OpenXml.Drawing.Pictures.Picture(
new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureProperties(
new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureDrawingProperties()),
new DocumentFormat.OpenXml.Drawing.Pictures.BlipFill(
new DocumentFormat.OpenXml.Drawing.Blip(
new DocumentFormat.OpenXml.Drawing.BlipExtensionList(
new DocumentFormat.OpenXml.Drawing.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = mainDocx.GetIdOfPart(imagePart),
CompressionState =
DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print
},
new DocumentFormat.OpenXml.Drawing.Stretch(
new DocumentFormat.OpenXml.Drawing.FillRectangle())),
new DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties(
new DocumentFormat.OpenXml.Drawing.Transform2D(
new DocumentFormat.OpenXml.Drawing.Offset() { X = 0L, Y = 0L },
new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 3672000, Cy = 2134800 }),
new DocumentFormat.OpenXml.Drawing.PresetGeometry(
new DocumentFormat.OpenXml.Drawing.AdjustValueList()
)
{ Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle }))
)
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
EditId = "50D07946"
});
body.AppendChild(new Paragraph(new Run(element)));
#endregion
}
mainDocx.Document.Save();
Process.Start(fileName);
}
生成效果如图1-4所示:
图1-4
备注:代码可以直接运行。