public void PrintToPdf(string pathName, bool cmykMode) { PdfWriter pdfWriter = new PdfWriter(Path.GetFileNameWithoutExtension(pathName), cmykMode); // Set up and position everything. printPreviewInProgress = false; printingToBitmaps = false; SetupPrinting(); PrintEventArgs printArgs = new PrintEventArgs(); BeginPrint(this, printArgs); while (currentPage < totalPages) { // Set the page settings. QueryPageSettingsEventArgs queryPageSettingsArgs = new QueryPageSettingsEventArgs(pageSettings); QueryPageSettings(this, queryPageSettingsArgs); Size pageSize = pageSettings.Bounds.Size; SizeF paperSizeInInches = new SizeF(pageSize.Width / 100F, pageSize.Height / 100F); Rectangle pageBounds = pageSettings.Bounds; Rectangle marginBounds = Rectangle.FromLTRB(pageBounds.Left + pageSettings.Margins.Left, pageBounds.Top + pageSettings.Margins.Top, pageBounds.Right - pageSettings.Margins.Right, pageBounds.Bottom - pageSettings.Margins.Bottom); float dpi = 1200; // Make a PDF high resolution, although this is unlikely to matter much. // create and print a page. using (IGraphicsTarget grTarget = pdfWriter.BeginPage(paperSizeInInches)) { // Move the origin of the graphics to the margin boundaries. Matrix translateTransform = new Matrix(); translateTransform.Translate(marginBounds.Left, marginBounds.Top); grTarget.PushTransform(translateTransform); SizeF size = new SizeF(marginBounds.Width, marginBounds.Height); DrawPage(grTarget, currentPage, size, dpi); grTarget.PopTransform(); } ++currentPage; } EndPrint(this, printArgs); pdfWriter.Save(pathName); }
// 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); }
// 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); }