我正在嘗試使用PDfSharp將pdf轉換爲jpeg。使用PdfSharp將PDF轉換爲Jpeg(資源爲空)
這裏是我的代碼:
PdfSharp.Pdf.PdfDocument document = PdfSharp.Pdf.IO.PdfReader.Open(doc);
PdfSharp.Pdf.PdfPage page = document.Pages[0];
// get resources dictionary
PdfSharp.Pdf.PdfDictionary resources = page.Elements.GetDictionary("/resources");
if (resources != null)
{
// get external objects dictionary
PdfSharp.Pdf.PdfDictionary xobjects = resources.Elements.GetDictionary("/xobject");
if (xobjects != null)
{
ICollection<PdfSharp.Pdf.PdfItem> items = xobjects.Elements.Values;
// iterate references to external objects
foreach (PdfSharp.Pdf.PdfItem item in items)
{
PdfSharp.Pdf.Advanced.PdfReference reference = item as PdfSharp.Pdf.Advanced.PdfReference;
if (reference != null)
{
PdfSharp.Pdf.PdfDictionary xobject = reference.Value as PdfSharp.Pdf.PdfDictionary;
// is external object an image?
if (xobject != null && xobject.Elements.GetString("/subtype") == "/image")
{
ExportJpegImage(xobject);
}
}
}
}
}
行:if (resources != null)
是返回false。我不確定應該包含哪些資源,但對於轉換的其餘部分似乎很重要。我從pdfSharp示例站點複製了此代碼。我的PDF可能有問題嗎?我使用單詞2010。
任何幫助將不勝感激!
資源鍵應以大寫'R'開頭。你有沒有嘗試過使用**/Resources **而不是**/resources **?其他鍵也有錯誤的大寫。還是以不區分大小寫的方式查找PDFSharp? – mkl 2013-09-07 07:01:42