【发布时间】:2012-07-07 02:43:03
【问题描述】:
我正在尝试使用 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。我不确定resources 应该包含什么,但它似乎对其余的转换很重要。我从 PDFsharp 示例站点复制了这段代码。我的PDF可能有问题吗?我使用 Word 2010 制作的。
【问题讨论】:
-
资源键应以大写“R”开头。您是否尝试过使用 /Resources 而不是 /resources?其他键也有错误的大小写。还是 PDFSharp 以不区分大小写的方式查找?