使用Magick.NET将PDF转换为PNG时缺少文本



我有一个MVC应用程序,它正在上传PDF文件,并使用Magick.NET将每个页面呈现为单个PNG图像。在大多数情况下,转换是很好的,但在某些情况下,我得到一个空白的图像,文本应该和其他行文本在同一图像中正确显示。有人知道是什么引起的吗?

下面是我使用的代码。

public FileResult PNGPreview(Guid id, Int32 index)
{
    MagickReadSettings settings = new MagickReadSettings();
    // Settings the density to 300 dpi will create an image with a better quality
    settings.FrameIndex = index;
    settings.FrameCount = 1;
    settings.Density = new PointD(300, 300);
    settings.UseMonochrome = true;
    using (MagickImageCollection images = new MagickImageCollection())
    {
        // Add all the pages of the pdf file to the collection
        images.Read(CreateDocument(id), settings);
        using (MemoryStream stream = new MemoryStream())
        {
            images[0].Write(stream, MagickFormat.Png24);
            stream.Close();
            byte[] result = stream.ToArray();
            return File(result, "image/png");
        }
    }
}
private byte[] CreateDocument(Guid id)
{
    PdfReader reader = new PdfReader(Server.MapPath(String.Format("~/documenttemplates/{0}.pdf", id)));
    byte[] result = null;
    using (MemoryStream ms = new MemoryStream())
    {
        PdfStamper stamper = new PdfStamper(reader, ms, '', false);
        stamper.Close();
        reader.Close();
        result = ms.ToArray();
    }
    return result;
}

导致这个问题的PDF文件是通过电子邮件提供给我的,我被告知这个文件是用Word创建的,然后用Foxit Pro编辑的。

魔法。. NET使用Ghostscript将PDF文件转换为图像。执行如下命令:

"c:Program Files (x86)gsgs9.16bingswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE
-dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -sDEVICE=pnggray"
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"  "-sOutputFile=Test.%d.png" "-fTest.pdf"

这将告诉我们创建的文件已损坏。

**** Error reading a content stream. The page may be incomplete.
**** File did not complete the page properly and may be damaged.
**** Error reading a content stream. The page may be incomplete.
**** File did not complete the page properly and may be damaged.
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Microsoft? Word 2013 <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.

这可以通过用不同的程序创建输入文件来解决

相关内容

  • 没有找到相关文章

最新更新