How to print with custom paper size in winforms
I'm trying to print a document in my application. But on different printers i get different results. This is my code:
PaperSize paperSize = new PaperSize("My Envelope", 440, 630);
paperSize.RawKind = (int)PaperKind.Custom;
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) => Console.Out.WriteLine("Printable Area for printer {0} = {1}", args.PageSettings.PrinterSettings.PrinterName, args.PageSettings.PrintableArea);
pd.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.Margins = new Margins(60, 40, 20, 20);
Console.Out.WriteLine("My paper size: " + pd.DefaultPageSettings.PaperSize);
PrintDialog printDialog = new PrintDialog(); // to choose printer
printDialog.Document = pd;
if (printDialog.ShowDialog(this) == DialogResult.OK)
{
// pd.DefaultPageSettings.PaperSize = paperSize; // uncomment to override size from dialog
Console.Out.WriteLine("Paper size for printer {0} = {1}", printDialog.PrinterSettings.PrinterName, pd.DefaultPageSettings.PaperSize);
_sptTxtControl.Print(pd);
}
When dialog shows I have two printers - Samsung and HP. This is the console output for these two:
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Paper size for printer HP LaserJet 1022n = [PaperSize A4 Kind=A4 Height=1169 Width=827]
Printable Area for printer HP LaserJet 1022n = {X=21,83333,Y=15,66667,Width=789,3333,Height=1137,333}
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Paper size for printer Samsung SCX-4x28 Series PCL6 = [PaperSize A4 Kind=A4 Height=1169 Width=827]
Printable Area for printer Samsung SCX-4x28 Series PCL6 = {X=17,33333,Y=17,16667,Width=792,3333,Height=1135,167}
You can see that the dialog is changing the size to A4. So if you uncommemt line after showdialog I'm enforcing papersize. The output when printing looks like this:
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Paper size for printer HP LaserJet 1022n = [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Printable Area for printer HP LaserJet 1022n = {X=21,83333,Y=15,66667,Width=789,3333,Height=1137,333}
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Paper size for printer Samsung SCX-4x28 Series PCL6 = [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Printable Area for printer Samsung SCX-4x28 Series PCL6 = {X=16,66667,Y=20,Width=400,1667,Height=589,8333}
You can see that Samsung printer has good Printable Area while HP has not. HP has always A4 size whatever I will change in code (set originatmargins etc.)
If I change my paper settings in print properties (sorry for Polish dialog):
and not change paper size after showing dialog the开发者_StackOverflow中文版n HP is printing everything ok. Output looks like this:
My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]
Paper size for printer HP LaserJet 1022n = [PaperSize My Envelop Format Kind=Custom Height=630 Width=440]
Printable Area for printer HP LaserJet 1022n = {X=18,66667,Y=16,Width=405,3333,Height=597,3333}
But I don't want to force user to save custom size for his printer. I have tried also this with a Kyocera printer - it works, but for two other HP printers it doesn't.
And the worst part is that Word 2010 prints ok the same RTF document with this size on both printers, co I cant't blame the HP driver.
Any ideas?
After the PrintDialog closes, don't just set
pd.DefaultPageSettings.PaperSize = paperSize;
Try also setting
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
I think that will take care of it.
setting
pd.DefaultPageSettings.PaperSize = paperSize;
and
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
might not work sometimes.
The most appropriate thing is to select a custom papersize that is installed in the Printers driver or Computer then setting the properties of the following
pd.DefaultPageSettings.PaperSize = ExistingPaperSize;
pd.PrinterSettings.PaperSize = ExistingPaperSize;
Like this code
PrintDocument pd = new PrintDocument();
pd.PrinterSettings = printdg.PrinterSettings;
PaperSize RequiredPaperSize = CalculatePaperSize(WIDTH,HEIGHT);
bool FoundMatchingPaperSize = false;
for (int index = 0; index < pd.PrinterSettings.PaperSizes.Count; index++)
{
if (pd.PrinterSettings.PaperSizes[index].Height == RequiredPaperSize.Height && pd.PrinterSettings.PaperSizes[index].Width == RequiredPaperSize.Width)
{
pd.PrinterSettings.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes[index];
pd.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes[index];
FoundMatchingPaperSize = true;
break;
}
}
//Method to calculate PaperSize from Centimeter to 1/100 of an inch
/// Caclulates the paper size
/// </summary>
/// <param name="WidthInCentimeters"></param>
/// <param name="HeightInCentimetres"></param>
/// <returns></returns>
public static System.Drawing.Printing.PaperSize CalculatePaperSize(double WidthInCentimeters,
double HeightInCentimetres)
{
int Width = int.Parse( ( Math.Round ((WidthInCentimeters*0.393701) * 100, 0, MidpointRounding.AwayFromZero) ).ToString() );
int Height = int.Parse( ( Math.Round ((HeightInCentimetres*0.393701) * 100, 0, MidpointRounding.AwayFromZero) ).ToString() );
PaperSize NewSize = new PaperSize();
NewSize.RawKind = (int)PaperKind.Custom;
NewSize.Width = Width;
NewSize.Height = Height;
NewSize.PaperName = "Letter";
return NewSize;
}
精彩评论