当前位置: 移动技术网 > IT编程>开发语言>c# > C#向PPT文档插入图片以及导出图片的实例

C#向PPT文档插入图片以及导出图片的实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

 powerpoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是powerpoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版powerpoint组件—free spire.presentation,以c#/vb.net编程的方式来快速地实现这两个功能。我们可以从官网下载free spire.presentation,创建项目后添加此dll作为引用。

插入图片

向ppt文档插入图片时,这里我选择插入两张图片到不同的两张幻灯片中。

具体步骤:

在之前需要添加以下命名空间:

using spire.presentation;
using spire.presentation.drawing;

步骤1:新建一个ppt文档。

presentation presentation = new presentation();

presentation.slides.append();

步骤2:插入第一张图片到第一张幻灯片

string imagefile = @"c:\users\administrator\pictures\01.jpg";
rectanglef rect = new rectanglef(350, 100, 300, 250);  
presentation.slides[0].shapes.appendembedimage(shapetype.rectangle, imagefile, rect);
presentation.slides[0].shapes[0].line.fillformat.solidfillcolor.color = color.floralwhite;

步骤3:添加形状,再添加文本到形状里面。

rectanglef rect2 = new rectanglef(50, 100, 300, 250);
iautoshape shape = presentation.slides[0].shapes.appendshape(shapetype.rectangle, rect2);
shape.fill.filltype = fillformattype.none;
shape.shapestyle.linecolor.color = color.white;

//添加文本到形状中
shape.textframe.text = "大熊猫是哺乳动物,已在地球上生存了至少800万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足1600只,属于中国国家一级保护动物。";
textrange textrange = shape.textframe.textrange;
shape.textframe.paragraphs[0].alignment = textalignmenttype.left;


//设置文本字体
textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
textrange.fill.solidcolor.color = color.black;
textrange.latinfont = new textfont("arial black"

步骤4:同样,插入第二张图片到第二张幻灯片,添加形状,再添加文本到形状里面。最后保存文档。

presentation.savetofile(@"c:\users\administrator\desktop\result.pptx ", fileformat.pptx2010);
system.diagnostics.process.start(@"c:\users\administrator\desktop\result.pptx ");

效果图:

 

全部代码:

 using system;
 using system.drawing;
 using system.windows.forms;
 using spire.presentation;
 using spire.presentation.drawing;
 
 namespace insertimageinpowerpointfille
 {
  public partial class form : form
  {
   public form()
   {
    initializecomponent();
   }
 
   private void button_click(object sender, eventargs e)
   {
    //新建ppt
    presentation presentation = new presentation();
    presentation.slides.append();
 
    //插入第一张图片到第一张幻灯片
    string imagefile = @"c:\users\administrator\pictures\.jpg";
    rectanglef rect = new rectanglef(, , , );
    presentation.slides[].shapes.appendembedimage(shapetype.rectangle, imagefile, rect);
    presentation.slides[].shapes[].line.fillformat.solidfillcolor.color = color.floralwhite;
 
    //添加形状
    rectanglef rect = new rectanglef(, , , );
    iautoshape shape = presentation.slides[].shapes.appendshape(shapetype.rectangle, rect);
    shape.fill.filltype = fillformattype.none;
    shape.shapestyle.linecolor.color = color.white;
 
    //添加文本到形状中
    shape.textframe.text = "大熊猫是哺乳动物,已在地球上生存了至少万年,被誉为活化石和中国国宝,世界自然基金会的形象大使,是世界生物多样性保护的旗舰物种。据第三次全国大熊猫野外种群调查,全世界野生大熊猫已不足只,属于中国国家一级保护动物。";
    textrange textrange = shape.textframe.textrange;
    shape.textframe.paragraphs[].alignment = textalignmenttype.left;
 
    //设置文本字体
    textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
    textrange.fill.solidcolor.color = color.black;
    textrange.latinfont = new textfont("arial black");
 
    //插入第二张图片到第二张幻灯片
    string imagefile = @"c:\users\administrator\pictures\.jpg";
    rectanglef rect = new rectanglef(, , , );
    presentation.slides[].shapes.appendembedimage(shapetype.rectangle, imagefile, rect);
    presentation.slides[].shapes[].line.fillformat.solidfillcolor.color = color.floralwhite;
 
    //添加形状
    rectanglef rect = new rectanglef(, , , );
    iautoshape shape = presentation.slides[].shapes.appendshape(shapetype.rectangle, rect);
    shape.fill.filltype = fillformattype.solid;
    shape.fill.filltype = fillformattype.none;
    shape.shapestyle.linecolor.color = color.white;
 
    //添加文本到形状中
    shape.textframe.text = "黑白相间的外表,有利隐蔽在密林的树上和积雪的地面而不易被天敌发现。相对锋利的爪和发达有力的前后肢,有利于大熊猫能快速爬上高大的乔木。";
    textrange textrange = shape.textframe.textrange;
 
    //设置文本字体
    textrange.fill.filltype = spire.presentation.drawing.fillformattype.solid;
    textrange.fill.solidcolor.color = color.blue;
    textrange.latinfont = new textfont("arial black");
 
    //保存文件
    presentation.savetofile(@"c:\users\administrator\desktop\result.pptx ", fileformat.pptx);
    system.diagnostics.process.start(@"c:\users\administrator\desktop\result.pptx ");
   }
  }
 }

从上面的代码可以发现,其实通过这个组件,我们还可以自由地设置我们想要的形状、文本、字体、颜色等等,用起来确实方便又快速。感兴趣的话可以试一下其他丰富的效果。

导出图片

现在,我们导出上述运行后文档的图片。

具体步骤:

同样添加如下命名空间:

using spire.presentation;

步骤1: 新建一个presentation对象,并加载presentation文件。

presentation ppt = new presentation();
ppt.loadfromfile(@"c:\users\administrator\desktop\result.pptx");

 步骤2:遍历ppt文档所有的图片,并保存为.png格式。

for (int i = 0; i < ppt.images.count; i++)

   {

    image image = ppt.images[i].image;

    image.save(string.format(@"..\..\images{0}.png", i));

   }

效果图:

 

全部代码:

 using system;
 using system.drawing;
 using system.windows.forms;
 using spire.presentation;
 
 namespace extractimagesfromppt
 {
  public partial class form : form
  {
   public form()
   {
    initializecomponent();
   }
 
   private void button_click(object sender, eventargs e)
   {
    presentation ppt = new presentation();
    ppt.loadfromfile(@"c:\users\administrator\desktop\result.pptx");
    for (int i = ; i < ppt.images.count; i++)
    {
     image image = ppt.images[i].image;
     image.save(string.format(@"..\..\images{}.png", i));
 
    }
   }
  }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网