zoukankan      html  css  js  c++  java
  • 使用Free Spire.Presentation生成PowerPoint文件

     

    使用Free Spire.Presentation生成PowerPoint文件

    前言

    之前有写过一篇使用Free Spire.XLS生成图表的文章,朋友圈内反应还不错,都希望我能继续写下去,把类似的产品都介绍一下。正好我前段时间把Spire的产品都过了一遍,趁着还算熟悉,写点文章分享一下自己的心得。这次介绍的是使用Free Spire.Presentation生成一个简单的PowerPoint文件。

    准备

    首先从官方网站上下载Free Spire.Presentation组件,安装后有一个Sample Center,类似功能展示的一个程序,有实例代码和相关dll文件,可编译运行,非常方便;当然,如果你只是想下载dll文件,可以使用nuget获取,命令如下:

    PM> Install-Package FreeSpire.Presentation

    步骤

    1. 创建一个PowerPoint文档,默认会生成一张新的幻灯片。

    Presentation ppt = new Presentation();

    ISlide slide = ppt.Slides[0];

    2. 使用图片来填充幻灯片的背景。

    //需要设置Type为Custom, 否则无效

    slide.SlideBackground.Type = BackgroundType.Custom;

    slide.SlideBackground.Fill.FillType = FillFormatType.Picture;

    slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = ppt.Images.Append(Image.FromFile("bg.jpg"));

    slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;

    3. 接下来填充一段文本,并设置相关的样式。这里要说明一下,如果不进行样式的设置,生成文本的颜色、字体、大小等会很奇怪。

    RectangleF textRect = new RectangleF(295, 26, 129, 30);

    IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, textRect);

    shape.Fill.FillType = FillFormatType.None;

    shape.Line.FillType = FillFormatType.None;

    TextParagraph tp = new TextParagraph();

    TextRange tr = new TextRange("National Report");

    tr.Format.LatinFont = new TextFont("Arial Narrow");

    tr.Format.FontHeight = 18f;

    tr.Fill.FillType = FillFormatType.Solid;

    tr.Fill.SolidColor.Color = Color.Black;

    tp.TextRanges.Append(tr);

    4. 插入一个表格并填充数据,然后设置表格的样式。我使用的数组在一个二维数组里,正好对应表格的行列。由于代码过长,这里只贴关键部分:

    ITable table = ppt.Slides[0].Shapes.AppendTable(ppt.SlideSize.Size.Width / 2 - 275, 90, widths, heights);

    //填充数据

    for (int i = 0; i < 13; i++)

    {

    for (int j = 0; j < 5; j++)

    {

    table[j, i].TextFrame.Text = dataStr[i, j];

    //设置字体

    table[j, i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont("Arial Narrow");

    //居中显示文本

    table[j, i].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;

    }

    }

    //使用内建的表格样式

    table.StylePreset = TableStylePreset.MediumStyle4Accent1;

    5. 设置页脚的内容。

    ppt.SetFooterText("Free Spire.Presentation");

    //默认不可见,下同

    ppt.SetFooterVisible(true);

    ppt.SetSlideNoVisible(true);

    ppt.SetDateTimeVisible(true);

    6. 保存到本地并打开。

    ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

    System.Diagnostics.Process.Start("Result.pptx");

    生成的PowerPoint文件如下图:

    clip_image002

    总结

    网上操作PowerPoint的组件不多,Free Spire.Presentation的优势在于免费易用,功能相较Commercial版本略有不足,而且做多只能处理10张幻灯片,不过对于普通使用者来说已经足够了。

  • 相关阅读:
    The 16th Zhejiang Provincial Collegiate Programming Contest Sponsored(E F G H I)
    Ubuntu iso下载地址(14、16、18)
    JS解决在提交form表单时某个值不存在 alter弹窗点确定不刷新界面
    搞搞电脑微信表情的破解(.dat转png or jpg)
    12.29 模拟赛
    bzoj 2151 种树
    bzoj 5110 Yazid的新生舞会
    【系列】 点分治
    12.8 模拟赛
    12.17 模拟赛
  • 原文地址:https://www.cnblogs.com/Yesi/p/4530288.html
Copyright © 2011-2022 走看看