C#采用OpenXml給word里面插入圖片
本文實(shí)例講述了C#采用OpenXml給word里面插入圖片的方法,分享給大家供大家參考。具體分析如下:
首先需要指出的是在MSDN官網(wǎng)有完整的OpenXML教程,雖然是全英文的不過(guò)還是很有幫助的。
注,原來(lái)摘抄代碼里面沒(méi)有模板,在copy過(guò)來(lái)發(fā)現(xiàn)插入word中的圖片大小不一樣,我們?nèi)绾尾檎以O(shè)置圖片大小帶代碼的那一塊,建議自己用在word里面插入一張圖片,通過(guò)OpenXml Tools 反編譯出C#代碼,然后改變圖片的大小,再次反編譯。
使用byeond compare 【http://www.scootersoftware.com/ 下載地址】比較C#代碼,就會(huì)發(fā)現(xiàn)是因?yàn)閚ew DW.Extent() { Cx = 990000L, Cy = 792000L} 是因?yàn)檫@段設(shè)置造成的。以后其實(shí)很多地方都可以借助OpenXml Tools反編譯來(lái)進(jìn)行對(duì)比。查看設(shè)置樣式的屬性位置。
MSDN openxml學(xué)習(xí)鏈接: 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;
// 通過(guò)后綴名判斷圖片類型, true 表示忽視大小寫
if (Enum.TryParse<ImagePartType>(picType, true, out imagePartType))
{
imagePart = doc.MainDocumentPart.AddImagePart(imagePartType);
}
imagePart.FeedData(File.Open(picturePath, FileMode.Open)); // 讀取圖片二進(jìn)制流
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 }, // 調(diào)節(jié)圖片大小
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 }), //與上面的對(duì)準(zhǔn)
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)));
}
}
}
本文示例運(yùn)行效果如下圖所示:

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# 漢字轉(zhuǎn)化拼音的簡(jiǎn)單實(shí)例代碼
C# 漢字轉(zhuǎn)化拼音的簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考一下2013-04-04
C#使用EF連接PGSql數(shù)據(jù)庫(kù)的完整步驟
這篇文章主要給大家介紹了關(guān)于C#使用EF連接PGSql數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
使用C#實(shí)現(xiàn)在word中插入頁(yè)眉頁(yè)腳的方法
這篇文章主要介紹了使用C#實(shí)現(xiàn)在word中插入頁(yè)眉頁(yè)腳的方法,是操作Word的常見(jiàn)方法,有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08
C#中Invoke和BeginInvoke實(shí)際應(yīng)用詳解
這篇文章主要給大家介紹了關(guān)于C#中Invoke和BeginInvoke實(shí)際應(yīng)用的相關(guān)資料,Invoke是對(duì)象方法,BeginInvoke是靜態(tài)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
基于Unity制作一個(gè)簡(jiǎn)易的計(jì)算器
今天主要和大家分享如何使用Unity制作計(jì)算器,難度中等,可以用來(lái)學(xué)習(xí),或者當(dāng)成其他項(xiàng)目的小組件導(dǎo)入。當(dāng)然,也可以導(dǎo)出來(lái),發(fā)布到網(wǎng)頁(yè)端,來(lái)做一個(gè)嵌入式工具也可以。感興趣的可以跟隨小編學(xué)習(xí)一下2022-03-03

