|
why is a 2o lb ream of paper called 20 lb?
|
|
|
|
|
Hello …
Thanks for the code …
I’m using it and it works fine, but I have another problem …
As default I have set PaperSize to A4, so when I run my Application where I’m using your code,
I set the “Custom SIZE” as shown in following method…
private void print( bool printAll )<br />
{<br />
string sPrinterName = pd.PrinterSettings.PrinterName;<br />
MJMCustomPrintForm.AddCustomPaperSize (sPrinterName,<br />
"CustomSIZE", 108.0f, 160.0f );<br />
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);<br />
pd.DefaultPageSettings.Landscape = true;<br />
<br />
pd.PrintPage += new PrintPageEventHandler( printPages );<br />
<br />
pd.Print();<br />
<br />
}
My problem is: How do I reset the PaperSize to the default A4 Size, just after pd.Print(); is called ???
I tried again to add PaperSize by calling …
MJMCustomPrintForm.AddCustomPaperSize( sPrinterName, "A4", 210.0f,
297.0f );
But I got an ApllicationException in Your code and folowing section in
line I marked with <<<< at the end …
if ( !AddForm( hPrinter, 1, ref formInfo ) )<br />
{<br />
StringBuilder strBuilder = new StringBuilder();<br />
strBuilder.AppendFormat( "Failed to add the custom paper size{0}<br />
to the printer {1}, System error number: {2}",<br />
paperName, printerName, GetLastError() );<br />
throw new ApplicationException( strBuilder.ToString ) );<<<<<br />
}
The Exception is following …
Failed to add the custom paper size A4 to the printer HP DeskJet 720C, System error number: 80
So … Do You have any idea how to programmatically reset the PaperSize to A4 ????
I’m running Windows Vista Ultimate SP1, Microsoft Visual Studio 2008, .NET Framework 3.5
Best regards ….
|
|
|
|
|
public void DefaultCustomPaperSize(string printerName, string paperName)
{
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
{
const int DM_OUT_BUFFER = 2;
const int DM_IN_BUFFER = 8;
structDevMode devMode = new structDevMode();
IntPtr hPrinterInfo, hDummy;
PRINTER_INFO_9 printerInfo;
printerInfo.pDevMode = IntPtr.Zero;
int iPrinterInfoSize, iDummyInt;
// GET THE SIZE OF THE DEV_MODE BUFFER
int iDevModeSize = DocumentProperties(IntPtr.Zero, hPrinter, printerName, IntPtr.Zero, IntPtr.Zero, 0);
if (iDevModeSize < 0)
throw new ApplicationException("Cannot get the size of the DEVMODE structure.");
// ALLOCATE THE BUFFER
IntPtr hDevMode = Marshal.AllocCoTaskMem(iDevModeSize + 100);
// GET A POINTER TO THE DEV_MODE BUFFER
int iRet = DocumentProperties(IntPtr.Zero, hPrinter, printerName, hDevMode, IntPtr.Zero, DM_OUT_BUFFER);
if (iRet < 0)
throw new ApplicationException("Cannot get the DEVMODE structure.");
// FILL THE DEV_MODE STRUCTURE
devMode = (structDevMode)Marshal.PtrToStructure(hDevMode, devMode.GetType());
// SET THE FORM NAME FIELDS TO INDICATE THAT THIS FIELD WILL BE MODIFIED
devMode.dmFields = 0x10000; // DM_FORMNAME
// SET THE FORM NAME
devMode.dmFormName = paperName;
// PUT THE DEV_MODE STRUCTURE BACK INTO THE POINTER
Marshal.StructureToPtr(devMode, hDevMode, true);
// MERGE THE NEW CHAGES WITH THE OLD
iRet = DocumentProperties(IntPtr.Zero, hPrinter, printerName,
printerInfo.pDevMode, printerInfo.pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);
if (iRet < 0)
throw new ApplicationException("Unable to set the orientation setting for this printer.");
// GET THE PRINTER INFO SIZE
GetPrinter(hPrinter, 9, IntPtr.Zero, 0, out iPrinterInfoSize);
if (iPrinterInfoSize == 0)
throw new ApplicationException("GetPrinter failed. Couldn't get the # bytes needed for shared PRINTER_INFO_9 structure");
// ALLOCATE THE BUFFER
hPrinterInfo = Marshal.AllocCoTaskMem(iPrinterInfoSize + 100);
// GET A POINTER TO THE PRINTER INFO BUFFER
bool bSuccess = GetPrinter(hPrinter, 9, hPrinterInfo, iPrinterInfoSize, out iDummyInt);
if (!bSuccess)
throw new ApplicationException("GetPrinter failed. Couldn't get the shared PRINTER_INFO_9 structure");
// FILL THE PRINTER INFO STRUCTURE
printerInfo = (PRINTER_INFO_9)Marshal.PtrToStructure(hPrinterInfo, printerInfo.GetType());
printerInfo.pDevMode = hDevMode;
// GET A POINTER TO THE PRINTER INFO STRUCTURE
Marshal.StructureToPtr(printerInfo, hPrinterInfo, true);
// SET THE PRINTER SETTINGS
bSuccess = SetPrinter(hPrinter, 9, hPrinterInfo, 0);
if (!bSuccess)
throw new Win32Exception(Marshal.GetLastWin32Error(), "SetPrinter() failed. Couldn't set the printer settings");
// Tell all open programs that this change occurred.
SendMessageTimeout(
new IntPtr(HWND_BROADCAST),
WM_SETTINGCHANGE,
IntPtr.Zero,
IntPtr.Zero,
frmViewer.SendMessageTimeoutFlags.SMTO_NORMAL, 1000,
//0x0000,
// 1000,
out hDummy);
}
finally
{
ClosePrinter(hPrinter);
}
}
else
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.AppendFormat("Failed to open the {0} printer, System error number: {1}",
printerName, GetLastError());
throw new ApplicationException(strBuilder.ToString());
}
}
}
// Use this code it Set to your Default Paper Size. For Example if u are Setting back A4 or other paper.
|
|
|
|
|
Thank you for your post.
However above code does not work. There was existing form A0 available is system. I would like to assign to printer by calling following code.
DefaultCustomPaperSize(<Name of printer>, "A0");
Code execute without any exception. Any clue whats going wrong here.
TIA
|
|
|
|
|
|
Sir,
Can u help me in sending the ReportContent namespace file it is not uploaded. Please send ReportContent.cs file.
i.e. it is for ReportContent.localreport.render();
Thanks in advance
Syed
|
|
|
|
|
Sir,
Can u Please tell me how to export() the Crystal Report in C#.net using Crystal Report Viewer so that it can be kept in a printer stream to print
Thanks in advance
|
|
|
|
|
Hi,
In the above scenario the printing will be done in Letter size not in the custom paper size which we have created.
Can anybody solve this problem please,
Thanks in advance
|
|
|
|
|
|
I'm need source, but i download, please send to mail: ngoctinhquangngai@gmail.com. Thank
|
|
|
|
|
Hi
I have tested with various printers working fine, but it is not working with lazerjet hp1010 serice. How to solve this problem
shankar
|
|
|
|
|
HI,
Thanks for nice code. I also just want to chage the orientation after adding new custom paper....
Can you help me on this....
Looking for your reply....
|
|
|
|
|
Just i run this code in vs 2005,it was not worked.why?
also i had no error messages
os:windows XP sp2
no
|
|
|
|
|
Hi twostepted,
I am REALLY new to the whole printing framework in C# and I have to say, that it seems WAY more complicated than it needs to be. (Don't even get me started on how long I spent trying to understand and work with the multi-page printing system for .Net!!! )
I used to work with the Printing Architecture in MFC and had a great deal more success with that than I have had so far with the .Net version.
I have managed to get basic printing working no worries, HOWEVER, I am writing an application at the moment that requires several custom paper sizes and I am having an extremely difficult time getting the printer (it seems - .Net says all is right with the world) to co-operate.
I can print to a standard A4 page and get all printer operations to work correctly no problem at all (which is great), but as I said, I need to print on custom paper size(s).
I have tried MSDN, Technet, CodeProject and a variety of other sources (not as reputable), along with MANY hours scouring GOOGLE trying to get even the smallest amount of USEFUL information.
It turns out, the article and source code you have provided is one of the ONLY examples ANYWHERE of setting custom paper sizes on printers, let alone any kind of intelligent discussion of how to achieve it.
Anyway, enough of that rambling.
My problem, as I indicated, is that I need to print to several custom size forms.
I have tried creating a custom paper size using the .Net PaperSize class, which all works in code, but when the paper is sent to the printer, i.e. the printer is told to use that paper size, it just ignores it and prints on the default (usually A4).
This is highly frustrating as when I Preview the page, using a standard Print Preview dialog, or control, the page displays correctly (right size).
I have tried at least three different models of Printer, Canon LBP-2410, Brother MFC-9180 and a Dell 1320C. All Printers run the code properly and there are no errors reported, but when the paper sizes are listed the custom paper is NEVER displayed or available, even through code.
I am getting extremely frustrated with this whole thing as it should be (IMHO) fairly simple to create, select and use a custom paper size on a printer. ( I can do it using WORD for goodness sake, why is it so hard!!!!).
Anyway, I would appreciate any help or information that you or any other reader/coder/janitor/garbage man ..... or anyone could provide.
Kind Regards,
D
|
|
|
|
|
hi,
i tried this application with EPSON LX-800 and it worked fine but my customer has LQ 2180 i tried on that and it is not working.
can you help me with this. Did i miss out something, or some configurations.
regards
|
|
|
|
|
I'm not sure what the problem is. You'll have to be more descriptive as to what exactly isn't working. This solution has some problems in that it doesn't throw an exception if the paper size hasn't actually been added. Often, the printer (or its drivers?) will reject the custom paper size because it isn't supported by the mfgr. YOu might with the mfgr to be sure the paper size you're trying to add is supported by the printer / driver.
|
|
|
|
|
Hi,
I tried the code with a paper size of 9 x 16 cm (we want to print mailers)
when i sent a test page from printer settings it prints exactly but I try printing from my application which is a VB 6 application it does not work as expected first mailer is printed the other one is little out of the format and the it changes per page.
I have a question:
1. when I run the utility of setting paper sizes does that changes settings for all trays.
if yes then I am not able to understand the problem we are facing and if the answer is now then can you help with this.
regards,
humair
|
|
|
|
|
My website (under construction) is componentworks.net. You can email me at travis@componentworks.net
--Travis
|
|
|
|
|
thanks for this Article that work for dot matrix printer.in Epson LX 800 printer driver have no any option through which we can set custome paper size. but your application do this one. it safe us against customer which is far away from us and is in different continent
thanks on this kind Anticipation.
syed zaim raza kazmi
|
|
|
|
|
I'm not sure what your problem is. I know you can run into problems with certain paper sizes on certain printers. AFAIK, the code I posted doesn't correctly check that a the paper size was added. We had to ask the printer manufacturer about it or maybe in documentation?
|
|
|
|
|
After I used it, my application that was printing throw exception - "Handle is invalid".
This same application was working prior to operation on this project.
Can some one help me please?
|
|
|
|
|
I'm not sure what your problem could be unless you just have the wrong name for the printer. I've seen that cause an invalid handle before.
--Travis
|
|
|
|
|
Do you have these code in VB.net?
|
|
|
|
|
No, sorry I don't have it in VB.NET. I had to port some similar code from VB.NET and it wasn't too hard. Let me know if you run into any roadblocks porting to VB.NET.
--Travis
|
|
|
|
|
As I am unable to convert this to vb.net 2003(do no understand whats going on),
I did the most obvios thing. Create a DLL of it. Wich can be referenced and called in vb.net.
Maybe a solution for you to.
<br />
using System;<br />
using System.Text;<br />
using System.Runtime.InteropServices;<br />
using System.Security;<br />
using System.ComponentModel;<br />
using System.Drawing.Printing;<br />
<br />
namespace CLS_PrintPaper<br />
{<br />
public class CLS_PrintPaper<br />
{<br />
private CLS_PrintPaper()<br />
{<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]<br />
internal struct structPrinterDefaults<br />
{<br />
[MarshalAs(UnmanagedType.LPTStr)] public String pDatatype;<br />
public IntPtr pDevMode;<br />
[MarshalAs(UnmanagedType.I4)] public int DesiredAccess;<br />
};<br />
<br />
[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);<br />
[DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=false, CallingConvention=CallingConvention.StdCall),SuppressUnmanagedCodeSecurityAttribute()] internal static extern bool ClosePrinter(IntPtr phPrinter);<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]<br />
internal struct structSize<br />
{<br />
public Int32 width;<br />
public Int32 height;<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]<br />
internal struct structRect<br />
{<br />
public Int32 left;<br />
public Int32 top;<br />
public Int32 right;<br />
public Int32 bottom;<br />
}<br />
<br />
[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Unicode)]<br />
internal struct FormInfo1<br />
{<br />
[FieldOffset(0), MarshalAs(UnmanagedType.I4)] public uint Flags;<br />
[FieldOffset(4), MarshalAs(UnmanagedType.LPWStr)] public String pName;<br />
[FieldOffset(8)] public structSize Size;<br />
[FieldOffset(16)] public structRect ImageableArea;<br />
};<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]<br />
internal struct structDevMode<br />
{<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String<br />
dmDeviceName;<br />
[MarshalAs(UnmanagedType.U2)] public short dmSpecVersion;<br />
[MarshalAs(UnmanagedType.U2)] public short dmDriverVersion;<br />
[MarshalAs(UnmanagedType.U2)] public short dmSize;<br />
[MarshalAs(UnmanagedType.U2)] public short dmDriverExtra;<br />
[MarshalAs(UnmanagedType.U4)] public int dmFields;<br />
[MarshalAs(UnmanagedType.I2)] public short dmOrientation;<br />
[MarshalAs(UnmanagedType.I2)] public short dmPaperSize;<br />
[MarshalAs(UnmanagedType.I2)] public short dmPaperLength;<br />
[MarshalAs(UnmanagedType.I2)] public short dmPaperWidth;<br />
[MarshalAs(UnmanagedType.I2)] public short dmScale;<br />
[MarshalAs(UnmanagedType.I2)] public short dmCopies;<br />
[MarshalAs(UnmanagedType.I2)] public short dmDefaultSource;<br />
[MarshalAs(UnmanagedType.I2)] public short dmPrintQuality;<br />
[MarshalAs(UnmanagedType.I2)] public short dmColor;<br />
[MarshalAs(UnmanagedType.I2)] public short dmDuplex;<br />
[MarshalAs(UnmanagedType.I2)] public short dmYResolution;<br />
[MarshalAs(UnmanagedType.I2)] public short dmTTOption;<br />
[MarshalAs(UnmanagedType.I2)] public short dmCollate;<br />
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String dmFormName;<br />
[MarshalAs(UnmanagedType.U2)] public short dmLogPixels;<br />
[MarshalAs(UnmanagedType.U4)] public int dmBitsPerPel;<br />
[MarshalAs(UnmanagedType.U4)] public int dmPelsWidth;<br />
[MarshalAs(UnmanagedType.U4)] public int dmPelsHeight;<br />
[MarshalAs(UnmanagedType.U4)] public int dmNup;<br />
[MarshalAs(UnmanagedType.U4)] public int dmDisplayFrequency;<br />
[MarshalAs(UnmanagedType.U4)] public int dmICMMethod;<br />
[MarshalAs(UnmanagedType.U4)] public int dmICMIntent;<br />
[MarshalAs(UnmanagedType.U4)] public int dmMediaType;<br />
[MarshalAs(UnmanagedType.U4)] public int dmDitherType;<br />
[MarshalAs(UnmanagedType.U4)] public int dmReserved1;<br />
[MarshalAs(UnmanagedType.U4)] public int dmReserved2;<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] <br />
internal struct PRINTER_INFO_9 <br />
{<br />
public IntPtr pDevMode;<br />
}<br />
<br />
[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);<br />
<br />
<br />
<br />
[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);<br />
<br />
[DllImport("kernel32.dll", EntryPoint="GetLastError", SetLastError=false, ExactSpelling=true, CallingConvention=CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()] internal static extern Int32 GetLastError();<br />
<br />
[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);<br />
<br />
[DllImport("GDI32.dll", EntryPoint="ResetDC", SetLastError=true,<br />
CharSet=CharSet.Unicode, ExactSpelling=false,<br />
CallingConvention=CallingConvention.StdCall),<br />
SuppressUnmanagedCodeSecurityAttribute()]<br />
internal static extern IntPtr ResetDC(<br />
IntPtr hDC, <br />
ref structDevMode<br />
pDevMode);<br />
<br />
[DllImport("GDI32.dll", EntryPoint="DeleteDC", SetLastError=true,<br />
CharSet=CharSet.Unicode, ExactSpelling=false,<br />
CallingConvention=CallingConvention.StdCall),<br />
SuppressUnmanagedCodeSecurityAttribute()]<br />
internal static extern bool DeleteDC(IntPtr hDC);<br />
<br />
[DllImport("winspool.Drv", EntryPoint="SetPrinterA", SetLastError=true,<br />
CharSet=CharSet.Auto, ExactSpelling=true,<br />
CallingConvention=CallingConvention.StdCall), SuppressUnmanagedCodeSecurityAttribute()]<br />
internal static extern bool SetPrinter(<br />
IntPtr hPrinter,<br />
[MarshalAs(UnmanagedType.I4)] int level, <br />
IntPtr pPrinter, <br />
[MarshalAs(UnmanagedType.I4)] int command);<br />
<br />
<br />
[DllImport("winspool.Drv", EntryPoint="DocumentPropertiesA", SetLastError=true, <br />
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] <br />
public static extern int DocumentProperties(<br />
IntPtr hwnd, <br />
IntPtr hPrinter,<br />
[MarshalAs(UnmanagedType.LPStr)] string pDeviceName ,<br />
IntPtr pDevModeOutput, <br />
IntPtr pDevModeInput, <br />
int fMode<br />
);<br />
<br />
[DllImport("winspool.Drv", EntryPoint="GetPrinterA", SetLastError=true, <br />
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] <br />
public static extern bool GetPrinter(<br />
IntPtr hPrinter, <br />
int dwLevel ,<br />
IntPtr pPrinter,<br />
int dwBuf , <br />
out int dwNeeded <br />
); <br />
<br />
[Flags] public enum SendMessageTimeoutFlags : uint<br />
{<br />
SMTO_NORMAL = 0x0000,<br />
SMTO_BLOCK = 0x0001,<br />
SMTO_ABORTIFHUNG = 0x0002,<br />
SMTO_NOTIMEOUTIFNOTHUNG = 0x0008<br />
}<br />
const int WM_SETTINGCHANGE = 0x001A;<br />
const int HWND_BROADCAST = 0xffff;<br />
<br />
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]<br />
public static extern IntPtr SendMessageTimeout(<br />
IntPtr windowHandle, <br />
uint Msg, <br />
IntPtr wParam, <br />
IntPtr lParam, <br />
SendMessageTimeoutFlags flags, <br />
uint timeout, <br />
out IntPtr result<br />
);<br />
<br />
public static void AddCustomPaperSizeToDefaultPrinter(string paperName, float widthMm, float heightMm)<br />
{<br />
PrintDocument pd = new PrintDocument();<br />
string sPrinterName = pd.PrinterSettings.PrinterName;<br />
AddCustomPaperSize(sPrinterName, paperName, widthMm, heightMm);<br />
}<br />
<br />
public static void AddCustomPaperSize(string printerName, string paperName, float<br />
widthMm, float heightMm)<br />
{<br />
if (PlatformID.Win32NT == Environment.OSVersion.Platform)<br />
{<br />
<br />
const int PRINTER_ACCESS_USE = 0x00000008;<br />
const int PRINTER_ACCESS_ADMINISTER = 0x00000004;<br />
<br />
structPrinterDefaults defaults = new structPrinterDefaults();<br />
defaults.pDatatype = null;<br />
defaults.pDevMode = IntPtr.Zero;<br />
defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;<br />
<br />
IntPtr hPrinter = IntPtr.Zero;<br />
<br />
if (OpenPrinter(printerName, out hPrinter, ref defaults))<br />
{<br />
try<br />
{<br />
DeleteForm(hPrinter, paperName);<br />
FormInfo1 formInfo = new FormInfo1();<br />
formInfo.Flags = 0;<br />
formInfo.pName = paperName;<br />
formInfo.Size.width = (int)(widthMm * 1000.0); <br />
formInfo.Size.height = (int)(heightMm * 1000.0);<br />
formInfo.ImageableArea.left = 0;<br />
formInfo.ImageableArea.right = formInfo.Size.width;<br />
formInfo.ImageableArea.top = 0;<br />
formInfo.ImageableArea.bottom = formInfo.Size.height;<br />
if (!AddForm(hPrinter, 1, ref formInfo))<br />
{<br />
StringBuilder strBuilder = new StringBuilder();<br />
strBuilder.AppendFormat("Failed to add the custom paper size {0} to the printer {1}, System error number: {2}",<br />
paperName, printerName, GetLastError());<br />
throw new ApplicationException(strBuilder.ToString());<br />
}<br />
<br />
const int DM_OUT_BUFFER = 2;<br />
const int DM_IN_BUFFER = 8;<br />
structDevMode devMode = new structDevMode();<br />
IntPtr hPrinterInfo, hDummy;<br />
PRINTER_INFO_9 printerInfo;<br />
printerInfo.pDevMode = IntPtr.Zero;<br />
int iPrinterInfoSize, iDummyInt;<br />
<br />
<br />
int iDevModeSize = DocumentProperties(IntPtr.Zero, hPrinter, printerName, IntPtr.Zero, IntPtr.Zero, 0);<br />
<br />
if(iDevModeSize < 0)<br />
throw new ApplicationException("Cannot get the size of the DEVMODE structure.");<br />
<br />
IntPtr hDevMode = Marshal.AllocCoTaskMem(iDevModeSize + 100);<br />
<br />
int iRet = DocumentProperties(IntPtr.Zero, hPrinter, printerName, hDevMode, IntPtr.Zero, DM_OUT_BUFFER);<br />
<br />
if(iRet < 0)<br />
throw new ApplicationException("Cannot get the DEVMODE structure.");<br />
<br />
devMode = (structDevMode)Marshal.PtrToStructure(hDevMode, devMode.GetType());<br />
<br />
devMode.dmFields = 0x10000;
devMode.dmFormName = paperName; <br />
<br />
Marshal.StructureToPtr(devMode, hDevMode, true);<br />
<br />
iRet = DocumentProperties(IntPtr.Zero, hPrinter, printerName, <br />
printerInfo.pDevMode, printerInfo.pDevMode, DM_IN_BUFFER | DM_OUT_BUFFER);<br />
<br />
if(iRet < 0)<br />
throw new ApplicationException("Unable to set the orientation setting for this printer.");<br />
<br />
GetPrinter(hPrinter, 9, IntPtr.Zero, 0, out iPrinterInfoSize);<br />
if(iPrinterInfoSize == 0)<br />
throw new ApplicationException("GetPrinter failed. Couldn't get the # bytes needed for shared PRINTER_INFO_9 structure");<br />
<br />
hPrinterInfo = Marshal.AllocCoTaskMem(iPrinterInfoSize + 100);<br />
<br />
bool bSuccess = GetPrinter(hPrinter, 9, hPrinterInfo, iPrinterInfoSize, out iDummyInt);<br />
<br />
if(!bSuccess)<br />
throw new ApplicationException("GetPrinter failed. Couldn't get the shared PRINTER_INFO_9 structure");<br />
<br />
printerInfo = (PRINTER_INFO_9)Marshal.PtrToStructure(hPrinterInfo, printerInfo.GetType());<br />
printerInfo.pDevMode = hDevMode;<br />
<br />
Marshal.StructureToPtr(printerInfo, hPrinterInfo, true);<br />
<br />
bSuccess = SetPrinter(hPrinter, 9, hPrinterInfo, 0);<br />
<br />
if(!bSuccess)<br />
throw new Win32Exception(Marshal.GetLastWin32Error(), "SetPrinter() failed. Couldn't set the printer settings");<br />
<br />
SendMessageTimeout(<br />
new IntPtr(HWND_BROADCAST), <br />
WM_SETTINGCHANGE, <br />
IntPtr.Zero, <br />
IntPtr.Zero, <br />
CLS_PrintPaper.SendMessageTimeoutFlags.SMTO_NORMAL, <br />
1000, <br />
out hDummy);<br />
}<br />
finally<br />
{<br />
ClosePrinter(hPrinter);<br />
}<br />
}<br />
else<br />
{<br />
StringBuilder strBuilder = new StringBuilder();<br />
strBuilder.AppendFormat("Failed to open the {0} printer, System error number: {1}",<br />
printerName, GetLastError());<br />
throw new ApplicationException(strBuilder.ToString());<br />
}<br />
}<br />
else<br />
{<br />
structDevMode pDevMode = new structDevMode();<br />
IntPtr hDC = CreateDC(null, printerName, null, ref pDevMode);<br />
if (hDC != IntPtr.Zero)<br />
{<br />
const long DM_PAPERSIZE = 0x00000002L;<br />
const long DM_PAPERLENGTH = 0x00000004L;<br />
const long DM_PAPERWIDTH = 0x00000008L;<br />
pDevMode.dmFields = (int)(DM_PAPERSIZE | DM_PAPERWIDTH | DM_PAPERLENGTH);<br />
pDevMode.dmPaperSize = 256;<br />
pDevMode.dmPaperWidth = (short)(widthMm * 1000.0);<br />
pDevMode.dmPaperLength = (short)(heightMm * 1000.0);<br />
ResetDC(hDC, ref pDevMode);<br />
DeleteDC(hDC);<br />
}<br />
}<br />
}<br />
<br />
public static void RemoveCustomPaperSizeToDefaultPrinter(string paperName)<br />
{<br />
PrintDocument pd = new PrintDocument();<br />
string sPrinterName = pd.PrinterSettings.PrinterName;<br />
RemoveCustomPaperSize(sPrinterName, paperName);<br />
}<br />
<br />
public static void RemoveCustomPaperSize(string printerName, string paperName)<br />
{<br />
if (PlatformID.Win32NT == Environment.OSVersion.Platform)<br />
{<br />
<br />
const int PRINTER_ACCESS_USE = 0x00000008;<br />
const int PRINTER_ACCESS_ADMINISTER = 0x00000004;<br />
<br />
structPrinterDefaults defaults = new structPrinterDefaults();<br />
defaults.pDatatype = null;<br />
defaults.pDevMode = IntPtr.Zero;<br />
defaults.DesiredAccess = PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE;<br />
<br />
IntPtr hPrinter = IntPtr.Zero;<br />
<br />
if (OpenPrinter(printerName, out hPrinter, ref defaults))<br />
{<br />
try<br />
{<br />
DeleteForm(hPrinter, paperName);<br />
<br />
IntPtr hDummy;<br />
SendMessageTimeout(<br />
new IntPtr(HWND_BROADCAST), <br />
WM_SETTINGCHANGE, <br />
IntPtr.Zero, <br />
IntPtr.Zero, <br />
CLS_PrintPaper.SendMessageTimeoutFlags.SMTO_NORMAL, <br />
1000, <br />
out hDummy);<br />
}<br />
finally<br />
{<br />
ClosePrinter(hPrinter);<br />
}<br />
}<br />
else<br />
{<br />
StringBuilder strBuilder = new StringBuilder();<br />
strBuilder.AppendFormat("Failed to open the {0} printer, System error number: {1}",<br />
printerName, GetLastError());<br />
throw new ApplicationException(strBuilder.ToString());<br />
}<br />
}<br />
else<br />
{<br />
<br />
}<br />
}<br />
}<br />
}<br />
|
|
|
|
|