【问题标题】:Converting PDF to Jpeg using PDFsharp (resource is null)使用 PDFsharp 将 PDF 转换为 Jpeg(资源为空)
【发布时间】: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 以不区分大小写的方式查找?

标签: c# .net pdf jpeg pdfsharp


【解决方案1】:

如果您想将 PDF 转换为 JPEG,并希望使用免费软件库来实现,请考虑使用ImageMagick。这可以在所有主要平台上运行,因此您在 Windows 上会很好。它可以在命令行上启动,您可以设置您喜欢的有损压缩级别。

编辑:啊,我在另一个问题上看到您正在使用 ImageMagick 的 .net 接口。如果您可以让它工作,那就太好了,但您可能会发现使用convert 命令更容易!

【讨论】:

  • 感谢您的回复。是的,我一直在使用 ImageMagickNet,但发现它比我预期的要困难。我对 .NET 世界还很陌生,但您知道我是否可以在我的网站中使用 convert 命令?
  • 我相信你可以。只需找到一种从网页中执行系统命令的方法(我不是 .net 程序员)并调用 convert.exe -quality 85 infile.pdf /temp/path/tempfile.jpg
猜你喜欢
  • 1970-01-01
  • 2011-05-16
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 2016-05-14
  • 1970-01-01
相关资源
最近更新 更多