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

发布于 2024-09-05 02:56:14 字数 678 浏览 3 评论 0原文

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

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

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

2010 年 6 月 9 日:

我已尝试过此操作,但不起作用。

为什么下面的代码不能按预期工作?

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();

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

For example, something like, 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.

09, June 2010:

I have tried this, but it is not working.

Why the following code is not working as expected?

Bitmap.GetPixel always returns 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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-09-12 02:56:14

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

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

该调用

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

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

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

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

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 creates PDF files, but it cannot render them.

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文