C#采用OpenXml給word里面插入圖片
本文實(shí)例講述了C#采用OpenXml給word里面插入圖片的方法,分享給大家供大家參考。具體分析如下:
首先需要指出的是在MSDN官網(wǎng)有完整的OpenXML教程,雖然是全英文的不過(guò)還是很有幫助的。
注,原來(lá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)文章
無(wú)法從 int? 轉(zhuǎn)換為 int 運(yùn)行時(shí)出現(xiàn)錯(cuò)誤
無(wú)法從"int?"轉(zhuǎn)換為"int" ,在運(yùn)行時(shí)會(huì)出現(xiàn)錯(cuò)誤,通過(guò)強(qiáng)制類型轉(zhuǎn)換(int)便可解決2014-05-05
C#中動(dòng)態(tài)數(shù)組用法實(shí)例
這篇文章主要介紹了C#中動(dòng)態(tài)數(shù)組用法,實(shí)例分析了C#中ArrayList實(shí)現(xiàn)動(dòng)態(tài)數(shù)組的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Unity UGUI的InputField輸入框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的InputField輸入框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
DevExpress實(shí)現(xiàn)GridControl刪除所有行的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl刪除所有行的方法,對(duì)于C#初學(xué)者有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08
C# Winform按鈕中圖片實(shí)現(xiàn)左圖右字的效果實(shí)例
這篇文章主要給大家介紹了關(guān)于C# Winform按鈕中圖片實(shí)現(xiàn)左圖右字效果的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
C#使用linq對(duì)數(shù)組進(jìn)行篩選排序的方法
這篇文章主要介紹了C#使用linq對(duì)數(shù)組進(jìn)行篩選排序的方法,實(shí)例分析了C#實(shí)用linq擴(kuò)展進(jìn)行數(shù)組排序的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C#實(shí)現(xiàn)多線程編程的簡(jiǎn)單案例
這篇文章介紹了C#實(shí)現(xiàn)多線程編程的簡(jiǎn)單案例,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

