31

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();
vi.su.
  • 685
  • 2
  • 9
  • 19
  • Could it be that `xgfx.Graphics` is always `null`? The description for the `new Bitmap(int, int, Graphics)` method: "Initializes a new instance of the Bitmap class with the specified size and with the resolution of the specified Graphics object." No wonder that all pixel return 0 as this function does not (and cannot) copy any pixels from the Graphics object. – I liked the old Stack Overflow Jun 30 '15 at 08:44

1 Answers1

23

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.

  • I don't understand, while writing to System.Drawing.Graphics is possible, reading pixels from it made intentionally impossible. For me, creating Bitmap object from Graphics looks like a reasonable requirement. :( – vi.su. Jun 11 '10 at 07:56
  • 14
    You write: "reading pixels from it made intentionally impossible". That's not true: we don't make it impossible. PDF is a vector format. How do you read pixels from a vector format? You can render PDF to a bitmap and read pixels from that. But PDFsharp doesn't render bitmaps. – I liked the old Stack Overflow Jun 14 '10 at 07:34
  • 1
    Can this be accomplished via MigraDoc? http://www.pdfsharp.net/wiki/documentviewer-sample.ashx – Scotty H Oct 01 '15 at 14:39
  • The DocumentPreview cannot display PDF files because PDFsharp cannot render PDF (as mentioned before). JPEG and PNG images show in the preview, but for PDF pages you only see a placeholder. MigraDoc uses PDFsharp for anything that relates to PDF. – I liked the old Stack Overflow Oct 01 '15 at 14:54
  • "The XGraphics class from PDFsharp can be used to draw on PDF pages and it can be used to create bitmaps." How do you get a bitmap from an XGraphics instance? – smurtagh Aug 08 '16 at 21:25
  • @smurtagh You can get an XGraphics object for any DC. If that DC belongs to a bitmap then the XGraphics object will draw on that bitmap. It does not create bitmaps, but it can draw on bitmaps that were created elsewhere. – I liked the old Stack Overflow Aug 10 '16 at 08:22
  • XGraphics.FromGraphics doesn't seem to exist anymore? How to get an XGraphics from a Bitmap DC? – Etherman Dec 09 '21 at 20:43
  • @Etherman `XGraphics.FromGraphics` still exists, but only on the GDI build that is built on GDI+. It does not exist in the WPF build or the Core build. – I liked the old Stack Overflow Dec 10 '21 at 07:07
  • Found it - need to use PDFSharp-GDI nuget package instead of PDFSharp. Only problem now is HtmlRenderer.PdfSharp package depends on PDFSharp so I get duplicates! – Etherman Dec 10 '21 at 11:27