如何使用 PDFsharp .NET 库将 PDF 页面导出为图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2995984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:22:37  来源:igfitidea点击:

How to export PDF page as an image using PDFsharp .NET library?

.netimagepdfbitmappdfsharp

提问by vi.su.

How to export a PDF page as an image using PDFsharp .NET library, for pixel level manipulation?

如何使用 PDFsharp .NET 库将 PDF 页面导出为图像,以进行像素级操作?

For example, something like, System.Drawing.BitMap.GetPixel()

例如,类似 System.Drawing.BitMap.GetPixel()

I am trying to find out empty area (all white, or of any colour) inside a PDF document, to write some graphics / image.

我试图找出 PDF 文档中的空白区域(全白或任何颜色),以编写一些图形/图像。

09, June 2010:

2010 年 6 月 9 日:

I have tried this, but it is not working.

我试过这个,但它不起作用。

Why the following code is not working as expected?

为什么以下代码没有按预期工作?

Bitmap.GetPixel always returns 0.

Bitmap.GetPixel 始终返回 0。

//
// PdfSharp.Pdf.PdfDocument
// PdfSharp.Pdf.PdfPage
// PdfSharp.Drawing.XGraphics
// System.Drawing.Bitmap
//
string srcPDF = @"C:\hcr\test\tmp\file1.pdf";
PdfDocument pdfd = PdfReader.Open(srcPDF);
XGraphics xgfx = XGraphics.FromPdfPage(pdfd.Pages[0]);
Bitmap b = new Bitmap((int) pdfp.Width.Point, (int) pdfp.Height.Point, xgfx.Graphics);

int rgb = b.GetPixel(0, 0).ToArgb();

回答by I liked the old Stack Overflow

The answer can be found in the PDFsharp FAQ list: http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx#Can_PDFsharp_show_PDF_files_Print_PDF_files_Create_images_from_PDF_files_3

答案可以在 PDFsharp 常见问题列表中找到:http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx#Can_PDFsharp_show_PDF_files_Print_PDF_files_Create_images_from_PDF_files_3

PDFsharp creates PDF files, but it cannot render them.

PDFsharp 创建 PDF 文件,但无法渲染它们。

The call

电话

Bitmap b = new Bitmap((int) pdfp.Width.Point, (int) pdfp.Height.Point, xgfx.Graphics);

does not initialize any bits of the bitmap and does not copy anything from the Graphics object except for the DPI setting of the Graphics object. Graphics objects draw things, but they do not remember what they have drawn and they cannot re-create the drawings in a call to new Bitmap(...). This does not work with the Graphics class from Microsoft, this does not work with the XGraphics class from PDFsharp either.

不初始化位图的任何位,也不从 Graphics 对象复制任何内容,除了 Graphics 对象的 DPI 设置。图形对象绘制事物,但它们不记得自己绘制了什么,并且无法在调用new Bitmap(...). 这不适用于 Microsoft 的 Graphics 类,也不适用于 PDFsharp 的 XGraphics 类。

The XGraphics class from PDFsharp can be used to draw on PDF pages and it can be used to draw on bitmaps, on a printer, or on the screen - it can draw on PDF pages and on any DC you can get from Windows. Same goes for MigraDoc.
So if you want to create PDF files and bitmaps with the same contents, PDFsharp and MigraDoc can help.

PDFsharp 的 XGraphics 类可用于在 PDF 页面上绘图,也可用于在位图、打印机或屏幕上绘图 - 它可以在 PDF 页面和您可以从 Windows 获得的任何 DC 上绘图。MigraDoc 也是如此。
因此,如果您想创建具有相同内容的 PDF 文件和位图,PDFsharp 和 MigraDoc 可以提供帮助。

But PDFsharp does not provide any way to render a PDF page to a bitmap.

但是 PDFsharp 不提供任何方式将 PDF 页面渲染为位图。