using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace PrinterPageSize
{
///
/// 自定义打印纸类型和尺寸
///
class MyPageSize
{
//调用方法
//float width = 2100;
//float height = 1600;
//PrintDocument printDocument1 = new PrintDocument();
//MyPageSize ps = new MyPageSize();
//string defaultPrinterName = printDocument1.PrinterSettings.PrinterName; //默认打印机名称
//string myPageSizeName = "MyCustomePageSize";
//ps.SetPrintForm(defaultPrinterName, myPageSizeName, width, height);
//PaperSize ps1 = ps.GetPrintForm(defaultPrinterName, myPageSizeName);
//printDocument1.DefaultPageSettings.PaperSize = ps1;
///
/// 获取打印纸类型
///
/// 打印机名称
/// 纸张类型名称
/// 纸张类型
public PaperSize GetPrintForm(string printerName, string paperName)
{
PaperSize paper = null;
PrinterSettings printer = new System.Drawing.Printing.PrinterSettings();
printer.PrinterName = printerName;
foreach (System.Drawing.Printing.PaperSize ps in printer.PaperSizes)
{
if (ps.PaperName.ToLower() == paperName.ToLower())
{
paper = ps;
break;
}
}
return paper;
}
///
/// 设置打印纸类型的尺寸,如果没有参数提供的类型则新建一个类型,
///
/// 打印机名称
/// 打印纸类型名称
/// 宽度,单位0.1毫米,需要在打印机允许的尺寸之内,否则按照默认打印纸类型打印
/// 高度,单位0.1毫米,需要在打印机允许的尺寸之内,否则按照默认打印纸类型打印
public void SetPrintForm(string printerName, string paperName, float width, float height)
{
if (PlatformID.Win32NT == Environment.OSVersion.Platform)
{
const int PRINTER_ACCESS_USE = 0x00000008;
const int PRINTER_ACCESS_ADMINISTER = 0x00000004;
structPrinterDefaults defaults = new structPrinterDefaults();
defaults.pDatatype = null;
defaults.pDevMode = IntPtr.Zero;
defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;
IntPtr hPrinter = IntPtr.Zero;
if (OpenPrinter(printerName, out hPrinter, ref defaults))
{
try
{
structFormInfo1 formInfo = new structFormInfo1();
formInfo.Flags = 0;
formInfo.pName = paperName;
formInfo.Size.width = (int)(width * 100.0);
formInfo.Size.height = (int)(height * 100.0);
formInfo.ImageableArea.left = 0;
formInfo.ImageableArea.right = formInfo.Size.width;
formInfo.ImageableArea.top = 0;
formInfo.ImageableArea.bottom = formInfo.Size.height;
bool rslt = false;
if (GetPrintForm(printerName, paperName) != null)
{
rslt = SetForm(hPrinter, paperName, 1, ref formInfo);
}
else
{
this.AddCustomPaperSize(printerName, paperName, width, height);
rslt = true;
}
if (!rslt)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("添加纸张{0}时发生错误!, 系统错误号: {1}", paperName, GetLastError());
//throw new ApplicationException(strBuilder.ToString());
MessageBox.Show(strBuilder.ToString());
}
}
finally
{
ClosePrinter(hPrinter);
}
}
}
}
///
/// 增加打印纸类型
///
/// 打印机名称
/// 打印纸类型名称
/// 宽度,单位0.1毫米,需要在打印机允许的尺寸之内,否则按照默认打印纸类型打印
/// 高度,单位0.1毫米,需要在打印机允许的尺寸之内,否则按照默认打印纸类型打印
public void AddCustomPaperSize(string printerName, string paperName, float width, float height)
{
if (PlatformID.Win32NT == Environment.OSVersion.Platform)
{
const int PRINTER_ACCESS_USE = 0x00000008;
const int PRINTER_ACCESS_ADMINISTER = 0x00000004;
structPrinterDefaults defaults = new structPrinterDefaults();
defaults.pDatatype = null;
defaults.pDevMode = IntPtr.Zero;
defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;
IntPtr hPrinter = IntPtr.Zero;
if (OpenPrinter(printerName, out hPrinter, ref defaults))
{
try
{
DeleteForm(hPrinter, paperName);
structFormInfo1 formInfo = new structFormInfo1();
formInfo.Flags = 0;
formInfo.pName = paperName;
formInfo.Size.width = (int)(width * 100.0);
formInfo.Size.height = (int)(height * 100.0);
formInfo.ImageableArea.left = 0;
formInfo.ImageableArea.right = formInfo.Size.width;
formInfo.ImageableArea.top = 0;
formInfo.ImageableArea.bottom = formInfo.Size.height;
if (!AddForm(hPrinter, 1, ref formInfo))
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("添加纸张{0}时发生错误!, 系统错误号: {1}", paperName, GetLastError());
throw new ApplicationException(strBuilder.ToString());
}
}
finally
{
ClosePrinter(hPrinter);
}
}
else
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("打开打印机{0} 时出现异常!, 系统错误号: {1}", printerName, GetLastError());
throw new ApplicationException(strBuilder.ToString());
}
}
else
{
structDevMode pDevMode = new structDevMode();
IntPtr hDC = CreateDC(null, printerName, null, ref pDevMode);
if (hDC != IntPtr.Zero)
{
const long DM_PAPERSIZE = 0x00000002L;
const long DM_PAPERLENGTH = 0x00000004L;
const long DM_PAPERWIDTH = 0x00000008L;
pDevMode.dmFields = (int)(DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH);
pDevMode.dmPaperSize = 256;
pDevMode.dmPaperWidth = (short)(width * 2.54 * 10000.0);
pDevMode.dmPaperLength = (short)(height * 2.54 * 10000.0);
ResetDC(hDC, ref pDevMode);
DeleteDC(hDC);
}
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structPrinterDefaults
{
[MarshalAs(UnmanagedType.LPTStr)]
public String pDatatype;
public IntPtr pDevMode;
[MarshalAs(UnmanagedType.I4)]
public int DesiredAccess;
};
[DllImport("winspool.Drv", EntryPoint = "OpenPrinter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPTStr)] string printerName, out IntPtr phPrinter, ref structPrinterDefaults pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool ClosePrinter(IntPtr phPrinter);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structSize
{
public Int32 width;
public Int32 height;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structRect
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structFormInfo1
{
[MarshalAs(UnmanagedType.I4)]
public int Flags;
[MarshalAs(UnmanagedType.LPTStr)]
public String pName;
public structSize Size;
public structRect ImageableArea;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structDevMode
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public String dmDeviceName;
[MarshalAs(UnmanagedType.U2)]
public short dmSpecVersion;
[MarshalAs(UnmanagedType.U2)]
public short dmDriverVersion;
[MarshalAs(UnmanagedType.U2)]
public short dmSize;
[MarshalAs(UnmanagedType.U2)]
public short dmDriverExtra;
[MarshalAs(UnmanagedType.U4)]
public int dmFields;
[MarshalAs(UnmanagedType.I2)]
public short dmOrientation;
[MarshalAs(UnmanagedType.I2)]
public short dmPaperSize;
[MarshalAs(UnmanagedType.I2)]
public short dmPaperLength;
[MarshalAs(UnmanagedType.I2)]
public short dmPaperWidth;
[MarshalAs(UnmanagedType.I2)]
public short dmScale;
[MarshalAs(UnmanagedType.I2)]
public short dmCopies;
[MarshalAs(UnmanagedType.I2)]
public short dmDefaultSource;
[MarshalAs(UnmanagedType.I2)]
public short dmPrintQuality;
[MarshalAs(UnmanagedType.I2)]
public short dmColor;
[MarshalAs(UnmanagedType.I2)]
public short dmDuplex;
[MarshalAs(UnmanagedType.I2)]
public short dmYResolution;
[MarshalAs(UnmanagedType.I2)]
public short dmTTOption;
[MarshalAs(UnmanagedType.I2)]
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public String dmFormName;
[MarshalAs(UnmanagedType.U2)]
public short dmLogPixels;
[MarshalAs(UnmanagedType.U4)]
public int dmBitsPerPel;
[MarshalAs(UnmanagedType.U4)]
public int dmPelsWidth;
[MarshalAs(UnmanagedType.U4)]
public int dmPelsHeight;
[MarshalAs(UnmanagedType.U4)]
public int dmNup;
[MarshalAs(UnmanagedType.U4)]
public int dmDisplayFrequency;
[MarshalAs(UnmanagedType.U4)]
public int dmICMMethod;
[MarshalAs(UnmanagedType.U4)]
public int dmICMIntent;
[MarshalAs(UnmanagedType.U4)]
public int dmMediaType;
[MarshalAs(UnmanagedType.U4)]
public int dmDitherType;
[MarshalAs(UnmanagedType.U4)]
public int dmReserved1;
[MarshalAs(UnmanagedType.U4)]
public int dmReserved2;
}
[DllImport("winspool.Drv", EntryPoint = "AddForm", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool AddForm(IntPtr phPrinter, [MarshalAs(UnmanagedType.I4)] int level, ref structFormInfo1 form);
[DllImport("winspool.Drv", EntryPoint = "DeleteForm", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool DeleteForm(IntPtr phPrinter, [MarshalAs(UnmanagedType.LPTStr)] string pName);
[DllImport("winspool.Drv", EntryPoint = "SetForm", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool SetForm(IntPtr phPrinter, [MarshalAs(UnmanagedType.LPTStr)] string pName, [MarshalAs(UnmanagedType.I4)] int level, ref structFormInfo1 form);
[DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = false, ExactSpelling = true, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern Int32 GetLastError();
[DllImport("GDI32.dll", EntryPoint = "CreateDC", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern IntPtr CreateDC([MarshalAs(UnmanagedType.LPTStr)]string pDrive, [MarshalAs(UnmanagedType.LPTStr)] string pName, [MarshalAs(UnmanagedType.LPTStr)] string pOutput, ref structDevMode pDevMode);
[DllImport("GDI32.dll", EntryPoint = "ResetDC", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern IntPtr ResetDC(IntPtr hDC, ref structDevMode pDevMode);
[DllImport("GDI32.dll", EntryPoint = "DeleteDC", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool DeleteDC(IntPtr hDC);
}
}
分享到:
相关推荐
在提供的代码中,可能包含了一个自定义的类,该类封装了获取打印机纸张尺寸的方法。 首先,我们需要了解`PrinterSettings`类。这个类包含了关于打印机的配置信息,如默认纸张来源、默认打印质量等。通过`...
在C#中,你可以使用`PageSetupDialog`和`PrintDocument`类来设置打印机的属性,例如纸张大小、边距等。`PrintDocument`类是实际执行打印任务的核心,而`PageSetupDialog`则允许用户自定义页面设置。例如: ```...
使用`PrinterSettings`类,开发者可以获取或设置打印机的属性,包括默认纸张和自定义纸张。通过`PrinterSettings.DefaultPageSettings.PaperSize`,你可以指定打印机使用自定义的纸张尺寸。 3. **编程设置纸张格式...
下面是一个简单的示例代码,演示了如何使用C# API设定打印机纸张: ```csharp using System; using System.Drawing; using System.Drawing.Printing; class Program { static void Main() { PrintDocument ...
总结起来,C#提供了强大的工具来更改和管理打印机设置。通过`PrinterSettings`类,我们可以方便地调整各种打印属性,包括纸张大小、打印机端口、副本数量、双面打印等。然而,实际的打印操作还需要结合`...
总的来说,C#调用打印机打印涉及到对Windows API的深入理解和使用,包括但不限于处理GDI+绘图、与操作系统打印服务的交互等。通过这个实例,开发者可以学习到如何创建一个简单的打印应用程序,并逐步掌握更复杂打印...
本文将深入探讨如何使用C#.NET编写DLL(动态链接库)来实现这一功能,特别是在Windows XP和NT系统上向默认打印机添加自定义纸张的过程。 首先,我们需要了解C#.NET编程基础。C#.NET是一种面向对象的编程语言,它...
4. **考虑打印机兼容性**: 虽然你不需要通过更改打印机默认设置来实现自定义纸张,但要确保你的打印机支持自定义纸张尺寸。大多数现代打印机都具备这一功能,但在打印前最好进行一次测试。 5. **使用...
总之,C#调用打印机打印文本涉及到多个类和事件的协作。通过熟练掌握PrintDocument、PrinterSettings和Graphics对象的使用,你可以实现灵活、自定义的文本打印功能。在实际项目中,根据需求调整代码,以适应不同的...
本实例将探讨如何利用C#来实现打印机的预览、设置和打印操作。这些功能涉及了Windows API调用、控件交互以及.NET Framework的打印服务。 首先,我们需要了解.NET Framework中的`System.Drawing.Printing`命名空间,...
以上就是C#控制打印机直接打印和设置默认打印机的基本步骤。在实际项目中,你可能需要考虑更多细节,比如错误处理、多页打印、用户界面交互等。此外,C#的打印功能也支持更高级的特性,如模板打印、打印预览等,这...
本项目是一个使用C#编程语言开发的打印机状态监控工具,它旨在提供实时、直观的打印机工作状态反馈,帮助管理员高效管理和维护打印环境。 C#是一种由微软开发的面向对象的编程语言,广泛应用于Windows桌面应用、Web...
根据提供的文件信息,我们可以总结出以下关于如何在C#中实现打印功能的相关知识点: ### 一、打印功能的实现步骤...通过这些知识点的学习,开发者可以更好地理解和掌握C#中的打印功能,并能够灵活地应用于实际项目中。
总的来说,通过.NET WinForm的打印功能,结合自定义纸张设置、连续打印逻辑和打印机特定的控制命令,可以实现项目中的榜单票据打印,包括自动进纸和撕纸功能。虽然可能需要一些调试和优化,但这是实现此类需求的有效...
利用 Windows API 32 中打印函数,对打印机进行发送打印任务、暂停、取消、获取打印任务、获取打印机列表、状态、纸张等信息。压缩包中包含4个平时收集的打印类,测试均可用。附含Windows API 函数 for Visual Basic...
对于小票打印机,可能需要考虑纸张尺寸,例如热敏纸的宽度通常是58mm。可以自定义`PrintDocument`的纸张大小,确保内容适应打印机: ```csharp PaperSize ticketSize = new PaperSize("CustomTicket", 58 * 96, ...
水晶报表自定义纸张设置 在 Crystal Report 中,自定义纸张设置是非常重要的,因为不同的...通过使用 C# 代码和 Crystal ReportViewer 中的 BestFitPage 和 SeparatePages 属性,我们可以轻松地实现自定义纸张设置。
通过分析和学习这个源码,开发者不仅可以掌握如何控制Zebra打印机,还可以深化对C#语言和硬件交互的理解,这对于开发其他类型的嵌入式系统或IoT项目也非常有价值。同时,这样的实践经验有助于提高问题解决能力,因为...
在IT行业中,打印机套打和自定义纸张尺寸是企业级应用中常见需求,尤其是在财务、物流、医疗等需要大量出具票据的领域。本项目"**C#打印合格证2.0打印机套打票据程序**"提供了一整套解决方案,包括完整的源代码和...
本文详细介绍了如何在水晶报表中设置自定义纸张尺寸的方法,包括操作系统级别的纸张设置以及通过编程方式指定纸张规格。正确设置纸张尺寸是确保打印质量的关键,特别是在处理票据打印任务时尤为重要。通过上述步骤,...