Scaling WPF Window for Printing - c#

Good Evening,
I am working on a small WPF app in VS2017 which uses c#. Its essentially just to make calculations for my workers in the field. I have everything built and tested, however, I am running into an issue when it comes to printing the results. I have scoured the corners of the internet and have gotten as far as getting the print dialog box to open and even print to PDF. The issue i am having is that when it prints, it is in full scale. I just need to be able to scale the app window to 80-90% of its size and then print. i will add the code and see if i am just overlooking something.
private void InvokePrint(object sender, RoutedEventArgs e)
{
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min((ActualWidth*.9),ActualHeight);
//get the size of the printer page
Size sz = new Size((Width*.9), ActualHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(0,0), sz));
//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(this, "Offset Calculations");
What it is printing
What I want to print

add reference to ReachFramework.dll 4.0
PrintDialog dlg = new PrintDialog();
if ((bool)dlg.ShowDialog().GetValueOrDefault()) {
//switch landscape
dlg.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);
//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new System.Windows.Point((int)capabilities.PageImageableArea.OriginWidth, (int)capabilities.PageImageableArea.OriginHeight), sz));
//show the print dialog
dlg.PrintVisual(this, "MyDoc_" + DateTime.Now.ToString("yyyyMMdd_HHmmss"));
}

Related

WPF C# Print image in Zebra GK888t (ZDesigner) printer

Environment:
Zebra (ZDesigner) GK888t EPL label printer
10cm x 5cm paper
Windows drivers ( any other do not work )
Configurated printer options in Windows printer preferences
width of paper: 10cm
height of paper: 5cm
media settings : direct thermal
Problem: I want to print generated image, but I don't want to show PrintDialog for user.
User can only print image by pressing button "PRINT", but i can't achieve it.
Working version with PrintDialog dialog:
Image image = new Image();
PrintDialog dlg = new PrintDialog();
bool? result = dlg.ShowDialog();
PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
image.Measure(sz);
image.Arrange(
new Rect(
new Point(
capabilities.PageImageableArea.OriginWidth,
capabilities.PageImageableArea.OriginHeight
)
,sz
)
);
dlg.PrintVisual(image, "Print a Large Image");
Version without PrintDialog dialog:
Image image = new Image();
PrintDialog dlg = new PrintDialog();
dlg.PageRange = new PageRange(1);
PrintQueue printQueue = new PrintServer()
.GetPrintQueues().ToList()
.FirstOrDefault(x => x.FullName.Contains("ZDesigner GK888t (EPL)"));
dlg.PrintQueue = printQueue;
PrintCapabilities capabilities = dlg.PrintQueue.GetPrintCapabilities(dlg.PrintTicket);
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
dlg.PrintTicket.PageMediaSize = new PageMediaSize(sz.Width, sz.Height);
image.Measure(sz);
image.Arrange(
new Rect(
new Point(
capabilities.PageImageableArea.OriginWidth,
capabilities.PageImageableArea.OriginHeight
)
,sz
)
);
dlg.PrintVisual(image, "Print a Large Image");
But this code do not work, Zebra printer in this case do not print, or print blank label, or stop in the middle of label and stop working (flashing red light)
Question: How to fix it? I guess it doesn't work well because configuration of printer isn't apply ( like paper size or media settings - direct thermal option) because of getting it from PrintQueues. Any other ways to print generated image?

Different print dimensions from different printers by same C# Windows Form Application

