用C#预览PDF

时间:2010-03-16 13:38:19

标签: c# .net pdf c#-3.0

我正在寻找允许预览PDF 1页文档的.NET GUI组件(不同于PDFsharp)。
基本上我需要类似于PictureBox的东西,我可以加载位图并显示它。

如果该组件允许缩放和移动图片,那将会很棒 免费软件解决方案首选:)

5 个答案:

答案 0 :(得分:5)

另一种选择是在GUI中使用WebBrowser控件。它将使用浏览器来呈现PDF,但我会使用Adobe Reader ActiveX组件而不是弄乱。

如果您不希望客户端上有任何类型的PDF阅读器,您还可以通过GhostScript将其转换为图形文件并将其显示为位图。

答案 1 :(得分:2)

您可以使用Acrobat Reader附带的activex组件。

How to render pdfs using C#

答案 2 :(得分:2)

问题相当陈旧,但提议的解决方案存在明显的缺点:

  • WebBrowser控件依赖于IE并且它可以显示PDF文档(只有安装了Adobe Reader才会出现这种情况)
  • GhostScript是根据AGPL许可的,它需要相当昂贵的许可证才能在封闭源/商业项目中使用。

幸运的是,存在免费替代方案:poppler工具(基于xpdf代码库),它们是根据GPL许可的,可以用作控制台实用程序。从.NET代码可以使用System.Diagnostics.Process执行。

为了简化poppler工具的使用,我们开发了NReco.PdfRenderer .NET包装器,它嵌入了poppler windows二进制文件(它们在首次使用时被提取)并提供了简单的API,用于将PDF页面呈现为图像: / p>

var pdfToImg = new NReco.PdfRenderer.PdfToImageConverter();
Image firstPageImg = pdfToImg.GenerateImage( "test.pdf", 1);

组件不是免费的,但其定价非常合理。

答案 3 :(得分:1)

ImageGear for .NET

不自由。可以说比你所关注的范围更大。我希望它有所帮助。

答案 4 :(得分:0)

Quick PDF Library,我的公司PDF SDK,将帮助您呈现PDF文件。它不是免费软件,但许可证允许免费分发您使用它构建的已编译应用程序。查找用于呈现PDF文件的免费/开源组件比其他基本PDF操作任务有点棘手,因为渲染PDF文件可能非常困难。

以下是一些C#示例源代码,介绍如何在表单的图片框中呈现PDF。

private void Form1_Load(object sender, EventArgs e)
{
    QuickPDFAX0718.PDFLibrary pdf = new QuickPDFAX0718.PDFLibrary();

    qp.UnlockKey("......Licence Key......");

    // Open PDF File
    int Handle = qp.DAOpenFile("C:\\sample.pdf", null);

    // Get Total Number of Pages in a PDF File
    int PageCount = qp.DAGetPageCount(Handle);

    int PageNo = 1;

    // It will get Reference of page 1 from PDF file
    int PageRefNo = qp.DAFindPage(Handle, PageNo);

    // You can change this parameter for Zoom In/Zoom Out purpose
    int Zoom = 76;
    double pageWidth = qp.DAGetPageWidth(Handle, PageRefNo) / Zoom;
    double pageHeight = qp.DAGetPageHeight(Handle, PageRefNo) / Zoom;

    // DPI use for rendering the page. Increase DPI will increate quality of image
    int dpi = 92;

    // Calculate Dimension of final output image
    Bitmap b = new Bitmap(Convert.ToInt32(pageWidth * dpi), Convert.ToInt32(pageHeight * dpi));

    // This will Draw render image on GDI
    using (Graphics g = Graphics.FromImage(b))
    {
    IntPtr dc = g.GetHdc();
    qp.DARenderPageToDC(Handle, PageRefNo, dpi, (int)dc);
    g.ReleaseHdc(dc);
    }

    // Assigne rendered image to PictureBox Control which will display PDF on Windows Form.
    pictureBox1.Image = b;
    pictureBox1.BorderStyle = BorderStyle.Fixed3D;
}

该库不包含内置函数来帮助您进行缩放,但由于您将PDF渲染为BMP图像,因此您可以非常轻松地处理缩放。