0

I have searched for hours trying to find out how to do this without any success.

I did find the 'FromGraphics' command which reads a 'system.drawing.graphics' object into the XGraphics type, but am unsure how to add this to the PDF output.

Most examples I find show how to take images from an existing file and save them to a PDF, but how do I do this with a pre-made system.drawing.graphics object that has been made pragmatically?

A C# and/or VB.net example would be very much appreciated instead of a GIF.

E.g. how do I change an example like the following to use the system.drawing.graphics object?

string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Images");
Document doc = new Document();
try
{
  PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Images.pdf",   FileMode.Create));
  doc.Open();

  doc.Add(new Paragraph("GIF"));
  Image gif = Image.GetInstance(imagepath + "/mikesdotnetting.gif");
  doc.Add(gif);
}
catch (Exception ex)
{
  //Log error;
}
finally
{
  doc.Close();
}
Garry_G
  • 169
  • 5
  • 12
  • 1
    You have gotten good answers. Just to make sure you get this right: The Graphics obejct __does not contain ANY graphics__. It is just a tool to paint pixels on a) a surface b) a bitmap c) a printPage. Or d) into a PDF, if you use the special Graphics object from your pdf lib.. – TaW Jun 05 '15 at 08:25

2 Answers2

2

Short example:

Dim pdfDoc As New PdfDocument
Dim page As New PdfPage
pdfDoc.Pages.Add(page)
Dim xg = XGraphics.FromPdfPage(page)
 'use the xg object to draw on the pdf page
pdfDoc.Save("path to file")
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • Sorry, I'm not getting this. If I have a pre-made system.drawing.graphics object stored to graphics and I move this it to an XGraphics gfx object: Eg. XGraphics gfx = FromGraphics(graphics, size) How do I add the gfx object to the PDF file page? Do I use the image type, and if so how? – Garry_G Jun 04 '15 at 23:08
  • Call the methods on the `xg` object. i.e. `xg.DrawString(...)` – OneFineDay Jun 04 '15 at 23:31
  • I use the `XGraphics.FromPdfPage` myself. It draws right to the page. – OneFineDay Jun 04 '15 at 23:33
  • The `FromGraphics(graphics, size)` will draw on the control that has the Graphics(like a picturebox) - see the [docs](http://www.pdfsharp.net/wiki/Graphics.ashx?AspxAutoDetectCookieSupport=1) so this method does not draw to the pdf page – OneFineDay Jun 04 '15 at 23:49
  • the `Graphics` object does not store anything, the `GraphicsPath` does. – OneFineDay Jun 04 '15 at 23:50
2

To answer the main question "How to save a system.drawing.graphics object to a PDF file using PDFsharp?"
You can't do that. You can use the XGraphics class to draw on PDF pages, on the screen, on a printer. You can create an XGraphics object for a PDF page or from a Graphics object. But anything that was drawn on the Graphics object with other than XGraphics routines will not appear in the PDF.

The other question: "How do I change an example like the following to use the system.drawing.graphics object?"
You need an XGraphics object to draw on PDF. User OneFineDay showed how to get one. Then you use xg.DrawImage(...) to draw images on a PDF page. You do not need a Graphics object unless you also want to draw on other media than PDF pages.