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

查看:35
本文介绍了使用免费的 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.

推荐答案

图书馆 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 的,正如下面 S.C. 在评论中指出的

编辑 1:更新的解决方案您是否尝试过 pdfsharp?

Edit 1: Updated solution Have you tried pdfsharp?

这个链接可能有帮助

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

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