Save Canvas to PDF

Oct 6, 2010 at 12:13 PM

Hello,

I am using .Net Image Tools to create an Image from a Canvas element. Now I want to create a PDF from the Image, but I am getting the following error:
{System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at PdfSharp.Pdf.PdfDictionary.DictionaryElements.set_Item(String key, PdfItem value)
   at PdfSharp.Pdf.Advanced.PdfImage.Merge(PdfDictionary other)
   at PdfSharp.Pdf.Advanced.PdfImage..ctor(PdfDocument document, XImage source_image)
   at PdfSharp.Pdf.Advanced.PdfImageTable.GetImage(XImage image)
   at PdfSharp.Pdf.PdfPage.GetImageName(XImage image)
   at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.GetImageName(XImage image)
   at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.Realize(XImage image)
   at PdfSharp.Drawing.Pdf.XGraphicsPdfRenderer.DrawImage(XImage image, Double x, Double y, Double width, Double height)
   at PdfSharp.Drawing.XGraphics.DrawImage(XImage image, Double x, Double y)
   at PdfSharp.Drawing.XGraphics.DrawImage(XImage image, Int32 x, Int32 y)
   at ImageToolsDemo.MainPage.button3_Click(Object sender, RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}

This is the code I am using:

SaveFileDialog d = new SaveFileDialog();
d.Filter = "PDF file format|*.pdf";

// Save the document...
if (d.ShowDialog() == true)
{
    PdfDocument document = new PdfDocument();
    PdfPage page = document.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);

    ImageTools.Image img = this.canvas1.ToImage();
    Stream s = img.ToStream();
    XImage pdfImg = XImage.FromStream(s);
    gfx.DrawImage(pdfImg, 0, 0);

    document.Save(d.OpenFile());
}
Does anybody know what the problem is?

Thx,
Danny
Oct 14, 2010 at 2:22 AM

Hi Danny,

 

I got the same stack trace as you, though eventuated a different way, I was loading a png from resources. changing the source file to a jpg fixed the problem.

I would suggest you need something like this to convert your canvas image to a jpeg:

 

SaveFileDialog d = new SaveFileDialog();
d.Filter = "PDF file format|*.pdf";

// Save the document...
if (d.ShowDialog() == true)
{
    PdfDocument document = new PdfDocument();
    PdfPage page = document.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);

    ImageTools.Image img = this.canvas1.ToImage();
    MemoryStream mstream = new MemoryStream();
    JpegEncoder encoder = new JpegEncoder();
    encoder.Encode(image, mstream);
    mstream.Seek(0, SeekOrigin.Begin);
    XImage pdfImg = XImage.FromStream(s);
    gfx.DrawImage(pdfImg, 0, 0);

    document.Save(d.OpenFile());
};

This requires imagetools to get access to the JpegEncoder, but once this is converted to jpeg format, it should work.

csmacnz
Oct 14, 2010 at 5:52 AM
csmacnz,
Just tried it out and it did work. Great !!!!
Thx a lot,
Danny
Jan 27, 2015 at 6:34 AM
Edited Jan 27, 2015 at 6:36 AM
hi i want Save Canvas to PDF

i am using this code:
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "PDF file format|*.pdf";

            // Save the document...
            if (d.ShowDialog() == true)
            {
                PdfDocument document = new PdfDocument();
                PdfPage page = document.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(page);
                ImageTools.Image img = this.LayoutRoot.ToImage();
                Stream s = img.ToStream();
                MemoryStream mstream = new MemoryStream();
                JpegEncoder encoder = new JpegEncoder();
                encoder.Encode(image, mstream);
                mstream.Seek(0, SeekOrigin.Begin);
                XImage pdfImg = XImage.FromStream(s);
                gfx.DrawImage(pdfImg, 0, 0);

                document.Save(d.OpenFile());
            }
"ImageTools.Image"
but error display "The type or namespace name 'image'does not exist in the namespace 'imageTools'(are you missing an assembly reference?)"
and also JpegEncoder
and what is image is in " encoder.Encode(image, mstream);"

i am inserting
using ImageTools;
using ImageTools.IO;
using ImageTools.IO.Png;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

any assembly reference missing?
Jan 27, 2015 at 8:05 AM
hello scheelings

please help me i am getting same error pls help me,
if possible than give me a one source code of this code.


thanks in advance,
Ketan mevada
Jan 27, 2015 at 11:22 AM
Thanks now final my code is running and running code is :
            SaveFileDialog d = new SaveFileDialog();
            d.Filter = "PDF file format|*.pdf";

            // Save the document...
            if (d.ShowDialog() == true)
            {
                PdfDocument document = new PdfDocument();
                PdfPage page = document.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(page);
                ExtendedImage myImage = LayoutRoot.ToImage();
                page.Width = ((ImageTools.ImageBase)(myImage)).Bounds.Width - 350;

                MemoryStream mstream = new MemoryStream();
                JpegEncoder encoder = new JpegEncoder();
                encoder.Quality = 90;
                encoder.Encode(myImage, mstream);
                mstream.Seek(0, SeekOrigin.Begin);
                XImage pdfImg = XImage.FromStream(mstream);
                gfx.DrawImage(pdfImg, 0, 0);

                btnSave.Visibility = Visibility.Visible;
                btnCBEPrint.Visibility = Visibility.Visible;
                document.Save(d.OpenFile());

            }
Ketan mevada
(silverlight and asp.net developer)