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

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?

Related

Scaling WPF Window for Printing

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"));
}

Microsoft Print to PDF not working when used comma(,) in file name

public void RenderCanvasImage(int maxRight, int maxBottom, Canvas surface, Transform transform)
{
RenderTargetBitmap renderBitmap = new RenderTargetBitmap(maxRight, maxBottom, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(surface);
PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() != true)
{
return;
}
Grid grid = new Grid();
Image myImage = new Image();
myImage.Source = renderBitmap;
myImage.Stretch = Stretch.Uniform;
grid.Children.Add(myImage);
Grid.SetColumn(myImage, 0);
Grid.SetRow(myImage, 1);
grid.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
grid.Arrange(new Rect(new Point(0, 0), grid.DesiredSize));
dialog.PrintVisual(grid, "My Canvas");
surface.LayoutTransform = transform;
}
I am using above code to print my canvas with Microsoft Print to PDF. Its working fine in all cases but when i enter comma in file name e.g.(Print,Test) while printing. It doesn't save my file and also not providing any error/exception code by which i could intimate the user to change file name.
Please help me to resolve my problem.
In short, don't do it
This is a known bug when printing to PDF
Use a different driver or don't put a comma in the file name
Its not only from WPF, its in general in certain situations.
Bug in "Print to PDF" and "Print to XPS" in Windows 10? comma in filename results in zero-byte file
Microsoft Print to PDF not working
Found a bug in Microsoft Print to PDF
Microsoft Print to PDF in Windows 10

In C#, what's the difference of PrinterSettings from a PrintDocument and PrintDialog?

Well, it's a very strange problem, and I already found a black magical solution. I'm curious about the reason.
I'm writing a program which use Brother QL-700 label printer to print labels. I need the labels be printed without showing the printer dialog.
The label printer support different size of label rolls and the default size of label roll is 29mm, while what I need is 62mm. I found that I can set the Page Size by
PrintDocument doc = new PrintDocument();
PaperSize size = new PaperSize() ;
size.Width = 244;//2.44 inch = 62mm
size.Height = 244;
size.RawKind = 256;//RawKind=0 does not work, I don't know why
doc.PrinterSettings.DefaultPageSettings.PaperSize = size;
However, this won't work, the driver of Printer would show a message saying the width does not fit.
But if I change the copy the PrinterSettings from a PrintDialog(), without showing it,
PrintDialog dlg = new PrintDialog();
doc.PrinterSettings = dlg.PrinterSettings;
Then it works.
In conclusion, what I don't understand is why
size.RawKind = 256;
and
PrintDialog dlg = new PrintDialog();
doc.PrinterSettings = dlg.PrinterSettings;
can make the printer work?
The documentation for RawKind does not state that 0 is a valid value. It also states that any value above 118 indicates a custom paper size. Since you are specifying a custom paper size, the value of 256 indicates that the size is custom and that is why it works.

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