C# 使用PDFsharp将PDF转换为Jpeg(资源为空)
我正在尝试使用PDFsharp将pdf转换为jpeg 这是我的密码:C# 使用PDFsharp将PDF转换为Jpeg(资源为空),c#,.net,pdf,jpeg,pdfsharp,C#,.net,Pdf,Jpeg,Pdfsharp,我正在尝试使用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
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);
}
}
}
}
}
PdfSharp.Pdf.PdfDocument document=PdfSharp.Pdf.IO.PdfReader.Open(doc);
PdfSharp.Pdf.PdfPage page=document.page[0];
//获取资源字典
PdfSharp.Pdf.PdfDictionary resources=page.Elements.GetDictionary(“/resources”);
if(资源!=null)
{
//获取外部对象字典
PdfSharp.Pdf.PdfDictionary xobjects=resources.Elements.GetDictionary(“/xobject”);
if(xObject!=null)
{
ICollection items=xobjects.Elements.Values;
//迭代对外部对象的引用
foreach(PdfSharp.Pdf.PdfItem项目中的项目)
{
PdfSharp.Pdf.Advanced.PdfReference reference=作为PdfSharp.Pdf.Advanced.PdfReference的项目;
如果(引用!=null)
{
PdfSharp.Pdf.PdfDictionary xobject=reference.Value为PdfSharp.Pdf.PdfDictionary;
//外部物体是图像吗?
if(xobject!=null&&xobject.Elements.GetString(“/subtype”)==“/image”)
{
导出JPEG图像(xobject);
}
}
}
}
}
行:
if(resources!=null)
返回false。我不确定资源
应该包含什么,但它似乎对转换的其余部分很重要。我从PDFsharp示例站点复制了这段代码。我的PDF有问题吗?我使用Word 2010。 如果你想把PDF转换成JPEG,并且想用一个免费的软件库来做,考虑一下。这在所有主要平台上都可以运行,所以在Windows上也可以。它可以在命令行上启动,您可以设置首选的有损压缩级别
编辑:啊,我在另一个问题上看到,您正在使用.net接口访问ImageMagick。如果您可以让它工作,那就太好了,但是您可能会发现只需使用
convert
命令就更容易了 谢谢你的回复。是的,我一直在使用ImageMagickNet,但我发现它比我预期的更难。我对.NET世界相当陌生,但是您知道我是否能够将convert
命令作为我站点的一部分使用吗?我相信您可以。只要找到一种在网页中执行系统命令的方法(我不是.net程序员)并调用convert.exe-quality 85 infle.pdf/temp/path/tempfile.jpg
资源键应该以大写字母“R”开头。您是否尝试过使用/Resources而不是/Resources?其他键也有错误的大小写。还是PDFSharp查找不区分大小写?