C#程序word打印出来是空白 纸有空白 但是有一部分显示不出来

&&为什么打印出的纸张中间有道空白的竖杠
为什么打印出的纸张中间有道空白的竖杠
联想LJ6000激光打印机,单位上使用的,才用了一个多月,累计打了大概200多张纸,现在出现了这种情况,在打印出的纸张靠中间部位,出现了一道空白的竖杠,大概有两三厘米这么宽.请问大家,有哪些原因可能造成这种情况的发生?
硒鼓有问题或者墨粉分布不均匀,建议换鼓
提问者的感言:谢谢您的解答!
其他回答2条
激光打印机不会缺黑水的,只可能缺粉.而"才用了一个多月,累计打了大概200多张纸",又出现"空白的竖杠",不可能是缺粉.
主要原因是硒鼓有问题,建议楼主找到购买厂家.要求返厂
可能原因有:
1.充电辊对应部分损伤
2.硒鼓感光鼓对应部分损伤
3.激光发射部分至硒鼓感光鼓的光通道上有异物阻挡了激光束照射至感光鼓
如果是3可以自己排除异物即可,如果是1或2只能更换硒鼓或者充电辊了
问答为您推荐
市场价:¥3850-¥3850
网友正在问
||||||||||
Copyright (C) 1999-, All Rights Reserved 版权所有 天极网络只需一步,快速开始
查看: 2354|回复: 8
求助夏普一体机2048N打印A3时,前半张纸是空白,后半张纸是正常的
主题帖子积分
技术员, 积分 59, 距离下一级还需 241 积分
技术员, 积分 59, 距离下一级还需 241 积分
在线时间9 小时
& & 夏普一体机2048N,A3进纸口选的手动进纸盒,打印A3时,前半张纸是空白,后半张纸是正常的,字体颜色也正常。复印A3是完全正常,有大神指导下!
帖子永久地址:&<button type="submit" class="pn" onclick="setCopy('求助夏普一体机2048N打印A3时,前半张纸是空白,后半张纸是正常的\nhttp://www.oachn.net/thread--1.html', '帖子地址已经复制到剪贴板您可以用快捷键 Ctrl + V 粘贴到 QQ、MSN 里。')">推荐给好友导航<font color="#、大家在,时遇到技术问题,请到技术讨论区发帖,上传资料请到上传区,下载不要使用迅雷等工具。
2、下载:、、、、、。
3、论坛导航:、、、、、。
3、网导航:、、、、、。
主题帖子积分
技术员, 积分 59, 距离下一级还需 241 积分
技术员, 积分 59, 距离下一级还需 241 积分
在线时间9 小时
纸张设置成A3的了,WORD里面的页面设置也是A3的,打印机的对话框也是A3的。
&&A3的版式 竖 &&就可以正常打印出来的, 把A3的版式 在页面设置改成
横&&,就出现问题了,前半部分是空白的,纸张后半部分有字了
主题帖子积分
高级工程师, 积分 5135, 距离下一级还需 14865 积分
高级工程师, 积分 5135, 距离下一级还需 14865 积分
在线时间182 小时
最好是发几张图片看一下
&&#25104;&#38271;&#20540;: 8390主题帖子积分
在线时间95 小时
如果明显一半有一半无&&那就是打印的时候机器纸张没有设置正确&&
主题帖子积分
技术员, 积分 73, 距离下一级还需 227 积分
技术员, 积分 73, 距离下一级还需 227 积分
在线时间6 小时
检查打印文件的页面设置是否是A3
主题帖子积分
技术员, 积分 44, 距离下一级还需 256 积分
技术员, 积分 44, 距离下一级还需 256 积分
在线时间13 小时
夏普的手送打印不正常,都是设置的问题,多试试几种方向,还有把“自动适应”点上,基本都可以解决。
主题帖子积分
技术员, 积分 102, 距离下一级还需 198 积分
技术员, 积分 102, 距离下一级还需 198 积分
在线时间24 小时
明显设置问题
主题帖子积分
技术员, 积分 59, 距离下一级还需 241 积分
技术员, 积分 59, 距离下一级还需 241 积分
在线时间9 小时
过来两天,机器自动就好了。感谢给出思路的各位
主题帖子积分
技术员, 积分 35, 距离下一级还需 265 积分
技术员, 积分 35, 距离下一级还需 265 积分
在线时间64 小时
优秀会员GG勋章
授予优秀会员(GG专用)。
热心会员以上级别可佩带。
授予对宣传推广论坛有突出贡献的会员。
积极发表原创文章,能够带动人气、并且经常发一些具有讨论意义的帖子。
热心宣传勋章
授予宣传推广论坛的会员。
论坛VIP会员专用
优秀会员以上可佩带。
热心会员勋章
在讨论区积极帮助会员、及时、正确的回答会员提出的问题。
Powered byC#打印程序使用自定义纸张无效?
在使用C#开发打印程序的时候,经常用到自定义纸张的方法。按照MSN中自定义纸张的流程做下来总是无效,打印机依然使用默认纸张来打印。以下为此问题原因分析及解决办法。
MSN中自定义纸张大小的代码:
PrintDocument pd=new System.Drawing.Printing.PrintDocument();
pd.DefaultPageSettings.PaperSize=new PaperSize("myPaper",400,600)
pd.PrintPage+=new PrintPageEventHandler(pd_PrintPage);
pd.Print();
原因可能和有关,因为部分客户那里系统可以正常使用自定义纸张打印。也有可能和打印机驱动有关,各类驱动参差不齐,接口实现不一,也可能是原因之一。
自定义纸张大小时需要注意以下两个问题:
1、自定义纸张大小不要超过打印机支持的最大打印幅面,不然添加自定义打印纸张会失败或者无效。
2、如果还是不行,建议使用系统api来实现自定义纸张,放弃C#自己的做法。可能用到的打印机API封装类:
using System.C
using System.T
using System.Runtime.InteropS
using System.S
using System.Drawing.P
namespace PrintAP
public class Printer...{
private Printer()...{
///泥人张版本加强版
API声明#region API声明
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structPrinterDefaults
[MarshalAs(UnmanagedType.LPTStr)]
public String pD
public IntPtr pDevM
[MarshalAs(UnmanagedType.I4)]
public int D
[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 rinter,
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
public Int32
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct structRect
public Int32
public Int32
public Int32
public Int32
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
internal struct FormInfo1
[FieldOffset(0), MarshalAs(UnmanagedType.I4)]
public uint F
[FieldOffset(4), MarshalAs(UnmanagedType.LPWStr)]
public String pN
[FieldOffset(8)]
public structSize S
[FieldOffset(16)]
public structRect ImageableA
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct structDevMode
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public String
[MarshalAs(UnmanagedType.U2)]
public short dmSpecV
[MarshalAs(UnmanagedType.U2)]
public short dmDriverV
[MarshalAs(UnmanagedType.U2)]
public short dmS
[MarshalAs(UnmanagedType.U2)]
public short dmDriverE
[MarshalAs(UnmanagedType.U4)]
public int dmF
[MarshalAs(UnmanagedType.I2)]
public short dmO
[MarshalAs(UnmanagedType.I2)]
public short dmPaperS
[MarshalAs(UnmanagedType.I2)]
public short dmPaperL
[MarshalAs(UnmanagedType.I2)]
public short dmPaperW
[MarshalAs(UnmanagedType.I2)]
public short dmS
[MarshalAs(UnmanagedType.I2)]
public short dmC
[MarshalAs(UnmanagedType.I2)]
public short dmDefaultS
[MarshalAs(UnmanagedType.I2)]
public short dmPrintQ
[MarshalAs(UnmanagedType.I2)]
public short dmC
[MarshalAs(UnmanagedType.I2)]
public short dmD
[MarshalAs(UnmanagedType.I2)]
public short dmYR
[MarshalAs(UnmanagedType.I2)]
public short dmTTO
[MarshalAs(UnmanagedType.I2)]
public short dmC
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public String dmFormN
[MarshalAs(UnmanagedType.U2)]
public short dmLogP
[MarshalAs(UnmanagedType.U4)]
public int dmBitsPerP
[MarshalAs(UnmanagedType.U4)]
public int dmPelsW
[MarshalAs(UnmanagedType.U4)]
public int dmPelsH
[MarshalAs(UnmanagedType.U4)]
public int dmN
[MarshalAs(UnmanagedType.U4)]
public int dmDisplayF
[MarshalAs(UnmanagedType.U4)]
public int dmICMM
[MarshalAs(UnmanagedType.U4)]
public int dmICMI
[MarshalAs(UnmanagedType.U4)]
public int dmMediaT
[MarshalAs(UnmanagedType.U4)]
public int dmDitherT
[MarshalAs(UnmanagedType.U4)]
public int dmReserved1;
[MarshalAs(UnmanagedType.U4)]
public int dmReserved2;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct PRINTER_INFO_9
public IntPtr pDevM
[DllImport("winspool.Drv", EntryPoint = "AddFormW", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool AddForm(
IntPtr phPrinter,
[MarshalAs(UnmanagedType.I4)] int level,
ref FormInfo1 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("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);
[DllImport("winspool.Drv", EntryPoint = "SetPrinterA", SetLastError = true,
CharSet = CharSet.Auto, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]
internal static extern bool SetPrinter(
IntPtr hPrinter,
[MarshalAs(UnmanagedType.I4)] int level,
IntPtr pPrinter,
[MarshalAs(UnmanagedType.I4)] int command);
[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesA", SetLastError = true,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
internal static extern int DocumentProperties(
IntPtr hwnd,
IntPtr hPrinter,
[MarshalAs(UnmanagedType.LPStr)] string pDeviceName,
IntPtr pDevModeOutput,
IntPtr pDevModeInput,
[DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
internal static extern bool GetPrinter(
IntPtr hPrinter,
int dwLevel,
IntPtr pPrinter,
int dwBuf,
out int dwNeeded
internal enum SendMessageTimeoutFlags : uint
SMTO_NORMAL = 0x0000,
SMTO_BLOCK = 0x0001,
SMTO_ABORTIFHUNG = 0x0002,
SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
const int WM_SETTINGCHANGE = 0x001A;
const int HWND_BROADCAST = 0
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessageTimeout(
IntPtr windowHandle,
IntPtr wParam,
IntPtr lParam,
SendMessageTimeoutFlags flags,
uint timeout,
out IntPtr result
//EnumPrinters用到的函数和结构体
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumPrinters(PrinterEnumFlags Flags, string Name, uint Level,
IntPtr pPrinterEnum, uint cbBuf,
ref uint pcbNeeded, ref uint pcReturned);
[StructLayout(LayoutKind.Sequential)]
internal struct PRINTER_INFO_2
public string pServerN
public string pPrinterN
public string pShareN
public string pPortN
public string pDriverN
public string pC
public string pL
public IntPtr pDevM
public string pSepF
public string pPrintP
public string pD
public string pP
public IntPtr pSecurityD
public uint A
public uint P
public uint DefaultP
public uint StartT
public uint UntilT
public uint S
public uint cJ
public uint AveragePPM;
[FlagsAttribute]
internal enum PrinterEnumFlags
PRINTER_ENUM_DEFAULT = 0x,
PRINTER_ENUM_LOCAL = 0x,
PRINTER_ENUM_CONNECTIONS = 0x,
PRINTER_ENUM_FAVORITE = 0x,
PRINTER_ENUM_NAME = 0x,
PRINTER_ENUM_REMOTE = 0x,
PRINTER_ENUM_SHARED = 0x,
PRINTER_ENUM_NETWORK = 0x,
PRINTER_ENUM_EXPAND = 0x,
PRINTER_ENUM_CONTAINER = 0x,
PRINTER_ENUM_ICONMASK = 0x00ff0000,
PRINTER_ENUM_ICON1 = 0x,
PRINTER_ENUM_ICON2 = 0x,
PRINTER_ENUM_ICON3 = 0x,
PRINTER_ENUM_ICON4 = 0x,
PRINTER_ENUM_ICON5 = 0x,
PRINTER_ENUM_ICON6 = 0x,
PRINTER_ENUM_ICON7 = 0x,
PRINTER_ENUM_ICON8 = 0x,
PRINTER_ENUM_HIDE = 0x
//打印机状态
[FlagsAttribute]
internal enum PrinterStatus
PRINTER_STATUS_BUSY = 0x,
PRINTER_STATUS_DOOR_OPEN = 0x,
PRINTER_STATUS_ERROR = 0x,
PRINTER_STATUS_INITIALIZING = 0x,
PRINTER_STATUS_IO_ACTIVE = 0x,
PRINTER_STATUS_MANUAL_FEED = 0x,
PRINTER_STATUS_NO_TONER = 0x,
PRINTER_STATUS_NOT_AVAILABLE = 0x,
PRINTER_STATUS_OFFLINE = 0x,
PRINTER_STATUS_OUT_OF_MEMORY = 0x,
PRINTER_STATUS_OUTPUT_BIN_FULL = 0x,
PRINTER_STATUS_PAGE_PUNT = 0x,
PRINTER_STATUS_PAPER_JAM = 0x,
PRINTER_STATUS_PAPER_OUT = 0x,
PRINTER_STATUS_PAPER_PROBLEM = 0x,
PRINTER_STATUS_PAUSED = 0x,
PRINTER_STATUS_PENDING_DELETION = 0x,
PRINTER_STATUS_PRINTING = 0x,
PRINTER_STATUS_PROCESSING = 0x,
PRINTER_STATUS_TONER_LOW = 0x,
PRINTER_STATUS_USER_INTERVENTION = 0x,
PRINTER_STATUS_WAITING = 0x,
PRINTER_STATUS_WARMING_UP = 0x
//GetDefaultPrinter用到的API函数说明
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);
//SetDefaultPrinter用到的API函数声明
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetDefaultPrinter(string Name);
//EnumFormsA用到的函数声明,应该和EnumPrinters类似
[DllImport("winspool.drv", EntryPoint = "EnumForms")]
internal static extern int EnumFormsA(IntPtr hPrinter, int Level, ref byte pForm, int cbBuf, ref int pcbNeeded, ref int pcReturned);
#endregion API声明
internal static int GetPrinterStatusInt(string PrinterName)
int intRet = 0;
structPrinterDefaults defaults = new structPrinterDefaults();
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
int cbNeeded = 0;
bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded);
if (cbNeeded & 0)
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
if (bolRet)
PRINTER_INFO_2 Info2 = new PRINTER_INFO_2();
Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
intRet = System.Convert.ToInt32(Info2.Status);
Marshal.FreeHGlobal(pAddr);
ClosePrinter(hPrinter);
return intR
internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
uint cbNeeded = 0;
uint cReturned = 0;
bool ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, IntPtr.Zero, 0, ref cbNeeded, ref cReturned);
IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null, 2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);
PRINTER_INFO_2[] Info2 = new PRINTER_INFO_2[cReturned];
int offset = pAddr.ToInt32();
for (int i = 0; i & cR i++)
Info2[i].pServerName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPrinterName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pShareName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPortName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDriverName = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pComment = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pLocation = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDevMode = Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].pSepFile = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pPrintProcessor = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pDatatype = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pParameters = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
offset += 4;
Info2[i].pSecurityDescriptor = Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].Attributes = (uint)Marshal.ReadIntPtr(new IntPtr(offset));
offset += 4;
Info2[i].Priority = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].DefaultPriority = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].StartTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].UntilTime = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].Status = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].cJobs = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Info2[i].AveragePPM = (uint)Marshal.ReadInt32(new IntPtr(offset));
offset += 4;
Marshal.FreeHGlobal(pAddr);
return Info2;
return new PRINTER_INFO_2[0];
获取当前指定打印机的状态#region 获取当前指定打印机的状态
/// 获取当前指定打印机的状态
/// 打印机名称
/// 打印机状态描述
public static string GetPrinterStatus(string PrinterName)
int intValue = GetPrinterStatusInt(PrinterName);
string strRet = string.E
switch (intValue)
strRet = "准备就绪(Ready)";
strRet = "忙(Busy)";
strRet = "门被打开(Printer Door Open)";
strRet = "错误(Printer Error)";
case 0x0008000:
strRet = "正在初始化(Initializing)";
strRet = "正在输入或输出(I/O Active)";
strRet = "手工送纸(Manual Feed)";
strRet = "无墨粉(No Toner)";
strRet = "不可用(Not Available)";
strRet = "脱机(Off Line)";
strRet = "内存溢出(Out of Memory)";
strRet = "输出口已满(Output Bin Full)";
strRet = "当前页无法打印(Page Punt)";
strRet = "塞纸(Paper Jam)";
strRet = "打印纸用完(Paper Out)";
strRet = "纸张问题(Page Problem)";
strRet = "暂停(Paused)";
strRet = "正在删除(Pending Deletion)";
strRet = "正在打印(Printing)";
strRet = "正在处理(Processing)";
strRet = "墨粉不足(Toner Low)";
strRet = "需要用户干预(User Intervention)";
strRet = "等待(Waiting)";
strRet = "正在准备(Warming Up)";
strRet = "未知状态(Unknown Status)";
return strR
#endregion 获取当前指定打印机的状态
删除已经存在的自定义纸张#region 删除已经存在的自定义纸张
/// 删除已经存在的自定义纸张
/// 打印机名称
/// 纸张名称
public static void DeleteCustomPaperSize(string PrinterName, string PaperName)
const int PRINTER_ACCESS_USE = 0x;
const int PRINTER_ACCESS_ADMINISTER = 0x;
structPrinterDefaults defaults = new structPrinterDefaults();
defaults.pDatatype =
defaults.pDevMode = IntPtr.Z
defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;
IntPtr hPrinter = IntPtr.Z
//打开打印机
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
DeleteForm(hPrinter, PaperName);
ClosePrinter(hPrinter);
#endregion 删除已经存在的自定义纸张
指定的打印机设置以mm为单位的自定义纸张(Form)#region 指定的打印机设置以mm为单位的自定义纸张(Form)
/// 指定的打印机设置以mm为单位的自定义纸张(Form)
/// 打印机名称
/// Form名称
/// 以mm为单位的宽度
/// 以mm为单位的高度
public static void AddCustomPaperSize(string PrinterName, string PaperName, float WidthInMm, float HeightInMm)
if (PlatformID.Win32NT == Environment.OSVersion.Platform)
const int PRINTER_ACCESS_USE = 0x;
const int PRINTER_ACCESS_ADMINISTER = 0x;
const int FORM_PRINTER = 0x;
structPrinterDefaults defaults = new structPrinterDefaults();
defaults.pDatatype =
defaults.pDevMode = IntPtr.Z
defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;
IntPtr hPrinter = IntPtr.Z
//打开打印机
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
//如果Form存在删除之
DeleteForm(hPrinter, PaperName);
//创建并初始化FORM_INFO_1
FormInfo1 formInfo = new FormInfo1();
formInfo.Flags = 0;
formInfo.pName = PaperN
formInfo.Size.width = (int)(WidthInMm * 1000.0);
formInfo.Size.height = (int)(HeightInMm * 1000.0);
formInfo.ImageableArea.left = 0;
formInfo.ImageableArea.right = formInfo.Size.
formInfo.ImageableArea.top = 0;
formInfo.ImageableArea.bottom = formInfo.Size.
if (!AddForm(hPrinter, 1, ref formInfo))
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("向打印机 {1} 添加自定义纸张 {0} 失败!错误代号:{2}",
PaperName, PrinterName, GetLastError());
throw new ApplicationException(strBuilder.ToString());
const int DM_OUT_BUFFER = 2;
const int DM_IN_BUFFER = 8;
structDevMode devMode = new structDevMode();
IntPtr hPrinterInfo, hD
PRINTER_INFO_9 printerI
printerInfo.pDevMode = IntPtr.Z
int iPrinterInfoSize, iDummyI
int iDevModeSize = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, IntPtr.Zero, IntPtr.Zero, 0);
if (iDevModeSize & 0)
throw new ApplicationException("无法取得DEVMODE结构的大小!");
//分配缓冲
IntPtr hDevMode = Marshal.AllocCoTaskMem(iDevModeSize + 100);
//获取DEV_MODE指针
int iRet = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, hDevMode, IntPtr.Zero, DM_OUT_BUFFER);
if (iRet & 0)
throw new ApplicationException("无法获得DEVMODE结构!");
//填充DEV_MODE
devMode = (structDevMode)Marshal.PtrToStructure(hDevMode, devMode.GetType());
devMode.dmFields = 0x10000;
//FORM名称
devMode.dmFormName = PaperN
Marshal.StructureToPtr(devMode, hDevMode, true);
iRet = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName,
printerInfo.pDevMode, printerInfo.pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (iRet & 0)
throw new ApplicationException("无法为打印机设定打印方向!");
GetPrinter(hPrinter, 9, IntPtr.Zero, 0, out iPrinterInfoSize);
if (iPrinterInfoSize == 0)
throw new ApplicationException("调用GetPrinter方法失败!");
hPrinterInfo = Marshal.AllocCoTaskMem(iPrinterInfoSize + 100);
bool bSuccess = GetPrinter(hPrinter, 9, hPrinterInfo, iPrinterInfoSize, out iDummyInt);
if (!bSuccess)
throw new ApplicationException("调用GetPrinter方法失败!");
printerInfo = (PRINTER_INFO_9)Marshal.PtrToStructure(hPrinterInfo, printerInfo.GetType());
printerInfo.pDevMode = hDevM
Marshal.StructureToPtr(printerInfo, hPrinterInfo, true);
bSuccess = SetPrinter(hPrinter, 9, hPrinterInfo, 0);
if (!bSuccess)
throw new Win32Exception(Marshal.GetLastWin32Error(), "调用SetPrinter方法失败,无法进行打印机设置!");
SendMessageTimeout(
new IntPtr(HWND_BROADCAST),
WM_SETTINGCHANGE,
IntPtr.Zero,
IntPtr.Zero,
Printer.SendMessageTimeoutFlags.SMTO_NORMAL,
out hDummy);
ClosePrinter(hPrinter);
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("无法打开打印机{0}, 错误代号: {1}",
PrinterName, GetLastError());
throw new ApplicationException(strBuilder.ToString());
structDevMode pDevMode = new structDevMode();
IntPtr hDC = CreateDC(null, PrinterName, null, ref pDevMode);
if (hDC != IntPtr.Zero)
const long DM_PAPERSIZE = 0xL;
const long DM_PAPERLENGTH = 0xL;
const long DM_PAPERWIDTH = 0xL;
pDevMode.dmFields = (int)(DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH);
pDevMode.dmPaperSize = 256;
pDevMode.dmPaperWidth = (short)(WidthInMm * 1000.0);
pDevMode.dmPaperLength = (short)(HeightInMm * 1000.0);
ResetDC(hDC, ref pDevMode);
DeleteDC(hDC);
#endregion 指定的打印机设置以mm为单位的自定义纸张(Form)
获取本地打印机列表#region 获取本地打印机列表
/// 获取本地打印机列表
/// 可以通过制定参数获取打印机
/// 打印机列表
public static System.Collections.ArrayList GetPrinterList()
System.Collections.ArrayList alRet = new System.Collections.ArrayList();
PRINTER_INFO_2[] Info2 = EnumPrintersByFlag(PrinterEnumFlags.PRINTER_ENUM_LOCAL);
for (int i = 0; i & Info2.L i++)
alRet.Add(Info2[i].pPrinterName);
return alR
#endregion 获取本地打印机列表
获取本机的默认打印机名称#region 获取本机的默认打印机名称
/// 获取本机的默认打印机名称
/// 默认打印机名称
public static string GetDeaultPrinterName()
StringBuilder dp = new StringBuilder(256);
int size = dp.C
if (GetDefaultPrinter(dp, ref size))
return dp.ToString();
return string.E
#endregion 获取本机的默认打印机名称
设置默认打印机#region 设置默认打印机
/// 设置默认打印机
/// 可用的打印机名称
public static void SetPrinterToDefault(string PrinterName)
SetDefaultPrinter(PrinterName);
#endregion 设置默认打印机
判断打印机是否在系统可用的打印机列表中#region 判断打印机是否在系统可用的打印机列表中
///// 判断打印机是否在系统可用的打印机列表中
///// 打印机名称
///// 是:在;否:不在
public static bool PrinterInList(string PrinterName)
bool bolRet =
System.Collections.ArrayList alPrinters = GetPrinterList();
for (int i = 0; i & alPrinters.C i++)
if (PrinterName == alPrinters[i].ToString())
alPrinters.Clear();
alPrinters =
return bolR
#endregion 判断打印机是否在系统可用的打印机列表中
判断表单是否在指定的打印机所支持的纸张列表中#region 判断表单是否在指定的打印机所支持的纸张列表中
///// 判断表单是否在指定的打印机所支持的纸张列表中,表单就是我们平常所说的纸张
///// 打印机名称
///// 纸张名称
///// 是:在;否:不在
public static bool FormInPrinter(string PrinterName, string PaperName)
bool bolRet =
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.PrinterSettings.PrinterName = PrinterN
foreach (System.Drawing.Printing.PaperSize ps in pd.PrinterSettings.PaperSizes)
if (ps.PaperName == PaperName)
pd.Dispose();
return bolR
#endregion 判断表单是否在指定的打印机所支持的纸张列表中
判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配#region 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
/// 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
/// 打印机名称
/// 表单名称
public static bool FormSameSize(string PrinterName, string FormName, decimal Width, decimal Height)
bool bolRet =
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
pd.PrinterSettings.PrinterName = PrinterN
foreach (System.Drawing.Printing.PaperSize ps in pd.PrinterSettings.PaperSizes)
if (ps.PaperName == FormName)
decimal decWidth = FromInchToCM(System.Convert.ToDecimal(ps.Width));
decimal decHeight = FromInchToCM(System.Convert.ToDecimal(ps.Height));
//只要整数位相同即认为是同一纸张,毕竟inch到cm的转换并不能整除
if (Math.Round(decWidth, 0) == Math.Round(Width, 0) && Math.Round(decHeight, 0) == Math.Round(Height, 0))
pd.Dispose();
return bolR
#endregion 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配
英寸到厘米的转换#region 英寸到厘米的转换
/// 英寸到厘米的转换
/// 英寸数
/// 厘米数,两位小数
public static decimal FromInchToCM(decimal inch)
return Math.Round((System.Convert.ToDecimal((inch / 100)) * System.Convert.ToDecimal(2.5400)), 2);
#endregion 英寸到厘米的转换
-------------------------------------
然后在程序里调用写好的API即可。
-------------------------
using System.Collections.G
using System.D
using System.D
using System.T
using System.Windows.F
using System.Runtime.InteropS
using System.IO;
namespace PrintAPI
public partial class Form1 : Form
public Form1()
InitializeComponent();
#region 读取ini文件
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal, int size, string filePath);
/// 对ini文件进行读操作的函数
/// []节点
/// 文件名的路径
public string IniReadValue(string Section, string Key, string filepath)
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, filepath);
return temp.ToString();
#endregion
#region 读取打印机相关配置
//读取打印机的名称
private string getPrinterName(string printerType)
string filepath = y.GetCurrentDirectory() + "/Printer.INI";
string printerName = IniReadValue("Printer", printerType, filepath);
return printerN
//读取票据类型的大小
private void getPrinterSize(string printerType, ref int width, ref int height)
string strWidth = string.E
string strHeight = string.E
string filepath = Directory.GetCurrentDirectory() + "/Printer.INI";
if (printerType.ToLower() == "invoice")
strWidth = IniReadValue("BillSize", "InvoiceWidth", filepath);
strHeight = IniReadValue("BillSize", "InvoiceHeight", filepath);
else if (printerType.ToLower() == "receipt")
strWidth = IniReadValue("BillSize", "ReceiptWidth", filepath);
strHeight = IniReadValue("BillSize", "ReceiptHeight", filepath);
width = Convert.ToInt32(strWidth);
height = Convert.ToInt32(strHeight);
#endregion
private void button1_Click(object sender, EventArgs e)
XRInvoice invoice = new XRInvoice();
//添加票据类型的打印规格
AddBillSizeForPrinter();
//指定报表打印的规格
string invoicePrinterName = getPrinterName("InvoicePrinter");
invoice.PaperKind = System.Drawing.Printing.PaperKind.C
invoice.PaperName = "XinLiInvoice";
invoice.Print(invoicePrinterName);
//指定收据打印规格
catch (Exception ex)
MessageBox.Show(ex.ToString());
private void AddBillSizeForPrinter()
//放到整个项目程序加载的时候
//读取发票、收据打印机的名称
string invoicePrinterName = getPrinterName("InvoicePrinter");
string receiptPrinterName = getPrinterName("receiptPrinter");
//读取发票、收据打印机的名称
//判断发票打印机是否在打印机列表中
bool isExistInvoicePrinter = Printer.PrinterInList(invoicePrinterName);
//判断发票打印机是否在打印机列表中
if (!isExistInvoicePrinter)
throw new Exception("指定的打印机不存在!");
//从ini文件中读取票据类型的size
int width = 0;
int height = 0;
//添加发票规格
getPrinterSize("invoice", ref width, ref height);
Printer.AddCustomPaperSize(invoicePrinterName, "XinLiInvoice", width, height);
//添加收据规格
getPrinterSize("receipt", ref width, ref height);
Printer.AddCustomPaperSize(receiptPrinterName, "XinLiReceipt", width, height);
--------------------------
使用到的Printer.ini配置文件
-------------------
InvoicePrinter= pdfFactory Pro
ReceiptPrinter = pdfFactory Pro
[BillSize]
InvoiceWidth = 706
InvoiceHeight = 515
ReceiptWidth = 706
ReceiptHeight = 515
关键词:& & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事/刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款
匿名 发表于 /bkhtml/c17/89.htm 秋天的作文网300字 /details/zs1807.html 最美的珍藏作文 /details/zs1808.html
(0) 匿名 发表于 XRInvoice 在哪引用呀!能回个吗:
你还可能关注…
本周热门内容}

我要回帖

更多关于 word图片打印出来空白 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信