C# 设置Word文档中图片的大小

在创建Word文档时,我们经常需要向文档中插入图片,但插入图片的大小有时候可能会太大或太小,这时候我们就需要对图片的大小进行调整,使得图片与文章更加协调、美观。这篇文章将介绍如何使用Free Spire.Doc组件和C#在Word文档中对新添加的图片和已有的图片进行大小设置。

在使用以下代码前需要创建一个C#应用程序并引用Spire.Doc.dll到工程中。

对新添加的图片进行大小设置

//创建Document实例
Document document = new Document();
 
//添加节和段落
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;
 
//设置图片的大小
Pic.Width = 120f;
Pic.Height = 170f;
 
//保存文档
document.SaveToFile("Image.docx", FileFormat.Docx);

效果图:


对已有的图片进行大小设置

//加载Word文档
Document document = new Document("Image.docx");
 
//获取第一个节
Section section = document.Sections[0];
//获取第一个段落
Paragraph paragraph = section.Paragraphs[0];
 
//调整段落中图片的大小 
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对象:
指定位置  :  设置书签。
主要代码:

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();               
//插入图片 并设置图片大小 
InlineShape il=oWord.Selection.InlineShapes.AddPicture("c:\\wjjpg.jpg", ref oMissing, ref oMissing, ref Range);
  il.Width = 40;
  il.Height = 50;

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# 使用 Aspose.Words 库插入图片时,你可以通过设置 `Shape` 对象的宽和高度来调整图片大小。但是,要确保在设置大小之前,已经设置图片的实际内容。 以下是一个示例代码,展示了如何在插入图片到 Word 文档后调整其大小: ```csharp using Aspose.Words; class Program { static void Main(string[] args) { // 加载 Word 文档 Document doc = new Document("input.docx"); // 创建 Shape 对象并设置图片路径 Shape shape = new Shape(doc, ShapeType.Image); shape.ImageData.SetImage("image.jpg"); // 获取 Shape 的图片对象 ImageData image = shape.ImageData; // 确保图片已加载 if (image.IsImage && !image.HasImage) { image.SetImage(image.ImageBytes); } // 设置 Shape 的大小 shape.Width = 200; shape.Height = 200; // 获取要插入图片的段落(假设为第一段) Paragraph para = doc.FirstSection.Body.FirstParagraph; // 在段落插入 Shape 对象 para.AppendChild(shape); // 保存文档 doc.Save("output.docx"); } } ``` 在上述代码,我们首先加载了要编辑的 Word 文档(假设为 "input.docx")。然后,创建一个 `Shape` 对象,并设置其类型为图像类型(`ShapeType.Image`)。接着,通过 `ImageData.SetImage()` 方法设置图像的路径。获取 `Shape` 对象的图片对象 `ImageData`,并确保图片已加载。然后,设置 `Shape` 对象的宽度和高度。获取要插入图片的段落(这里假设你的文档的第一段是合适的位置),并将 `Shape` 对象添加到该段落的末尾。最后,保存文档(保存为 "output.docx")。 请注意,上述代码图片路径、Shape 对象的位置和大小等都是示例的值,你需要根据你的实际情况调整这些值。确保在调整图片大小之前,已经加载了图片的内容。这样,设置 `Shape` 对象的宽度和高度才会生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值