C# 设置Word文档中图片的大小
在創(chuàng)建Word文檔時,我們經(jīng)常需要向文檔中插入圖片,但插入圖片的大小有時候可能會太大或太小,這時候我們就需要對圖片的大小進(jìn)行調(diào)整,使得圖片與文章更加協(xié)調(diào)、美觀。這篇文章將介紹如何使用Free Spire.Doc組件和C#在Word文檔中對新添加的圖片和已有的圖片進(jìn)行大小設(shè)置。
在使用以下代碼前需要創(chuàng)建一個C#應(yīng)用程序并引用Spire.Doc.dll到工程中。
對新添加的圖片進(jìn)行大小設(shè)置
//創(chuàng)建Document實例
Document document = new Document();
?
//添加節(jié)和段落
Section s = document.AddSection();
Paragraph p = s.AddParagraph();
?
//添加圖片到段落
DocPicture Pic = p.AppendPicture(Image.FromFile(@"MickeyMouse.jpg"));
?
picture.TextWrappingStyle = TextWrappingStyle.Square;?
picture.HorizontalPosition = 180f;
picture.VerticalPosition = 60f;
?
//設(shè)置圖片的大小
Pic.Width = 120f;
Pic.Height = 170f;
?
//保存文檔
document.SaveToFile("Image.docx", FileFormat.Docx);
效果圖:
對已有的圖片進(jìn)行大小設(shè)置
//加載Word文檔
Document document = new Document("Image.docx");
?
//獲取第一個節(jié)
Section section = document.Sections[0];
//獲取第一個段落
Paragraph paragraph = section.Paragraphs[0];
?
//調(diào)整段落中圖片的大小?
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
? ? if (docObj is DocPicture)
? ? {
? ? ? ? DocPicture picture = docObj as DocPicture;
? ? ? ? picture.Width = 50f;
? ? ? ? picture.Height = 50f;
? ? }
}
?
//保存文檔?
document.SaveToFile("ResizeImages.docx");
效果圖:
C#操作Word對象:
指定位置??:? 設(shè)置書簽。
主要代碼:
Word.Application oWord;
?Word.Document oDoc;
object name= "d:\\myfile.doc";
??object Range=System.Reflection.Missing.Value;
object bookmarks="C2";
oWord = new Word.ApplicationClass();
oWord.Visible = true;
//打開文檔
oDoc = oWord.Documents.Open(ref name, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref? oMissing, ref? oMissing, ref oMissing, ref oMissing);
oDoc.Bookmarks.get_Item(ref bookmarks).Select();???????????????
//插入圖片?并設(shè)置圖片大小?
InlineShape il=oWord.Selection.InlineShapes.AddPicture("c:\\wjjpg.jpg", ref oMissing, ref oMissing, ref Range);
? il.Width = 40;
? il.Height = 50;
總結(jié)
以上是生活随笔為你收集整理的C# 设置Word文档中图片的大小的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在 CentOS 7上安装和使用 D
- 下一篇: c# char unsigned_dll