使用c#将pdf保存到jpeg [英] Save pdf to jpeg using c#

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

问题描述

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

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

我搜索过很多信息,但似乎我没有得到任何明确的信息。

I have searched a lot of information but seems that I don't get nothing 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 an 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

  2. 下载de library

  3. 按照网络中的步骤

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

  1. Go to: http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx
  2. Download de library
  3. Follow the steps in the web
  4. 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应用程序。添加nugetPdfiumViewer。

  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;
    }

编辑2:更改代码以显示页面索引为0正如SC下面的评论所指出的

编辑1:更新解决方案
你试过pdfsharp吗?

Edit 1: Updated solution Have you tried pdfsharp?

此链接可能会有所帮助

这篇关于使用c#将pdf保存到jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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