我有一個MVC應用程序正在上傳PDF文件並使用Magick.NET將每個頁面呈現爲單個PNG圖像。在大多數情況下,轉換效果並不理想,但在一些情況下,我獲得了空白圖像,其中應顯示文本,並在同一圖像中正確顯示其他文本行。有誰知道這可能是什麼原因造成的?使用Magick.NET將PDF轉換爲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;
}
問題是隨機的,還是一些PDF文件一直轉換爲空白圖像? – Micke
一些PDF文件一致轉換。我首先想到它可能是一個字體問題,但PDF具有像Helvetica,Arial等標準字體。 – Steve
我認爲這將是有益的,如果你可以共享一個PDF文件轉換爲空白圖像,如果有的話。 – Micke