有什么办法,我可以将HTML文档(文件不是URL)转换为图像,或将PDF转换为图像?
我能够使用Ghostscript DLL执行上述操作,有没有其他方法,我可以这样做,而不使用Ghostscript DLL?
我正在开发一个C#Windows应用程序.
使用LibPdf进行PDF到图像转换
LibPdf库将PDF文件转换为图像.支持的图像格式是PNG和BMP,但您可以轻松添加更多.
用法示例:
using (FileStream file = File.OpenRead(@"..\path\to\pdf\file.pdf")) // in file
{
var bytes = new byte[file.Length];
file.Read(bytes, 0, bytes.Length);
using (var pdf = new LibPdf(bytes))
{
byte[] pngBytes = pdf.GetImage(0,ImageType.PNG); // image type
using (var outFile = File.Create(@"..\path\to\pdf\file.png")) // out file
{
outFile.Write(pngBytes, 0, pngBytes.Length);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ImageMagick,您还应该看看这个免费且功能强大的工具.它能够做你想做的事情,并提供一些.NET绑定(以及与其他几种语言的绑定).
在最简单的形式中,它就像写命令一样
convert file.pdf imagefile.png
Run Code Online (Sandbox Code Playgroud)
最好的免费 nuget 包,您可以将 Pdf 的每一页保存为 png,并且使用自定义分辨率Docnet.core这可以在 .net 核心项目中使用。
他们有 github 和不错的例子,但在这里我想添加我的代码,用于阅读更多一页的 en pdf
string webRootPath = _hostingEnvironment.WebRootPath;
string fullPath = webRootPath + "/uploads/user-manual/file.pdf";
string fullPaths = webRootPath + "/uploads/user-manual";
using (var library = DocLib.Instance)
{
using (var docReader = library.GetDocReader(fullPath, 1080, 1920))
{
for (int i = 1; i < docReader.GetPageCount(); i++)
{
using (var pageReader = docReader.GetPageReader(i))
{
var bytes = EmailTemplates.GetModifiedImage(pageReader);
System.IO.File.WriteAllBytes(fullPaths+"/page_image_" +i+".png", bytes);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在他们的 github 存储库中找到其他功能。
尝试 Freeware.Pdf2Png,检查以下网址:
PDF 到 PNG 转换器。
byte[] png = Freeware.Pdf2Png.Convert(pdf, 1);
Run Code Online (Sandbox Code Playgroud)
https://www.nuget.org/packages/Freeware.Pdf2Png/1.0.1?_src=template
在关于信息中,它说 MIT 许可证,我于 2022 年 3 月 22 日检查。但正如 Mitya 所说,请仔细检查。