M
Midnight Walker
Guest
I've written a C# print test project with pre-printed form - System.Drawing.Printing.PrintDocument. The purpose of it is to print the image from a file on custom paper size. Before to print the image is scaling down according to the paper size.
public PrintDocument printDoc = new PrintDocument();
.....
private void PrintButton_Click(object sender, EventArgs e)
{
string FileName = "D:\\temp\\testprint.png";
try
{
if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected.
PrintDocument pd = new PrintDocument();
PaperSize paperSize = new PaperSize("TEST PAPER SIZE", 50, 50);
paperSize.RawKind = (int)PaperKind.Custom;
//Disable the printing document pop-up dialog shown during printing.
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
//For testing only: Hardcoded set paper size to particular paper.
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += (sndr, args) =>
{
System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);
args.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
//Adjust the size of the image to the page to print the full image without loosing any part of the image.
System.Drawing.Rectangle m = args.MarginBounds;
//Logic below maintains Aspect Ratio.
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
//Calculating optimal orientation.
pd.DefaultPageSettings.Landscape = m.Width > m.Height;
//Putting image in center of page.
m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
args.Graphics.DrawImage(i, m);
};
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The image scaling works fine but paper size isn't changing it always stay A4. I've googled a lot and applied some options but still can't set custom paper properties. Can anyone help me to find out what the problem is? Or at least point me where to dig on?
Thanks in advance!
Continue reading...
public PrintDocument printDoc = new PrintDocument();
.....
private void PrintButton_Click(object sender, EventArgs e)
{
string FileName = "D:\\temp\\testprint.png";
try
{
if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected.
PrintDocument pd = new PrintDocument();
PaperSize paperSize = new PaperSize("TEST PAPER SIZE", 50, 50);
paperSize.RawKind = (int)PaperKind.Custom;
//Disable the printing document pop-up dialog shown during printing.
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
//For testing only: Hardcoded set paper size to particular paper.
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += (sndr, args) =>
{
System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);
args.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
//Adjust the size of the image to the page to print the full image without loosing any part of the image.
System.Drawing.Rectangle m = args.MarginBounds;
//Logic below maintains Aspect Ratio.
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
//Calculating optimal orientation.
pd.DefaultPageSettings.Landscape = m.Width > m.Height;
//Putting image in center of page.
m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
args.Graphics.DrawImage(i, m);
};
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The image scaling works fine but paper size isn't changing it always stay A4. I've googled a lot and applied some options but still can't set custom paper properties. Can anyone help me to find out what the problem is? Or at least point me where to dig on?
Thanks in advance!
Continue reading...