private void btn_fis_Click(object sender, EventArgs e) { printDocument1.DocumentName = "Fis Sureti"; //printDialog1.Document = printDocument1; //if (printDialog1.ShowDialog() == DialogResult.OK) //{ // makePrintStr(); // printDocument1.Print(); //} makePrintStr(); System.Drawing.Printing.PaperSize ps = new System.Drawing.Printing.PaperSize("new", 400, PublicVariables.kagit_boyu); System.Drawing.Printing.PrinterResolution pr = new System.Drawing.Printing.PrinterResolution(); pr.Kind = System.Drawing.Printing.PrinterResolutionKind.Draft; printDocument1.DefaultPageSettings.PaperSize = ps; printDocument1.DefaultPageSettings.PrinterResolution = pr; printDocument1.Print(); //string s = RawPrinterHelper.GetDefaultPrinterName(); //RawPrinterHelper.SendStringToPrinter(s, print_str); if (durum == "G") { this.Close(); } else { } }
/// <summary> /// Get total count of pages. Set any local page-size related values (e.g. linesPerPage) /// </summary> /// <param name="e"></param> /// <returns></returns> public override async Task <int> RenderAsync(System.Drawing.Printing.PrinterResolution printerResolution) { Helpers.Logging.TraceMessage("HtmlFileContent.RenderAsync()"); await base.RenderAsync(printerResolution); int width = (int)PageSize.Width; // (printerResolution.X * PageSize.Width / 100); int height = (int)PageSize.Height; // (printerResolution.Y * PageSize.Height / 100); Helpers.Logging.TraceMessage($"HtmlFileContent.RenderAsync - Page size: {width}x{height} @ {printerResolution.X}x{printerResolution.Y} dpi"); string css; try { // TODO: Make sure wiprint.css is in the same dir as .config file once setup is impl using StreamReader cssStream = new StreamReader("winprint.css"); css = await cssStream.ReadToEndAsync(); cssStream.Close(); } catch { css = IncludedWinPrintCss.CssString; } var resources = new HtmlResources(filePath); litehtml = new GDIPlusContainer(css, resources.GetResourceString, resources.GetResourceBytes); litehtml.Size = new LiteHtmlSize(width, 0); litehtml.PageHeight = height; htmlBitmap = new Bitmap(width, height); //htmlBitmap.SetResolution(printerResolution.X, printerResolution.Y); var g = Graphics.FromImage(htmlBitmap); g.PageUnit = GraphicsUnit.Display; //g.FillRectangle(Brushes.LightYellow, new Rectangle(0, 0, width, height)); Helpers.Logging.TraceMessage($"HtmlFileContent.RenderAsync() Graphic is {htmlBitmap.Width}x{htmlBitmap.Height} @ {g.DpiX}x{g.DpiY} dpi. PageUnit = {g.PageUnit.ToString()}"); litehtml.Graphics = g; Helpers.Logging.TraceMessage($"PageUnit = {g.PageUnit.ToString()}"); litehtml.Document.CreateFromString(document); litehtml.Document.OnMediaChanged(); // TODO: Use return of Render() to get "best width" int bestWidth = litehtml.Document.Render((int)width); //litehtml.SetViewport(new LiteHtmlPoint(0, 0), new LiteHtmlSize(width, height)); //litehtml.Draw(); litehtml.Graphics = null; Helpers.Logging.TraceMessage($"Litehtml_DocumentSizeKnown {litehtml.Document.Width()}x{litehtml.Document.Height()} bestWidth = {bestWidth}"); var n = (int)(litehtml.Document.Height() / height) + 1; Helpers.Logging.TraceMessage($"HtmlFileContent.RenderAsync - {n} pages."); return(n); }
private void print01_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Bitmap _NewBitmap = new Bitmap(this.pan_design.Width, pan_design.Height); //_NewBitmap.SetResolution(203, 203); pan_design.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height)); System.Drawing.Printing.PrinterResolution prnres = new System.Drawing.Printing.PrinterResolution(); int iiix = print01.PrinterSettings.DefaultPageSettings.PrinterResolution.X; int iiiy = print01.PrinterSettings.DefaultPageSettings.PrinterResolution.Y; if (iiix != this.DesignFormData01.DPI) { MessageBox.Show("对不起!当前默认打印机分辨率和配置分辨率不符,肯能会导致打印失真。"); } //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; e.Graphics.DrawImage(_NewBitmap, 0, 0, dpiX * DesignFormData01.PrintWidth / (float)25.4, dpiY * DesignFormData01.PrintHeight / (float)25.4); }
public void SetHdevmode(IntPtr hdevmode) { IntSecurity.AllPrintingAndUnmanagedCode.Demand(); if (hdevmode == IntPtr.Zero) { throw new ArgumentException(System.Drawing.SR.GetString("InvalidPrinterHandle", new object[] { hdevmode })); } SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE)UnsafeNativeMethods.PtrToStructure(SafeNativeMethods.GlobalLock(new HandleRef(null, hdevmode)), typeof(SafeNativeMethods.DEVMODE)); if ((mode.dmFields & 0x800) == 0x800) { this.color = mode.dmColor == 2; } if ((mode.dmFields & 1) == 1) { this.landscape = mode.dmOrientation == 2; } this.paperSize = this.PaperSizeFromMode(mode); this.paperSource = this.PaperSourceFromMode(mode); this.printerResolution = this.PrinterResolutionFromMode(mode); SafeNativeMethods.GlobalUnlock(new HandleRef(null, hdevmode)); }
/// <summary> /// Get total count of pages. Set any local page-size related values (e.g. linesPerPage) /// </summary> /// <param name="e"></param> /// <returns></returns> public override async Task <int> RenderAsync(System.Drawing.Printing.PrinterResolution printerResolution, EventHandler <string> reflowProgress) { LogService.TraceMessage(); _ready = false; reflowProgress?.Invoke(this, "HtmlFileContent.RenderAsync"); await base.RenderAsync(printerResolution, reflowProgress).ConfigureAwait(false); var width = (int)PageSize.Width; // (printerResolution.X * PageSize.Width / 100); var height = (int)PageSize.Height; // (printerResolution.Y * PageSize.Height / 100); LogService.TraceMessage($"HtmlFileContent.RenderAsync - Page size: {width}x{height} @ {printerResolution.X}x{printerResolution.Y} dpi"); string css; try { // TODO: Make sure winprint.css is in the same dir as .config file once setup is impl using var cssStream = new StreamReader("winprint.css"); css = await cssStream.ReadToEndAsync(); cssStream.Close(); reflowProgress?.Invoke(this, "Read winprint.css"); } catch { css = IncludedWinPrintCss.CssString; } // BUGBUG: wihtout knowing the relative root path fo the html document we can't load any resources var resources = new HtmlResources(""); _litehtml = new GDIPlusContainer(css, resources.GetResourceString, resources.GetResourceBytes) { Diagnostics = ContentSettings.Diagnostics, Size = new LiteHtmlSize(width, 0), PageHeight = height }; _htmlBitmap = new Bitmap(width, height); //htmlBitmap.SetResolution(printerResolution.X, printerResolution.Y); var g = Graphics.FromImage(_htmlBitmap); g.PageUnit = GraphicsUnit.Display; g.TextRenderingHint = ContentTypeEngineBase.TextRenderingHint; //g.FillRectangle(Brushes.LightYellow, new Rectangle(0, 0, width, height)); LogService.TraceMessage($"HtmlFileContent.RenderAsync() Graphic is {_htmlBitmap.Width} x {_htmlBitmap.Height} @ {g.DpiX} x {g.DpiY} dpi. PageUnit = {g.PageUnit.ToString()}"); _litehtml.Graphics = g; _litehtml.StringFormat = ContentTypeEngineBase.StringFormat; _litehtml.Grayscale = ContentSettings.Grayscale; _litehtml.Darkness = ContentSettings.Darkness; _litehtml.PrintBackground = ContentSettings.PrintBackground; //Logging.TraceMessage("litehtml.Document.CreateFromString(document)"); reflowProgress?.Invoke(this, "litehtml.Document.CreateFromString(document)"); _litehtml.Document.CreateFromString(Document); //Logging.TraceMessage("back from litehtml.Document.CreateFromString(document)"); reflowProgress?.Invoke(this, "back from litehtml.Document.CreateFromString(document)"); _litehtml.Document.OnMediaChanged(); // TODO: Use return of Render() to get "best width" var bestWidth = _litehtml.Document.Render(width); reflowProgress?.Invoke(this, "Done with Render"); // Note, setting viewport does nothing //litehtml.SetViewport(new LiteHtmlPoint(0, 0), new LiteHtmlSize(width, height)); _litehtml.Graphics = null; //Logging.TraceMessage($"Litehtml_DocumentSizeKnown {_litehtml.Document.Width()}x{_litehtml.Document.Height()} bestWidth = {bestWidth}"); var n = _litehtml.Document.Height() / height + 1; Logging.TraceMessage($"HtmlFileContent.RenderAsync - {n} pages."); _ready = true; return(n); }
public void SetHdevmode(IntPtr hdevmode) { IntSecurity.AllPrintingAndUnmanagedCode.Demand(); if (hdevmode == IntPtr.Zero) { throw new ArgumentException(System.Drawing.SR.GetString("InvalidPrinterHandle", new object[] { hdevmode })); } SafeNativeMethods.DEVMODE mode = (SafeNativeMethods.DEVMODE) UnsafeNativeMethods.PtrToStructure(SafeNativeMethods.GlobalLock(new HandleRef(null, hdevmode)), typeof(SafeNativeMethods.DEVMODE)); if ((mode.dmFields & 0x800) == 0x800) { this.color = mode.dmColor == 2; } if ((mode.dmFields & 1) == 1) { this.landscape = mode.dmOrientation == 2; } this.paperSize = this.PaperSizeFromMode(mode); this.paperSource = this.PaperSourceFromMode(mode); this.printerResolution = this.PrinterResolutionFromMode(mode); SafeNativeMethods.GlobalUnlock(new HandleRef(null, hdevmode)); }
/// <summary> /// Get total count of pages. Set any local page-size related values (e.g. linesPerPage). /// </summary> /// <param name="e"></param> /// <returns></returns> public override async Task <int> RenderAsync(System.Drawing.Printing.PrinterResolution printerResolution, EventHandler <string> reflowProgress) { LogService.TraceMessage(); if (Document == null) { throw new ArgumentNullException("document can't be null for Render"); } var dpiX = printerResolution.X; var dpiY = printerResolution.Y; // BUGBUG: On Windows we can use the printer's resolution to be more accurate. But on Linux we // have to use 96dpi. See https://github.com/mono/libgdiplus/issues/623, etc... if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || dpiX < 0 || dpiY < 0) { dpiX = dpiY = 96; } // Create a representative Graphcis used for determining glyph metrics. using var bitmap = new Bitmap(1, 1); bitmap.SetResolution(dpiX, dpiY); var g = Graphics.FromImage(bitmap); g.PageUnit = GraphicsUnit.Display; // Display is 1/100th" // Calculate the number of lines per page; first we need our font. Keep it around. _cachedFont = new System.Drawing.Font(ContentSettings.Font.Family, ContentSettings.Font.Size / 72F * 96, ContentSettings.Font.Style, GraphicsUnit.Pixel); // World? Log.Debug("Font: {f}, {s} ({p}), {st}", _cachedFont.Name, _cachedFont.Size, _cachedFont.SizeInPoints, _cachedFont.Style); if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { _cachedFont.Dispose(); _cachedFont = new System.Drawing.Font(ContentSettings.Font.Family, ContentSettings.Font.Size, ContentSettings.Font.Style, GraphicsUnit.Point); Log.Debug("Font: {f}, {s} ({p}), {st}", _cachedFont.Name, _cachedFont.Size, _cachedFont.SizeInPoints, _cachedFont.Style); g.PageUnit = GraphicsUnit.Display; // Display is 1/100th" } _lineHeight = _cachedFont.GetHeight(dpiY); if (PageSize.Height < _lineHeight) { throw new InvalidOperationException("The line height is greater than page height."); } // Round down # of lines per page to ensure lines don't clip on bottom _linesPerPage = (int)Math.Floor(PageSize.Height / _lineHeight); // 3 digits + 1 wide - Will support 999 lines before line numbers start to not fit // TODO: Make line number width dynamic // Note, MeasureString is actually dependent on lineNumberWidth! lineNumberWidth = ContentSettings.LineNumbers ? MeasureString(g, new string('0', 4)).Width : 0; // This is the shortest line length (in chars) that we think we'll see. // This is used as a performance optimization (probably premature) and // could be 0 with no functional change. _minLineLen = (int)((PageSize.Width - lineNumberWidth) / MeasureString(g, "W").Width); // Note, MeasureLines may increment numPages due to form feeds and line wrapping _wrappedLines = LineWrapDocument(g, Document); // new List<string>(); var n = (int)Math.Ceiling(_wrappedLines.Count / (double)_linesPerPage); Log.Debug("Rendered {pages} pages of {linesperpage} lines per page, for a total of {lines} lines.", n, _linesPerPage, _wrappedLines.Count); return(await Task.FromResult(n)); }