silverpdf IE8 error

Nov 15, 2010 at 7:22 AM

Hello,

 

I'm very fond of the silverpdf component. It works like a charm in the Google Chrome browser. But... not in IE8...

When I'm creating a pdf file in IE8 a damaged file is generated. Please let me know what I'm doing wrong. This is the source code I use to create a pdf file:

 

	private void printButton_Click(object sender, RoutedEventArgs e)
        {
            bool? dialogResult = saveFileDlg.ShowDialog();

            if (dialogResult == true)
            {
                Rect b = myDiagram.Panel.DiagramBounds;

                double w = b.Width, h = b.Height;
                double s = 1.0;

                BitmapSource bmp = myDiagram.Panel.MakeBitmap(new Size(w, h), 96, new Point(b.X, b.Y), s, BitmapConversion);

                //if (saveFileDlg.FilterIndex == 3)
                //{
                //    s = (w > 2000) ? 2000 / w : w;
                //    s = (h > 2000) ? Math.Min(s, 2000 / h) : s;

                //    w *= s;
                //    h *= s;
                //}
            }
        }

        

        private void BitmapConversion(BitmapSource Img)
        {
            WriteableBitmap wb = new WriteableBitmap(Img);

            if (saveFileDlg.SafeFileName != "")
            {
                switch (saveFileDlg.FilterIndex)
                {
                    case 1:
                        SaveToImage(wb, saveFileDlg, new PngEncoder());
                        break;
                    case 2:
                        SaveToImage(wb, saveFileDlg, new JpegEncoder());
                        break;
                    case 3:
                        //Voor de pdf wordt een jpeg gebruikt
                        SaveToImage(wb, saveFileDlg, new JpegEncoder());
                        break;
                }
            }
        }
        
        private void SaveToImage(WriteableBitmap wb, SaveFileDialog dialog, IImageEncoder encoder)
        {
            if (saveFileDlg.FilterIndex == 3)
            {
                MemoryStream mstream = new MemoryStream();
                ExtendedImage image = wb.ToImage();

                PdfDocument document = new PdfDocument();
                PdfPage page = document.AddPage();
                XGraphics gfx = XGraphics.FromPdfPage(page);
                XImage img;

                page.Rotate = 90;
                encoder.Encode(image, mstream);
                mstream.Seek(0, SeekOrigin.Begin);
                img = XImage.FromStream(mstream);

                gfx.DrawImage(img, 0, 0);
                
                page.Height = img.PixelWidth * .75;
                page.Width = img.PixelHeight * .75;

                document.Save(dialog.OpenFile());
            }
            else
            {
                using (var stream = dialog.OpenFile())
                {
                    ExtendedImage image = wb.ToImage();
                    encoder.Encode(image, stream);
                }
            }
        }

 

 

Nov 22, 2010 at 6:22 AM
Edited Nov 22, 2010 at 6:25 AM

I still haven't found a solution yet. Any suggestions would be greatly appreciated.

Nov 23, 2010 at 2:59 PM
Edited Nov 23, 2010 at 3:01 PM

Error message I'm getting when opening a generated pdf file:

 



Nov 25, 2010 at 9:39 AM

Ah I got the error. It's in this small line of code:

saveFileDlg.FilterIndex

This seems to return a 1 everytime your call saveFileDlg.FilterIndex in IE. This is a known bug and has nothing to do with silverPdf.
https://forums.silverlight.net/forums/p/119489/299833.aspx I just use saveFileDlg.SafeFileName.EndsWith(".pdf") and it works.