C#使用iTextSharp设置PDF所有页面背景图功能实例
作者:jwang 时间:2021-10-26 02:01:59
本文实例讲述了C#使用iTextSharp设置PDF所有页面背景图功能的方法。分享给大家供大家参考。具体如下:
在生成PDF 的时候,虽然可以在页面中设置背景图。
但有些内容过长夸页面的时候,就很难设置背景图,变成了空白背景的页面!
以下是重新生成每一页 PDF 背景图功能代码!
public void SetPdfBackground(string pdfFilePath)
{
//重新生成的 PDF 的路径
string destFile = HttpContext.Current.Server.MapPath("sample.pdf");
//create new pdf document
FileStream stream = new FileStream(destFile, FileMode.Create, FileAccess.ReadWrite);
PdfReader reader = new PdfReader(pdfFilePath);
//read pdf stream
PdfStamper stamper = new PdfStamper(reader, stream);
string imagePage = HttpContext.Current.Server.MapPath("../images/2012/bg2.png");
System.Drawing.Image image = System.Drawing.Image.FromFile(imagePage);
var img = Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
img.SetAbsolutePosition(0, 0);
int totalPage = reader.NumberOfPages;
for (int current = 1; current <= totalPage; current++)
{
var canvas = stamper.GetUnderContent(current);
var page = stamper.GetImportedPage(reader, current);
canvas.AddImage(img);
}
stamper.Close();
reader.Close();
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,iTextSharp,PDF
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 集成Dubbo框架实例
2022-02-03 21:23:27
深入浅析Spring 的aop实现原理
2023-01-10 00:00:10
C# XML字符串包含特殊字符的处理转换方法小结
2022-08-17 07:21:24
![](https://img.aspxhome.com/file/2023/3/96363_0s.jpg)
java实现文件上传下载和图片压缩代码示例
2023-01-02 17:49:30
Java实战之在线租房系统的实现
2022-09-29 04:44:18
![](https://img.aspxhome.com/file/2023/8/60808_0s.jpg)
C#多线程之线程同步
2022-06-14 19:58:03
![](https://img.aspxhome.com/file/2023/2/94622_0s.jpg)
Android Wear计时器开发
2023-04-10 01:22:21
![](https://img.aspxhome.com/file/2023/4/93324_0s.png)
java协程框架quasar和kotlin中的协程对比分析
2021-06-25 23:22:05
![](https://img.aspxhome.com/file/2023/4/75694_0s.png)
java清除u盘内存卡里的垃圾文件示例
2023-05-12 06:41:38
java实现表单必填参数验证的方法
2023-06-16 21:41:24
Java方法的覆盖与隐藏的区别分析
2021-12-19 05:15:52
Java环境下高德地图Api的使用方式
2022-06-13 06:43:59
![](https://img.aspxhome.com/file/2023/6/85826_0s.jpg)
java如何使用自己的maven本地仓库详解
2022-08-01 12:13:44
jsp、struts、spring、mybatis实现前端页面功能模块化拆分的方案
2023-11-25 07:38:00
Android 读取资源文件实例详解
2022-08-28 13:31:17
Java中的引用类型和使用场景详细
2023-11-29 03:58:19
Java并发编程之Java内存模型
2023-08-17 23:06:10
![](https://img.aspxhome.com/file/2023/7/91547_0s.png)
C# 添加对System.Configuration.dll文件的引用操作
2022-03-05 22:20:31
![](https://img.aspxhome.com/file/2023/7/69807_0s.jpg)
Java使用递归法解决汉诺塔问题的代码示例
2023-09-05 20:31:28
![](https://img.aspxhome.com/file/2023/1/64051_0s.jpg)
springcloud微服务之Eureka配置详解
2021-06-15 14:35:28