RunRenderPdf() public method

RunRenderPdf will render a Pdf given the page structure
public RunRenderPdf ( IStreamGen sg, fyiReporting.RDL.Pages pgs ) : void
sg IStreamGen
pgs fyiReporting.RDL.Pages
return void
Example #1
0
        private void SaveAsPdf(Report report, OneFileStreamGen sg)
        {
            Pages pgs = report.BuildPages();
            FileStream strm=null;
            System.Drawing.Image im=null;

            // Handle any parameters
            float x = 0;		// x position of image
            float y = 0;		// y position of image
            float h = 0;		// height of image
            float w = 0;		// width position of image
            string fname=null;
            int index = _StampInfo.LastIndexOf('?');
            bool bClip=false;	// we force clip if either height or width not specified

            if (index >= 0)
            {
                // Get all the arguments for sizing the image
                ListDictionary ld = this.GetParameters(_StampInfo.Substring(index+1));
                fname = _StampInfo.Substring(0, index);
                string ws = (string)ld["x"];
                x = Size(ws);
                ws = (string)ld["y"];
                y = Size(ws);
                ws = (string)ld["h"];
                if (ws == null)
                {
                    bClip = true;
                    ws = "12in";	// just give it a big value
                }
                h = Size(ws);
                ws = (string)ld["w"];
                if (ws == null)
                {
                    bClip = true;
                    ws = "12in";	// just give it a big value
                }
                w = Size(ws);
            }
            else
            {
                fname = _StampInfo;
                // force size
                bClip = true;
                h = Size("12in");
                w = Size("12in");
            }

            // Stamp the first page
            foreach (Page p in pgs)		// we loop then break after obtaining one
            {
                try
                {
                    strm = new FileStream(fname, System.IO.FileMode.Open, FileAccess.Read);
                    im = System.Drawing.Image.FromStream(strm);
                    int height = im.Height;
                    int width = im.Width;
                    MemoryStream ostrm = new MemoryStream();

                    /* Replaced with high quality JPEG encoder
                      * 06122007AJM */
             					ImageFormat imf = ImageFormat.Jpeg;
             					//im.Save(ostrm, imf);
                    System.Drawing.Imaging.ImageCodecInfo[] info;
                    info = ImageCodecInfo.GetImageEncoders();
                    EncoderParameters encoderParameters;
                    encoderParameters = new EncoderParameters(1);
                    encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
                    System.Drawing.Imaging.ImageCodecInfo codec = null;
                    for (int i = 0; i < info.Length; i++)
                    {
                        if (info[i].FormatDescription == "JPEG")
                        {
                            codec = info[i];
                            break;
                        }
                    }
                    im.Save(ostrm, codec, encoderParameters);
                    // end change
                    byte[] ba = ostrm.ToArray();
                    ostrm.Close();
                    PageImage pi = new PageImage(imf, ba, width, height);
                    pi.SI = new StyleInfo();			// defaults are ok; don't want border, etc
                    // Set location, height and width
                    pi.X = x;
                    pi.Y = y;
                    pi.H = h;
                    pi.W = w;
                    pi.Sizing = bClip? ImageSizingEnum.Clip: ImageSizingEnum.FitProportional;

                    p.InsertObject(pi);
                }
                catch (Exception e)
                {
                    // image failed to load, continue processing
                    Console.WriteLine("Stamping image failed.  {0}", e.Message);
                }
                finally
                {
                    if (strm != null)
                        strm.Close();
                    if (im != null)
                        im.Dispose();
                }
                break;			// only stamp the first page
            }

            // Now create the PDF
            report.RunRenderPdf(sg, pgs);
        }