/// <summary> /// print the page to PDF /// </summary> protected void PrintPage() { // first page? then we should store some settings if (CurrentPageNr == 0) { if (FMarginType == eMarginType.ePrintableArea) { // if no printer is installed, use default values FLeftMargin = 0; FTopMargin = 0.1f; FRightMargin = -0.1f; FBottomMargin = 0.1f; FWidth = 8.268333f; FHeight = 11.69333f; } // Calculate the number of lines per page. FLinesPerPage = (float)FHeight / (float)FXDefaultFont.GetHeight() * CurrentLineHeight; if (FNumberOfPages == 0) { // do a dry run without printing but calculate the number of pages StartSimulatePrinting(); int pageCounter = 0; FNumberOfPages = 1; do { pageCounter++; PrintPage(); } while (HasMorePages()); FinishSimulatePrinting(); BeginPrint(null, null); FNumberOfPages = pageCounter; } } CurrentPageNr++; CurrentYPos = FTopMargin; CurrentXPos = FLeftMargin; FPrinterLayout.PrintPageHeader(); float CurrentYPosBefore = CurrentYPos; float CurrentXPosBefore = CurrentXPos; FPrinterLayout.PrintPageBody(); if ((CurrentYPosBefore == CurrentYPos) && (CurrentXPosBefore == CurrentXPos) && HasMorePages()) { throw new Exception("failure printing, does not fit the page"); } FPrinterLayout.PrintPageFooter(); }
void BeginPrint(object ASender, PrintEventArgs AEv) { if ((FNumberOfPages == 0) && (CurrentPageNr != 0)) { FNumberOfPages = CurrentPageNr; } CurrentPageNr = 0; CurrentDocumentNr = 1; FPrinterLayout.StartPrintDocument(); }
/// <summary> /// store a pdf to file. will check for paper size using the PrinterLayout. By default A4 is chosen /// </summary> /// <param name="AFilename"></param> public void SavePDF(string AFilename) { PaperKind MyPaperKind; Margins MyMargins; float WidthInPoint; float HeightInPoint; if (FPrinterLayout.GetPageSize(out MyPaperKind, out MyMargins, out WidthInPoint, out HeightInPoint)) { SavePDF(AFilename, MyPaperKind, MyMargins, WidthInPoint, HeightInPoint); } else { SavePDF(AFilename, PaperKind.A4, new Margins(20, 20, 20, 39), -1, -1); } }
/// <summary> /// The PrintPage event is raised for each page to be printed. /// </summary> /// <returns>void</returns> protected virtual void PrintPage(Object ASender, PrintPageEventArgs AEv) { this.FEv = AEv; // ev.Graphics.PageUnit := GraphicsUnit.Point; twips // ev.Graphics.PageUnit := GraphicsUnit.Millimeter; // u := ev.Graphics.PageUnit; default world ??? FEv.Graphics.PageUnit = GraphicsUnit.Inch; FEv.Graphics.TranslateTransform(0, 0); // first page? then we should store some settings if (CurrentPageNr == 0) { if (FMarginType == eMarginType.ePrintableArea) { // if no printer is installed, use default values FLeftMargin = 0; FTopMargin = 0.1f; FRightMargin = -0.1f; FBottomMargin = 0.1f; FWidth = 8.268333f; FHeight = 11.69333f; try { // margin is set by the printing program, eg. HTML Renderer if (FEv.PageSettings.PrintableArea.Width != 0) { FLeftMargin = FEv.PageSettings.PrintableArea.Left / 100.0f; FTopMargin = FEv.PageSettings.PrintableArea.Top / 100.0f; FRightMargin = (FEv.PageSettings.PaperSize.Width - FEv.PageSettings.PrintableArea.Right) / 100.0f; FBottomMargin = (FEv.PageSettings.PaperSize.Height - FEv.PageSettings.PrintableArea.Bottom) / 100.0f; FWidth = FEv.PageSettings.PrintableArea.Width / 100.0f; FHeight = FEv.PageSettings.PrintableArea.Height / 100.0f; } } catch (Exception) { TLogging.Log("no printer"); } } else if (FMarginType == eMarginType.eDefaultMargins) { // prepare the margins here, this is used for the reporting printing FLeftMargin = FEv.MarginBounds.Left / 100.0f; FTopMargin = FEv.MarginBounds.Top / 100.0f; FRightMargin = FEv.MarginBounds.Right / 100.0f; FBottomMargin = FEv.MarginBounds.Bottom / 100.0f; FWidth = FEv.MarginBounds.Width / 100.0f; FHeight = FEv.MarginBounds.Height / 100.0f; if (FprintAction != PrintAction.PrintToPreview) // A "real printer" prints from the hard margin, but the preview has no hard margin. { FLeftMargin = 0; FTopMargin = 0; } } else if (FMarginType == eMarginType.eCalculatedMargins) { // the margins have been set in SetPageSize } FBlackPen = new Pen(Color.Black, Cm(0.05f)); // Calculate the number of lines per page. FLinesPerPage = (float)FHeight / (float)FDefaultFont.GetHeight(FEv.Graphics) * CurrentLineHeight; if (FNumberOfPages == 0) { // do a dry run without printing but calculate the number of pages StartSimulatePrinting(); int pageCounter = 0; FNumberOfPages = 1; do { pageCounter++; PrintPage(null, FEv); } while (HasMorePages()); FinishSimulatePrinting(); BeginPrint(null, null); FNumberOfPages = pageCounter; } } CurrentPageNr++; if (AEv.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (AEv.PageSettings.PrinterSettings.FromPage > CurrentPageNr) { CurrentPageNr = AEv.PageSettings.PrinterSettings.FromPage; CurrentDocumentNr = AEv.PageSettings.PrinterSettings.FromPage; } } CurrentYPos = FTopMargin; CurrentXPos = FLeftMargin; FPrinterLayout.PrintPageHeader(); float CurrentYPosBefore = CurrentYPos; float CurrentXPosBefore = CurrentXPos; FPrinterLayout.PrintPageBody(); if ((CurrentYPosBefore == CurrentYPos) && (CurrentXPosBefore == CurrentXPos) && FEv.HasMorePages) { throw new Exception("failure printing, does not fit the page"); } FPrinterLayout.PrintPageFooter(); if (AEv.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (CurrentPageNr == AEv.PageSettings.PrinterSettings.ToPage) { SetHasMorePages(false); return; } } }
private void SetPageSize() { PaperKind MyPaperKind; Margins MyMargins; float WidthInPoint; float HeightInPoint; if (FPrinterLayout.GetPageSize(out MyPaperKind, out MyMargins, out WidthInPoint, out HeightInPoint)) { FDocument.DefaultPageSettings.Margins = MyMargins; if (MyPaperKind == PaperKind.Custom) { // PaperSize: Height and Width in hundreds of an inch FDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", Convert.ToInt32(WidthInPoint / 72.0f * 100.0f), Convert.ToInt32(HeightInPoint / 72.0f * 100.0f)); FWidth = WidthInPoint / 72.0f * 100.0f; FHeight = HeightInPoint / 72.0f * 100.0f; FLeftMargin = MyMargins.Left; FTopMargin = MyMargins.Top; FRightMargin = MyMargins.Right; FBottomMargin = MyMargins.Bottom; } else { try { foreach (PaperSize pkSize in FDocument.PrinterSettings.PaperSizes) { if (pkSize.Kind == MyPaperKind) { FDocument.DefaultPageSettings.PaperSize = pkSize; if (FOrientation == eOrientation.ePortrait) { FWidth = pkSize.Width / 100.0f; FHeight = pkSize.Height / 100.0f; } else { FWidth = pkSize.Height / 100.0f; FHeight = pkSize.Width / 100.0f; } FLeftMargin = MyMargins.Left / 100.0f; FTopMargin = MyMargins.Top / 100.0f; FRightMargin = MyMargins.Right / 100.0f; FBottomMargin = MyMargins.Bottom / 100.0f; FWidth -= FLeftMargin + FRightMargin; FHeight -= FTopMargin + FBottomMargin; } } } catch (Exception) { // exception: no printers installed FDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", Convert.ToInt32(WidthInPoint / 72.0f * 100.0f), Convert.ToInt32(HeightInPoint / 72.0f * 100.0f)); FWidth = WidthInPoint / 72.0f * 100.0f; FHeight = HeightInPoint / 72.0f * 100.0f; FLeftMargin = MyMargins.Left; FTopMargin = MyMargins.Top; FRightMargin = MyMargins.Right; FBottomMargin = MyMargins.Bottom; } } FMarginType = eMarginType.eCalculatedMargins; } if (FHeight < 0) { throw new Exception("TGfxPrinter.SetPageSize: invalid paper size, height is negative"); } }
/// <summary> /// The PrintPage event is raised for each page to be printed. /// </summary> /// <returns>void</returns> protected virtual void PrintPage(Object ASender, PrintPageEventArgs AEv) { this.FEv = AEv; // ev.Graphics.PageUnit := GraphicsUnit.Point; twips // ev.Graphics.PageUnit := GraphicsUnit.Millimeter; // u := ev.Graphics.PageUnit; default world ??? FEv.Graphics.PageUnit = GraphicsUnit.Inch; FEv.Graphics.TranslateTransform(0, 0); // first page? then we should store some settings if ((CurrentPageNr == 0) && (ASender != null)) { if (FMarginType == eMarginType.ePrintableArea) { // if no printer is installed, use default values FLeftMargin = 0; FTopMargin = 0.1f; FRightMargin = -0.1f; FBottomMargin = 0.1f; FWidth = 8.268333f; FHeight = 11.69333f; try { // margin is set by the printing program, eg. HTML Renderer if (FEv.PageSettings.PrintableArea.Width != 0) { FLeftMargin = FEv.PageSettings.PrintableArea.Left / 100.0f; FTopMargin = FEv.PageSettings.PrintableArea.Top / 100.0f; FRightMargin = (FEv.PageSettings.PaperSize.Width - FEv.PageSettings.PrintableArea.Right) / 100.0f; FBottomMargin = (FEv.PageSettings.PaperSize.Height - FEv.PageSettings.PrintableArea.Bottom) / 100.0f; FWidth = FEv.PageSettings.PrintableArea.Width / 100.0f; FHeight = FEv.PageSettings.PrintableArea.Height / 100.0f; } } catch (Exception) { TLogging.Log("no printer"); } } else if (FMarginType == eMarginType.eDefaultMargins) { // prepare the margins here, this is used for the reporting printing FLeftMargin = FEv.MarginBounds.Left / 100.0f; FTopMargin = FEv.MarginBounds.Top / 100.0f; FRightMargin = FEv.MarginBounds.Right / 100.0f; FBottomMargin = FEv.MarginBounds.Bottom / 100.0f; FWidth = FEv.MarginBounds.Width / 100.0f; FHeight = FEv.MarginBounds.Height / 100.0f; if (FprintAction != PrintAction.PrintToPreview) // A "real printer" prints from the hard margin, but the preview has no hard margin. { FLeftMargin = 0; FTopMargin = 0; } } else if (FMarginType == eMarginType.eCalculatedMargins) { // the margins have been set in SetPageSize } FBlackPen = new Pen(Color.Black, Cm(0.05f)); // Calculate the number of lines per page. FLinesPerPage = (float)FHeight / (float)FDefaultFont.GetHeight(FEv.Graphics) * CurrentLineHeight; if (FNumberOfPages == 0) { // do a dry run without printing but calculate the number of pages StartSimulatePrinting(); int pageCounter = 0; FNumberOfPages = 1; do { pageCounter++; PrintPage(null, FEv); } while (HasMorePages()); FinishSimulatePrinting(); BeginPrint(null, null); FNumberOfPages = pageCounter; } } if (AEv.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { // Do we need to skip some pages at the start?? if ((ASender != null) && (CurrentPageNr < AEv.PageSettings.PrinterSettings.FromPage - 1)) { // We are going to make recursive calls to this method to 'print' the pages we need to skip. // But we don't want to call this paragraph of code again when we come back. // We coming in here the first time because ASender is not null and there are pages to skip // While skipping, ASender will be null so we won't come back // After skipping, while printing real pages again, ASender will be non-null but the page number will be 'too high' PrintingMode = ePrintingMode.eDoSimulate; while (CurrentPageNr < AEv.PageSettings.PrinterSettings.FromPage - 1) { PrintPage(null, FEv); } // Now restore the real print mode. Now we are ready to continue with the first piece of paper and with real printing. // All the TPrinterState properties will be correct because we haven't pushed/popped anything onto the stack. PrintingMode = ePrintingMode.eDoPrint; } } CurrentPageNr++; CurrentYPos = FTopMargin; CurrentXPos = FLeftMargin; FPrinterLayout.PrintPageHeader(); float CurrentYPosBefore = CurrentYPos; float CurrentXPosBefore = CurrentXPos; FPrinterLayout.PrintPageBody(); if ((CurrentYPosBefore == CurrentYPos) && (CurrentXPosBefore == CurrentXPos) && FEv.HasMorePages) { throw new Exception("failure printing, does not fit the page"); } FPrinterLayout.PrintPageFooter(); if (AEv.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (CurrentPageNr == AEv.PageSettings.PrinterSettings.ToPage) { SetHasMorePages(false); return; } } }