示例#1
0
        /// <summary>
        /// Exports the chart to the specified output stream as binary. When
        /// exporting to a web response the WriteToHttpResponse() method is likely
        /// preferred.
        /// </summary>
        /// <param name="outputStream">An output stream.</param>
        internal void WriteToStream(Stream outputStream)
        {
            switch (this.ContentType)
            {
            case "image/jpeg":
                CreateSvgDocument(this.Svg).Draw().Save(
                    outputStream,
                    ImageFormat.Jpeg);
                break;

            case "image/png":
                // PNG output requires a seekable stream.
                using (MemoryStream seekableStream = new MemoryStream())
                {
                    CreateSvgDocument(this.Svg).Draw().Save(
                        seekableStream,
                        ImageFormat.Png);
                    seekableStream.WriteTo(outputStream);
                }
                break;

            case "application/pdf":
                List <SvgDocument> svgDocs = CreateSvgDocuments(this.Svgs);

                // Create PDF document.
                using (Document pdfDoc = new Document())
                {
                    // Scalar to convert from 72 dpi to 150 dpi.
                    float dpiScalar = 150f / 72f;

                    // Set page size. Page dimensions are in 1/72nds of an inch.
                    // Page dimensions are scaled to boost dpi and keep page
                    // dimensions to a smaller size.
                    pdfDoc.SetPageSize(new Rectangle(
                                           svgDocs[0].Width / dpiScalar,
                                           svgDocs[0].Height / dpiScalar));

                    // Set margin to none.
                    pdfDoc.SetMargins(0f, 0f, 0f, 0f);

                    // Create PDF writer to write to response stream.
                    using (PdfWriter pdfWriter = PdfWriter.GetInstance(
                               pdfDoc,
                               outputStream))
                    {
                        // Configure PdfWriter.
                        pdfWriter.SetPdfVersion(PdfWriter.PDF_VERSION_1_5);
                        pdfWriter.CompressionLevel = PdfStream.DEFAULT_COMPRESSION;

                        // Add meta data.
                        pdfDoc.AddCreator(PdfMetaCreator);
                        pdfDoc.AddTitle(this.Name);

                        // Output PDF document.
                        pdfDoc.Open();
                        pdfDoc.NewPage();
                        foreach (SvgDocument svgDoc in svgDocs)
                        {
                            // Create image element from SVG image.
                            Image image = Image.GetInstance(svgDoc.Draw(), ImageFormat.Bmp);
                            image.CompressionLevel = PdfStream.DEFAULT_COMPRESSION;

                            // Must match scaling performed when setting page size.
                            image.ScalePercent(100f / dpiScalar);

                            // Add image element to PDF document.
                            pdfDoc.Add(image);
                        }
                        pdfDoc.CloseDocument();
                    }
                }

                break;

            case "image/svg+xml":
                using (StreamWriter writer = new StreamWriter(outputStream))
                {
                    writer.Write(string.Join(",", this.Svgs));
                    writer.Flush();
                }
                break;

            case "application/msword":
            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                WordGenerator.CreateDocStreamBySvgs(CreateSvgDocuments(this.Svgs), outputStream);
                break;
            //case "application/vnd.ms-powerpoint":

            //    break;
            case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
                PowerPointGenerator.CreatePowerPointXStreamBySvgs(CreateSvgDocuments(this.Svgs), outputStream);
                break;

            case "application/vnd.ms-excel":
                if (this.Table == null)
                {
                    ExcelGenerator.CreateExcelStreamBySvgs(CreateSvgDocuments(this.Svgs), outputStream);
                }
                else
                {
                    ExcelGenerator.CreateExcelWithTableStreamBySvg(CreateSvgDocument(this.Svg), outputStream, this.Table);
                }
                break;

            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                if (this.Table == null)
                {
                    ExcelGenerator.CreateExcelXStreamBySvgs(CreateSvgDocuments(this.Svgs), outputStream);
                }
                else
                {
                    ExcelGenerator.CreateExcelXWithTableStreamBySvg(CreateSvgDocument(this.Svg), outputStream, this.Table);
                }
                break;
                break;

            default:
                throw new InvalidOperationException(string.Format(
                                                        "ContentType '{0}' is invalid.", this.ContentType));
            }

            outputStream.Flush();
        }