【问题标题】:Converting pdf to image using c# and Ghostscript使用 c# 和 Ghostscript 将 pdf 转换为图像
【发布时间】:2020-01-30 21:01:05
【问题描述】:

我有一个编程任务。我需要使用免费库或命令行程序使用 C# 将 pdf 转换为图像。

到目前为止,使用 Ghostscript 进行转换是可行的,只是它会在图像中心创建一个 8 个红色框:

pdf的内容只有纯白色,但为什么我的图像上有8个红色框?我做错了什么?

代码如下:

string outputImagesPath = null;


string inputPDFFile = null;
inputPDFFile = @"C:\Users\user\cover.pdf";
outputImagesPath = @"C:\user\Desktop\1.jpg";
string ghostScriptPath = @"C:\Users\gswin32.exe";

String ars = "-o" + outputImagesPath+ "%03d.png -sDEVICE=jpeg -dJPEGQ=100 " + inputPDFFile;
Process proc = new Process();
proc.StartInfo.FileName = ghostScriptPath;
proc.StartInfo.Arguments = ars;
proc.StartInfo.CreateNoWindow = true;
//proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string strOutput = proc.StandardOutput.ReadToEnd();
Console.WriteLine(strOutput);
proc.WaitForExit();

这里是pdf的链接,谢谢https://drive.google.com/open?id=0B0auNx4EZsCUUkFHWGR4MjV5NzA

【问题讨论】:

  • 我相信你的错误在这里+ outputImagesPath+ "%03d.png。不应该是+ outputImagesPath+ "%d.jpeg吗?

标签: c# ghostscript


【解决方案1】:

看看这里,使用另一个支持 .net core 的 nuget 包,无需安装到服务器或本地计算机。best answer saving pdf pages as images

【讨论】:

    【解决方案2】:

    这很可能是由缺少字体或 CIDFont 引起的,矩形是 .notdef 字形,当找不到字形时使用。当然,不看原始PDF文件是无法判断的。

    但是,如果您检查 Ghostscript 反向通道(不,我无法告诉您如何使用 Ghostscript.NET 执行此操作,因为这不是 Artifex 产品)您可能会看到有关缺少字形的警告。

    我可以进一步查看,但前提是您提供 PDF 文件。了解您使用的 Ghostscript 版本也很有帮助。

    【讨论】:

    • 我上传了 pdf 文件。你能帮我么。谢谢
    • 好吧,基本上你的文件过于复杂了,它包括多种形式和虚假的透明度,导致未压缩文件的大小为 4.2 MB。其中一种形式包含“ (3')3')'HPR'HPR) Tj ”,它实际上绘制了文本。有问题的字体是嵌入的 CIDFont,使用中的 CMap 将这些字体映射到字体中不存在的字形。因此 .notdef 结果。基本上,您的 PDF 文件已损坏,而且效率极低。您“修复”此问题的唯一方法是从文件中删除文本。
    • 顺便说一下,Ghostscript 并不是“免费”的,它的开源,这不完全一样。请确保您遵守获得许可的 AGPL 条款。
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    相关资源
    最近更新 更多