我正在寻找一个.NET GUI组件(不同于PDFsharp),它可以预览PDF单页文档。基本上,我需要类似于PictureBox的东西,可以加载位图并显示出来。
如果该组件允许缩放和移动图片,则更好。最好是免费软件解决方案 :)
如果该组件允许缩放和移动图片,则更好。最好是免费软件解决方案 :)
幸运的是,存在免费的替代品:基于xpdf代码库的poppler工具,其受GPL许可并可用作控制台实用程序。从.NET代码中,可以使用System.Diagnostics.Process执行它。
为了简化poppler工具的使用,我们开发了NReco.PdfRenderer .NET包装器,其中嵌入了poppler windows二进制文件(它们在首次使用时被提取),并提供了简单的API,用于将PDF页面渲染为图像:
var pdfToImg = new NReco.PdfRenderer.PdfToImageConverter();
Image firstPageImg = pdfToImg.GenerateImage( "test.pdf", 1);
组件不是免费的,但它的定价非常合理。
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图像,因此您可以相当轻松地处理缩放。