如何导出PDF页面使用PDFsharp .NET库中的形象?库中、形象、页面、PDF

2023-09-03 01:21:44 作者:伶幽

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

例如,像,System.Drawing.BitMap.GetPixel()

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

09,2010年6月:

我已经试过这一点,但它不能正常工作。

为什么以下code不能按预期工作?

Bitmap.GetPixel总是返回0。

  //
// PdfSharp.Pdf.PdfDocument
// PdfSharp.Pdf.PdfPage
// PdfSharp.Drawing.XGraphics
// System.Drawing.Bitmap
//
字符串srcPDF = @C:\ HCR \测试\ TMP \ file1.pdf;
PdfDocument pdfd = PdfReader.Open(srcPDF);
XGraphics xgfx = XGraphics.FromPdfPage(pdfd.Pages [0]);
位图B =新位图((INT)pdfp.Width.Point,(INT)pdfp.Height.Point,xgfx.Graphics);

INT RGB = b.GetPixel(0,0).ToArgb();
 

解决方案

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

PDFsharp创建PDF文件,但它不能使它们

呼叫

 位图B =新位图((INT)pdfp.Width.Point,(INT)pdfp.Height.Point,xgfx.Graphics);
 
如何把SQLServer表数据导出为Excel文件

不初始化位图中的任何位,不会复制任何东西从图形除了DPI设置对象的图形对象。图形对象绘制的东西,但他们不记得他们已制定,他们不能在电话重新创建的图纸新位图(...)。这并不与微软Graphics类的工作,这并不与PDFsharp的XGraphics类擦出火花。

从PDFsharp的XGraphics类可以用来借鉴PDF页面,它可以被用来创建位图。也是一样的MigraDoc。 所以,如果你想创建的PDF文件和位图具有相同的内容,PDFsharp和MigraDoc可以提供帮助。

但PDFsharp不提供任何的方式来呈现PDF页面为位图。

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

解决方案

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 create bitmaps. 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.