.NET 6使用IText7将pdf转成图片
时间: 2024-03-02 22:50:53 浏览: 87
您好!关于使用IText7将PDF转成图片的问题,以下是一个简单的示例代码:
```csharp
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Svg.Renderers;
using iText.Svg.Svg2Pdf;
// 加载待转换的PDF文件
PdfDocument pdfDoc = new PdfDocument(new PdfReader(@"path/to/input.pdf"));
// 遍历PDF的每一页,将其转换成图片
for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
{
// 生成输出文件名
string outputFileName = $"output-{i}.png";
// 生成输出的Image对象
using (var image = new SvgConverter().ConvertToImage(new PdfPage(pdfDoc.GetPage(i)), new SvgConverterProperties().SetImageFormat("png")))
{
// 将Image对象保存为文件
image.Save(outputFileName);
}
}
// 关闭PDF文件
pdfDoc.Close();
```
以上代码使用IText7加载PDF文件,遍历每一页并将其转换成图片,最终保存为PNG格式的文件。需要注意的是,IText7使用了SVG转换器将PDF页面转换成图片,因此需要在代码中引入相关的命名空间(`iText.Svg.Renderers`和`iText.Svg.Svg2Pdf`)。此外,还需要安装`System.Drawing.Common`和`SkiaSharp`两个NuGet包来支持图片的处理和输出。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)