使用免费的C#解决方案将pdf转换为jpeg [英] Convert pdf to jpeg using a free c# solution

查看:139
本文介绍了使用免费的C#解决方案将pdf转换为jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C#将pdf文件转换为jpeg.而且解决方案(库)必须是免费的.

I need to convert a pdf file into a jpeg using C#. And the solution (library) has to be free.

我搜索了很多信息,但似乎我不清楚.

I have searched a lot of information but seems that I don't get anything clear.

我已经尝试过itextsharp和pdfbox(但是,我认为pdf2image仅适用于Java)

I already tried itextsharp and pdfbox (but this, the pdf2image is only for java, I think) with no success.

我尝试分别从pdf中提取图像,但是当我尝试提取图像时出现参数无效的错误……似乎它们的编码方式很奇怪.

I tried to extract the images from the pdf individually, but I have an error of invalid parameters when I try to extract the images... Seems that they have a strange encoding.

任何人都可以向我推荐任何一个将pdf保存为jpeg的库吗?例子也将不胜感激.

Anyone can recommend me any library to save a pdf into a jpeg? Examples will be very appreciated too.

解决方案:如何使用C#将pdf转换为图像

  1. 转到 http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx

下载库

按照网络上的步骤

将您的代码添加到您的应用程序中,就像这样(非常简单):

Add your code to your application, like this (very simple):

    //Transform pdf to jpg
    PdfToImage.PDFConvert pp = new PDFConvert();
    pp.OutputFormat = "jpeg"; //format
    pp.JPEGQuality = 100; //100% quality
    pp.ResolutionX = 300; //dpi
    pp.ResolutionY = 300;
    pp.FirstPageToConvert = 1; //pages you want
    pp.LastPageToConvert = 1;
    pp.Convert(PATH_PDF+ "report.pdf", PATH_IMAGE + "name.jpg");  

推荐答案

pdfiumviewer 可能会有所帮助这里.它也可以作为nuget使用.

The library pdfiumviewer might be helpful here. It is also available as nuget.

  1. 创建一个新的winforms应用程序.向其中添加nuget"PdfiumViewer".
  2. 这还将在项目x86和x64文件夹中添加两个名为"pdfium.dll"的本机dll.将复制到输出目录"设置为始终复制".
  3. 尝试以下代码(更改路径以适合您的设置).

  1. Create a new winforms app. Add nuget "PdfiumViewer" to it.
  2. This will also add two native dll's named "pdfium.dll" in folders x86 and x64 to your project. Set "Copy to Output Directory" to "Copy Always".
  3. Try out the following code (change paths to suit your setup).

    try
    {
        using (var document = PdfiumViewer.PdfDocument.Load(@"input.pdf"))
        {
            var image = document.Render(0, 300, 300, true);
            image.Save(@"output.png", ImageFormat.Png);
        }
    }
    catch (Exception ex)
    {
        // handle exception here;
    }

更改的代码以显示页面索引为0,如以下S.C.的注释所指出的那样

更新了解决方案您尝试过pdfsharp吗?

Edit 1: Updated solution Have you tried pdfsharp?

此链接可能会有所帮助

这篇关于使用免费的C#解决方案将pdf转换为jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