Exemplo n.º 1
0
        // Create a single PDF file
        void CreateOnePdfFile(string fileName, IEnumerable <CourseDesignator> courseDesignators)
        {
            List <CoursePage> pages     = LayoutPages(courseDesignators);
            PdfWriter         pdfWriter = new PdfWriter(Path.GetFileNameWithoutExtension(fileName), coursePdfSettings.ColorModel == ColorModel.CMYK);

            foreach (CoursePage page in pages)
            {
                CoursePage pageToDraw = page;

                SizeF paperSize = new SizeF(pageToDraw.paperSize.Width / 100F, pageToDraw.paperSize.Height / 100F);
                if (pageToDraw.landscape)
                {
                    paperSize = new SizeF(paperSize.Height, paperSize.Width);
                }

                if (controller.UpdateProgressDialog(string.Format(MiscText.CreatingFile, Path.GetFileName(fileName)), (double)currentPage / (double)totalPages))
                {
                    throw new Exception(MiscText.CancelledByUser);
                }

                IGraphicsTarget grTarget;
                PdfImporter     pdfImporter = null;

                if (IsPdfMap)
                {
                    // We need to re-obtain a PdfImporter every time, or else very strange bugs start to crop up.

                    pdfImporter = new PdfImporter(sourcePdfMapFileName);

                    float scaleRatio = CourseView.CreatePrintingCourseView(eventDB, page.courseDesignator).ScaleRatio;
                    if (scaleRatio == 1.0)
                    {
                        // If we're doing a PDF at scale 1, we just copy the page directly.
                        grTarget   = pdfWriter.BeginCopiedPage(pdfImporter, 0);
                        pageToDraw = PdfNonScaledPage(page.courseDesignator);
                    }
                    else
                    {
                        Matrix     transform = Geometry.CreateInvertedRectangleTransform(page.printRectangle, page.mapRectangle);
                        RectangleF printedPortionInMapCoords = Geometry.TransformRectangle(transform, new RectangleF(0, 0, paperSize.Width * 100F, paperSize.Height * 100F));
                        RectangleF printedPortionInInches    = new RectangleF(
                            Geometry.InchesFromMm(printedPortionInMapCoords.Left),
                            Geometry.InchesFromMm(mapBounds.Height - printedPortionInMapCoords.Bottom),
                            Geometry.InchesFromMm(printedPortionInMapCoords.Width),
                            Geometry.InchesFromMm(printedPortionInMapCoords.Height));

                        grTarget = pdfWriter.BeginCopiedPartialPage(pdfImporter, 0, paperSize, printedPortionInInches);
                    }

                    // Don't draw the map normally.
                    mapDisplay.SetMapFile(MapType.None, null);
                }
                else
                {
                    grTarget = pdfWriter.BeginPage(paperSize);
                }

                DrawPage(grTarget, pageToDraw);
                pdfWriter.EndPage(grTarget);
                grTarget.Dispose();

                if (pdfImporter != null)
                {
                    pdfImporter.Dispose();
                    pdfImporter = null;
                }

                currentPage += 1;
            }

            pdfWriter.Save(fileName);
        }
Exemplo n.º 2
0
        // Create a single PDF file
        void CreateOnePdfFile(string fileName, IEnumerable<CourseDesignator> courseDesignators)
        {
            List<CoursePage> pages = LayoutPages(courseDesignators);
            PdfWriter pdfWriter = new PdfWriter(Path.GetFileNameWithoutExtension(fileName), coursePdfSettings.ColorModel == ColorModel.CMYK);

            foreach (CoursePage page in pages) {
                CoursePage pageToDraw = page;

                SizeF paperSize = new SizeF(pageToDraw.paperSize.Width / 100F, pageToDraw.paperSize.Height / 100F);
                if (pageToDraw.landscape)
                    paperSize = new SizeF(paperSize.Height, paperSize.Width);

                if (controller.UpdateProgressDialog(string.Format(MiscText.CreatingFile, Path.GetFileName(fileName)), (double)currentPage / (double)totalPages))
                    throw new Exception(MiscText.CancelledByUser);

                IGraphicsTarget grTarget;

                if (IsPdfMap) {
                    float scaleRatio = CourseView.CreatePrintingCourseView(eventDB, page.courseDesignator).ScaleRatio;
                    if (scaleRatio == 1.0) {
                        // If we're doing a PDF at scale 1, we just copy the page directly.
                        grTarget = pdfWriter.BeginCopiedPage(pdfMapPage);
                        pageToDraw = PdfNonScaledPage(page.courseDesignator);
                    }
                    else {
                        using (XForm xForm = pdfImporter.GetXForm(0)) {
                            RectangleF pageArea;
                            if (page.landscape)
                                pageArea = new RectangleF(0, 0, Geometry.HundredthsInchesFromMm(mapBounds.Height), Geometry.HundredthsInchesFromMm(mapBounds.Width));
                            else
                                pageArea = new RectangleF(0, 0, Geometry.HundredthsInchesFromMm(mapBounds.Width), Geometry.HundredthsInchesFromMm(mapBounds.Height));

                            Matrix transform = Geometry.CreateInvertedRectangleTransform(page.printRectangle, page.mapRectangle);
                            RectangleF printedPortionInMapCoords = Geometry.TransformRectangle(transform, pageArea);
                            Matrix mapToPortraitPage = Geometry.CreateInvertedRectangleTransform(mapBounds, new RectangleF(0, 0, page.paperSize.Width / 100F, page.paperSize.Height / 100F));
                            RectangleF sourcePartialRectInInches = Geometry.TransformRectangle(mapToPortraitPage, printedPortionInMapCoords);
                            grTarget = pdfWriter.BeginCopiedPartialPage(xForm, paperSize, sourcePartialRectInInches);
                        }
                    }

                    // Don't draw the map normally.
                    mapDisplay.SetMapFile(MapType.None, null);
                }
                else {
                    grTarget = pdfWriter.BeginPage(paperSize);
                }

                DrawPage(grTarget, pageToDraw);
                pdfWriter.EndPage(grTarget);
                grTarget.Dispose();

                currentPage += 1;
            }

            pdfWriter.Save(fileName);
        }