C#实现PDF文件添加图片背景

2019-12-26 18:00:47王冬梅

第四步:加载图片并把它设置为页面背景。


Image backgroundImage = Image.FromFile("background.jpg");
page.BackgroundImage = backgroundImage;


第五步:保存文件并重新打开。


doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");


放上全部代码:


using System.Drawing;
using Spire.Pdf;
 
namespace Add_image_background_to_PDF
{
  class Program
  {
    static void Main(string[] args)
    {
      PdfDocument doc = new PdfDocument();
      doc.LoadFromFile("sample.pdf");
      PdfPageBase page = doc.Pages[0];
      Image backgroundImage = Image.FromFile("background.jpg");
      page.BackgroundImage = backgroundImage;
 
      doc.SaveToFile("result.pdf");
      System.Diagnostics.Process.Start("result.pdf");
    }
  }
}

 总结:

PDF虽不是微软的办公软件,但却因为它有诸多优点,而被广泛使用。PDF本身相较于Word和Excel等文件更不容易被编辑,而需要借用其它的组件,在这个示例中我使用的是E-iceblue公司的免费PDF组件,就目前来看我想要的功能基本上都能满足,也比较方便。



注:相关教程知识阅读请移步到c#教程频道。