Export list of canvas into PDF

Jul 25, 2012 at 8:11 AM


I have a list of canvas having tree-view, text and images, I want to export all these canvas into PDF.

I am doing this with the help of silverpdf dll, It's working fine for me when only text content are there over canvas. But when I add image as a child of canvas it causing the error "Bitmap cannot accessed" when I am trying to convert canvas into image.


Here is my sample code:


 private void ExportToPdf_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

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

            // Save the document...
            if (d.ShowDialog() == true)

                // Create a new PDF document
                PdfDocument document = new PdfDocument();

                // Create an empty page
                PdfPage page;

                // Get an XGraphics object for drawing
                XGraphics gfx;

                XPdfFontOptions options;


                XImage pdfImg;
                foreach (var item in lstPages)
                    page = document.AddPage();
                    gfx = XGraphics.FromPdfPage(page);
                    options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
                    pdfImg = DrawUI(item);
                    gfx.DrawImage(pdfImg, 20, 20);




        private XImage DrawUI(Canvas cnv)

            ImageTools.ExtendedImage img = ImageExtensions.ToImage(cnv);
            MemoryStream mstream = new MemoryStream();
            JpegEncoder encoder = new JpegEncoder();
            encoder.Encode(img, mstream);
            mstream.Seek(0, SeekOrigin.Begin);

            XImage pdfImg = XImage.FromStream(mstream);
            return pdfImg;



Please suggest, how can i export all these canvas together.