Ejemplo n.º 1
0
    public void ProcessRequest(HttpContext context)
    {
        HttpResponse response = context.Response;

        response.Clear();

        AppState appState      = AppState.FromJson(context.Request.Form["state"]);
        string   templateID    = context.Request.Form["template"];
        string   scaleMode     = context.Request.Form["scalemode"];
        double   originalWidth = Convert.ToDouble(context.Request.Form["width"]);

        // if the user entered a feet-per-inch scale, compute the pixel width of the map
        // for the scale given the extent width

        if (scaleMode == "input")
        {
            double extentWidth = appState.Extent.Width * (AppContext.AppSettings.MapUnits == "feet" ? 1 : Constants.FeetPerMeter);
            double scale       = Convert.ToDouble(context.Request.Form["scale"]);

            originalWidth = extentWidth * 96 / scale;
            scaleMode     = "scale";
        }

        PreserveMode preserveMode = (PreserveMode)Enum.Parse(typeof(PreserveMode), scaleMode, true);

        // read in the user inputs

        List <String> input = new List <String>();

        Configuration.ApplicationRow   application = AppContext.GetConfiguration().Application.First(o => o.ApplicationID == appState.Application);
        Configuration.PrintTemplateRow template    = application.GetPrintTemplates().First(o => o.TemplateID == templateID);

        foreach (Configuration.PrintTemplateContentRow element in template.GetPrintTemplateContentRows().Where(o => o.ContentType == "input"))
        {
            string fieldName = String.Format("input_{0}_{1}", template.TemplateID, element.SequenceNo);
            input.Add(context.Request.Form[fieldName]);
        }

        // produce the PDF output

        PdfMap pdfMap = new PdfMap(appState, templateID, input, preserveMode, originalWidth);

        pdfMap.Write(response);
        response.End();
    }
Ejemplo n.º 2
0
    public void Write(HttpResponse response, bool inline)
    {
        response.Clear();
        response.ContentType = "application/pdf";
        response.AddHeader("Content-Disposition", (inline ? "inline" : "attachment") + "; filename=Map.pdf");

        // create the PDF document

        Configuration config = AppContext.GetConfiguration();

        Configuration.PrintTemplateRow printTemplate = config.PrintTemplate.First(o => o.TemplateID == _templateId);

        float pageWidth  = Convert.ToSingle(printTemplate.PageWidth * PointsPerInch);
        float pageHeight = Convert.ToSingle(printTemplate.PageHeight * PointsPerInch);

        Rectangle pageSize = new Rectangle(pageWidth, pageHeight);

        pageSize.BackgroundColor = new Color(System.Drawing.Color.White);
        Document document = new Document(pageSize);

        PdfWriter writer = PdfWriter.GetInstance(document, response.OutputStream);

        document.Open();
        PdfContentByte content = writer.DirectContent;

        // get the extent of the main map and fit it to the proportions of
        // the map box on the page

        double mapScale = 0;

        Configuration.PrintTemplateContentRow mapElement = printTemplate.GetPrintTemplateContentRows().FirstOrDefault(o => o.ContentType == "map");

        if (mapElement != null)
        {
            if (_preserveMode == PreserveMode.Extent)
            {
                _appState.Extent.Reaspect(mapElement.Width, mapElement.Height);
            }
            else
            {
                IPoint c = new Point(_appState.Extent.Centre);

                double dx;
                double dy;

                if (_preserveMode == PreserveMode.Scale)
                {
                    double ratio = _appState.Extent.Width * 96 / _originalWidth;
                    dx = mapElement.Width * ratio * 0.5;
                    dy = mapElement.Height * ratio * 0.5;
                }
                else
                {
                    dx = _appState.Extent.Width * 0.5;
                    dy = dx * mapElement.Height / mapElement.Width;
                }

                _appState.Extent = new Envelope(new Coordinate(c.Coordinate.X - dx, c.Coordinate.Y - dy), new Coordinate(c.Coordinate.X + dx, c.Coordinate.Y + dy));
            }

            double conversion = AppContext.AppSettings.MapUnits == "feet" ? 1 : Constants.FeetPerMeter;
            mapScale = _appState.Extent.Width * conversion / mapElement.Width;

            _pixelSize = _appState.Extent.Width / (mapElement.Width * PixelsPerInch);
        }

        int inputIndex = 0;

        // get the page template elements and draw each one to the page

        foreach (Configuration.PrintTemplateContentRow element in printTemplate.GetPrintTemplateContentRows())
        {
            switch (element.ContentType)
            {
            case "box":
                CreatePdfBox(content, element);
                break;

            case "date":
                CreatePdfText(content, element, DateTime.Now.ToString("MMMM d, yyyy"));
                break;

            case "image":
                CreatePdfImage(content, element);
                break;

            case "legend":
                CreatePdfLegend(content, element);
                break;

            case "map":
                CreatePdfMap(content, element);
                break;

            case "overviewmap":
                CreatePdfOverviewMap(content, element);
                break;

            case "scale":
                if (mapScale > 0)
                {
                    CreatePdfText(content, element, "1\" = " + mapScale.ToString("0") + " ft");
                }
                break;

            case "scalefeet":
                if (mapScale > 0)
                {
                    CreatePdfText(content, element, mapScale.ToString("0") + " ft");
                }
                break;

            case "tabdata":
                CreatePdfTabData(content, element);
                break;

            case "text":
                if (!element.IsTextNull())
                {
                    CreatePdfText(content, element, element.Text);
                }
                else if (!element.IsFileNameNull())
                {
                    string fileName = HttpContext.Current.Server.MapPath("Text/Print") + "\\" + element.FileName;

                    if (File.Exists(fileName))
                    {
                        string text = File.ReadAllText(fileName);
                        CreatePdfText(content, element, text);
                    }
                }
                break;

            case "input":
                if (inputIndex < _input.Count)
                {
                    CreatePdfText(content, element, _input[inputIndex]);
                    ++inputIndex;
                }
                break;
            }
        }

        document.Close();
        response.End();
    }