The same C# application is printing text in different dimension by different printers. The text printed is same along x-axis but differ along y-axis. I mean in one print, the text is slightly(4-5mm) upwards than other print but same along x-axis i.e. no text is backwards or forward than text of other print. Eg:
"This text is same along x-axis but differ among y-axis"(Print 1)
"This text is same along x-axis but differ among y-axis"(Print 2)
My Page settings are:
private void PaperSettings()
{
PaperSize paperSize = new PaperSize("New Page", 377, 1095);
paperSize.RawKind = (int)PaperKind.Custom;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
Margins margin = new Margins(0, 0, 0, 0);
printDocument1.DefaultPageSettings.Margins = margin;
printDocument1.DefaultPageSettings.Landscape = true;
PrinterSettings printer = new PrinterSettings();
printDocument1.PrinterSettings.PrinterName = printer.PrinterName;
}
On changing the paper width(377) manually, the text is shifted upwards on increasing it and downwards on decreasing it. However the same page settings is not working on different printers*(HP Officejet J3500 printing text slightly downdards than text printed by HP Deskjet 1510).*
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(label1Payee.Text, label1Payee.Font, Brushes.Black, label1Payee.Location.X, label1Payee.Location.Y);
e.Graphics.DrawString(labelAmountWords.Text, labelAmountWords.Font, Brushes.Black, labelAmountWords.Location.X, labelAmountWords.Location.Y);
e.Graphics.DrawString(labelDate1.Text, labelDate1.Font, Brushes.Black, labelDate1.Location.X, labelDate1.Location.Y);
e.Graphics.DrawString(labelAmount.Text, labelAmount.Font, Brushes.Black, labelAmount.Location.X, labelAmount.Location.Y);
}
Any Suggestions..Thank you!
Every printer has some margins where the printer does not print. These margins are called printer's HardMargins. Say you want to print something on co-ordinates (0, 0) but my printer prints it on (16, 16). This (HardMarginX, HardMarginY).
Every printer may differ from hard margins. e.g. My HP LaserJet 1020 is (16, 16) but my Canon Pixma ip1300 is (0, 0).
So what you have to do is, get the HardMarginX and HardMarginY, do some math and print where you want to print.
One more thing, if you try to print beyond these margins, document may be clipped. Search for the PrintableArea of the printer over internet and you get the idea.

WPF printing on paper results in clipping

I am using the WPF PrintVisual method in order to print an object. Check my code below.
PrintDialog dialog = new PrintDialog();
System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
Double height = dialog.PrintableAreaHeight;
Double width = dialog.PrintableAreaWidth;
myObj.Measure(new Size(width, height));
myObj.Arrange(new Rect(new Point((capabilities.PageImageableArea.OriginWidth), (capabilities.PageImageableArea.OriginHeight)), new Size(width, height)));
dialog.PrintVisual(myObj, "Print in WPF with fit to printing page");
If I print this in xps, it works very well. But when I print on paper (A4 => 8.5 x 11 inches) then the object gets clipped from the right as it generates the margin at the left side of the paper. Can anyone tell me how I can avoid this margin on the paper and print the object full length?
Thanks
It is very suspicious that you are using dialog.PrintableAreaHeight and dialog.PrintableAreaWidth for the printable size, but capabilities.PageImageableArea for the printable origin. These are unrelated measurements; try using
new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight)
in myObj.Measure and myObj.Arrange.

Issue printing image as expected

I have an image that I'm trying to print to legal size. However, there are a few challenges to this.
The image will vary in size. This is because I'm using a control that has limited print options but can export to an image.
I want to maximize the print area on the page. Smallest margin possible
The user needs to be able to select a printer and set options
Here's the code I'm using:
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile(Globals.TempDirectory + #"\temp.jpg");
Point p = new Point(100, 100);
Margins margins = new Margins(50, 50, 50, 50);
pd.DefaultPageSettings.Margins = margins;
args.Graphics.DrawImage(i, p);
};
pd.Print();
I've been having trouble with this because I can't set margins and can't seem to get the print out right. I want it to print in legal but when I print the image, it's not rotated properly and it just prints to a default printer. I'm up for anything to get this to work.
Printing in C# sucks
try a
printdialog()
to allow the user to select a printer and settings. once you get that to work the rest of it might click for you.
Edit: Showing you where and how to use it.
PrintDialog pDialog = new PrintDialog();
if (pDialog.ShowDialog() == DialogResult.OK)
{
pd.PrinterSettings = pDialog.PrinterSettings;
pd.Print();
}

ZEBRA label printing

I need to print labels on my ZEBRA S4M printer.
PrintDocument pd = new PrintDocument { PrinterSettings = new PrinterSettings { PrinterName = "ZDesigner S4M-203dpi ZPL" } };
pd.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
And when using Portrait landscape - it's alright, but in Landscape mode I have a big space from the left side of label.. How can I print a horizontal text on my labels?
Thanks a lot.
ADDED
I think that this problem is possible because of small PrintingArea property that I can't change. It's size 300x200 and real paper size is about 200x500...
I believe you have two options, either try changing your default paper size, or performing a translate.

Categories

Resources