我有一个 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, '\0', false);
stamper.Close();
reader.Close();
result = ms.ToArray();
}
return result;
}