internal PrintForm(FormInfo1 form) { this._Flags = form.Flags; this._Name = form.pName; this._Unit = PrintSystemUnit.ThousandthsOfMillimeter; var size = form.Size; this._Width = size.cx; this._Height = size.cy; var area = form.ImageableArea; this._LeftMargin = area.left; this._RightMargin = size.cx - area.right; this._TopMargin = area.top; this._BottomMargin = size.cy - area.bottom; }
/// <summary> /// 长度单位转换 /// </summary> /// <param name="value">长度数值</param> /// <param name="source">原始单位</param> /// <param name="target">目标单位</param> /// <returns></returns> /// <remarks> /// 个别情况转换可能存在误差, 例如: /// 1). System.Drawing.Printing.PaperSize; Unit = 0.01 in; 并且值类型为整数; /// A4 = { Width = 827, Height = 1169 }; /// 将单位转换为 0.001 mm; /// A4 = { Width = 210058, Height = 296926 }; 跟实际值不符: { Width = 210000, Height = 297000 }; /// /// 2). 反向计算 /// A4 = { Width = 210000, Height = 297000 } Unit: 0.001 mm; /// 将单位转换为 0.01 in; /// A4 = { Width = 826.77165354330714, Height = 1169.2913385826771 }; /// 调用 Math.Round(double) 之后: /// A4 = { Width = 827, Height = 1169 }; Unit: 0.01 in; /// </remarks> public static double Convert(double value, PrintSystemUnit source, PrintSystemUnit target) { switch (source) { case PrintSystemUnit.ThousandthsOfMillimeter: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 0.1); // ÷10; case PrintSystemUnit.TenthsOfMillimeter: return(value * 0.01); // ÷100; case PrintSystemUnit.Millimeter: return(value * 0.001); // ÷1000; case PrintSystemUnit.Centimeter: return(value * 0.0001); // ÷10000; case PrintSystemUnit.ThousandthsOfInch: return(value / 25.4); case PrintSystemUnit.HundredthsOfInch: return(value / 254d); case PrintSystemUnit.Inch: return(value / 25400d); } break; case PrintSystemUnit.HundredthsOfMillimeter: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 10d); case PrintSystemUnit.HundredthsOfMillimeter: return(value); case PrintSystemUnit.TenthsOfMillimeter: return(value * 0.1); // ÷10; case PrintSystemUnit.Millimeter: return(value * 0.01); // ÷100; case PrintSystemUnit.Centimeter: return(value * 0.0001); // ÷1000; case PrintSystemUnit.ThousandthsOfInch: return(value / 2.54); case PrintSystemUnit.HundredthsOfInch: return(value / 25.4); case PrintSystemUnit.Inch: return(value / 2540d); } break; case PrintSystemUnit.TenthsOfMillimeter: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 100d); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 10d); case PrintSystemUnit.TenthsOfMillimeter: return(value); case PrintSystemUnit.Millimeter: return(value * 0.1); // ÷10; case PrintSystemUnit.Centimeter: return(value * 0.01d); // ÷100; case PrintSystemUnit.ThousandthsOfInch: return(value / 0.254); case PrintSystemUnit.HundredthsOfInch: return(value / 2.54); case PrintSystemUnit.Inch: return(value / 254d); } break; case PrintSystemUnit.Millimeter: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 1000d); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 100d); case PrintSystemUnit.TenthsOfMillimeter: return(value * 10d); case PrintSystemUnit.Millimeter: return(value); case PrintSystemUnit.Centimeter: return(value * 0.1); // ÷10; case PrintSystemUnit.ThousandthsOfInch: return(value / 0.0254); case PrintSystemUnit.HundredthsOfInch: return(value / 0.254); case PrintSystemUnit.Inch: return(value / 25.4); } break; case PrintSystemUnit.Centimeter: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 10000d); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 1000d); case PrintSystemUnit.TenthsOfMillimeter: return(value * 100d); case PrintSystemUnit.Millimeter: return(value * 10d); case PrintSystemUnit.Centimeter: return(value); case PrintSystemUnit.ThousandthsOfInch: return(value / 0.00254); case PrintSystemUnit.HundredthsOfInch: return(value / 0.0254); case PrintSystemUnit.Inch: return(value / 2.54); } break; case PrintSystemUnit.ThousandthsOfInch: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 25.4); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 2.54); case PrintSystemUnit.TenthsOfMillimeter: return(value * 0.254); case PrintSystemUnit.Millimeter: return(value * 0.0254); case PrintSystemUnit.Centimeter: return(value * 0.00254); case PrintSystemUnit.ThousandthsOfInch: return(value); case PrintSystemUnit.HundredthsOfInch: return(value * 0.1); // ÷10; case PrintSystemUnit.Inch: return(value * 0.001); // ÷1000; } break; case PrintSystemUnit.HundredthsOfInch: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 254d); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 25.4); case PrintSystemUnit.TenthsOfMillimeter: return(value * 2.54); case PrintSystemUnit.Millimeter: return(value * 0.254); case PrintSystemUnit.Centimeter: return(value * 0.0254); case PrintSystemUnit.ThousandthsOfInch: return(value * 10d); case PrintSystemUnit.HundredthsOfInch: return(value); case PrintSystemUnit.Inch: return(value * 0.01); // ÷100; } break; case PrintSystemUnit.Inch: switch (target) { case PrintSystemUnit.ThousandthsOfMillimeter: return(value * 25400d); case PrintSystemUnit.HundredthsOfMillimeter: return(value * 2540d); case PrintSystemUnit.TenthsOfMillimeter: return(value * 254d); case PrintSystemUnit.Millimeter: return(value * 25.4); case PrintSystemUnit.Centimeter: return(value * 2.54); case PrintSystemUnit.ThousandthsOfInch: return(value * 1000d); case PrintSystemUnit.HundredthsOfInch: return(value * 100d); case PrintSystemUnit.Inch: return(value); } break; default: throw new ArgumentOutOfRangeException("source"); } throw new ArgumentOutOfRangeException("target"); }
public double GetHeight(PrintSystemUnit unit) { return(PrintSystemUnitConvert.Convert(this._Height, _Unit, unit)); }
public double GetWidth(PrintSystemUnit unit) { return(PrintSystemUnitConvert.Convert(this._Width, _Unit, unit)); }
static void AddOrUpdateForm(IntPtr hPrinter, string pFormName, double width, double height, PrintSystemUnit unit) { var level = 1; var o = Native.ThunkGetForm(hPrinter, pFormName, level); if (o != null) { var form = (FormInfo1)o; if (form.Flags == FormInfoFlags.Builtin) { throw new ArgumentException("内建规格不可修改"); } var w = (int)PrintSystemUnitConvert.Convert(width, unit, PrintSystemUnit.ThousandthsOfMillimeter); var h = (int)PrintSystemUnitConvert.Convert(height, unit, PrintSystemUnit.ThousandthsOfMillimeter); form.Size = new SIZE(w, h); form.ImageableArea = new RECT(0, 0, w, h); // Update Native.ThunkSetForm(hPrinter, pFormName, form); } else { var w = (int)PrintSystemUnitConvert.Convert(width, unit, PrintSystemUnit.ThousandthsOfMillimeter); var h = (int)PrintSystemUnitConvert.Convert(height, unit, PrintSystemUnit.ThousandthsOfMillimeter); var form = new FormInfo1() { Flags = FormInfoFlags.User, pName = pFormName, Size = new SIZE(w, h), ImageableArea = new RECT(0, 0, w, h), }; // Add Native.ThunkAddForm(hPrinter, form); } }
public static void AddFormOfServer(string pPrinterName, string pFormName, double width, double height, PrintSystemUnit unit) { var w = (int)PrintSystemUnitConvert.Convert(width, unit, PrintSystemUnit.ThousandthsOfMillimeter); var h = (int)PrintSystemUnitConvert.Convert(height, unit, PrintSystemUnit.ThousandthsOfMillimeter); AddFormOfServer(pPrinterName, pFormName, w, h); }
public static void AddOrUpdatePrinterForm(string pPrinterName, string pFormName, double width, double height, PrintSystemUnit unit) { PrinterDefaults pDefault = null; //if (pPrinterName != null) pDefault = new PrinterDefaults() { DesiredAccess = (int)PrintAccessRights.PrinterGenericExecute }; //else // String.Empty 将抛异常: 打印机名无效; //pDefault = new PrinterDefaults() { DesiredAccess = (int)PrintAccessRights.ServerGenericWrite }; var hPrinter = IntPtr.Zero; try { if (!Native.OpenPrinter(pPrinterName, out hPrinter, pDefault)) { Error.ThrowLastError(); } AddOrUpdateForm(hPrinter, pFormName, width, height, unit); } finally { Native.ClosePrinter(hPrinter); hPrinter = IntPtr.Zero; } }