I have a WPF user control and I want to be able to print it using PrintDialog.PrintVisual(). I don't want to show the print dialog so I want to be able to set a specific printer and printer tray to print it to. I figured out how to print to a specific printer but I need to print to "Tray 3" of my printer and I can't figure out how.
PrintDialog dialog = new PrintDialog();
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue pq = localPrintServer.GetPrintQueue("HC102-HP5SIMXX");
dialog.PrintQueue = pq;
//Set printer tray somehow
dialog.PrintVisual(myControl, "My control");
UPDATE: More info here:
http://social.msdn.microsoft.com/Forums/en-US/windowsxps/thread/f5859148-26f1-4e89-949c-180413bcc898/
http://www.wittersworld.com/selecting-the-input-tray-when-printing-xps-documents/
You have to use the GetPrintcapabilitiesAsXML to be able to get the full list of InputBins.
You can query InputBinCapability on PrintCapabilities to query the available InputBins.
The create a PrintTicket which chooses the tray via InputBin.
Then tell the PrintQueue to use the User ticket via the UserPrintTicket
http://msdn.microsoft.com/en-us/library/system.printing.printqueue.userprintticket
http://msdn.microsoft.com/en-us/library/system.printing.printcapabilities.inputbincapability
http://msdn.microsoft.com/en-us/library/system.printing.printticket
Related
I am trying to print to an Epson TM-H6000 Slip printer. I am able to print to the receipt printer but not the slip.
PrintDialog printDlg = new PrintDialog();
printDlg.ShowDialog();
FlowDocument doc = new FlowDocument(new Paragraph(new Run("Some text goes here")));
IDocumentPaginatorSource idpSource = doc;
printDlg.PrintDocument(idpSource.DocumentPaginator, "Testing");
Selecting the receipt will print the text to the receipt printer. Sending the same command to slip, it feeds the paper but does not print anything on it.
Edit: I should note that it does print a windows test page
I found a solution for this however it is not a good solution. Basically, if I set the font size 19 or larger it prints. If it is under 19 then it just feeds the page.
I don't know if this is an issue with MS PrintDocument or Epson's printer driver. If anyone has an idea please reply!
I'm writing a WPF application to get and save print profiles (and another application using this print profiles to print documents).
It works fine except when I try to use Adobe PDF Creator has a printer with custom size (like 800mm by 1200mm). Then the PageMediaSize Width and Height are null in the print ticket.
Here's the code I use to get the PrintTicket :
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == true)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += (o, a) =>
{
PrintQueue pq = pd.PrintQueue;
PrintTicket ticket = pd.PrintTicket;
...
a.Cancel = true;
};
doc.Print();
}
The PrindDialog contains the correct width and height for the page, but if I try to use the PrintTicket to print a document it crash, stating that PageMediaSize cannot contains null values.
Anyone have an idea on how to get a working PrintTicket ?
It sounds like a problem with Adobe PDF Creator's driver. The driver provides values for all the sizes, so if there is a problem, you might want to contact the makers of the printer/driver.
A way around this would be to figure out the sizes (800mm x 1200mm) and assume a resolution of 1/96 inches. Then do a conversion:
Width: ( 800mm) / (25.4 mm/in) / (1/96in) = 3023.62
Height: (1200mm) / (25.4 mm/in) / (1/96in) = 4535.43
And with those values you can say:
pd.PrintTicket.PageMediaSize = new PageMediaSize(Width, Height);
I have a till roll printer and I want to print on it. I don't need paging but I do need to adjust the page size instead. No matter what code I use the printer page size always uses the settings in the Printer Preferences.
PrinterSettings ps = new PrinterSettings();
ps.DefaultPageSettings.PaperSize = new PaperSize("rec", 200, 900);
System.Windows.Controls.PrintDialog printDialog = new PrintDialog();
MyPaginator paginator = new MyPaginator(print);
paginator.PageSize = new Size(200, 900);
printDialog.PrintDocument(paginator, "My Receipt");
I'm using a DocumentPaginator as I thought this would allow be to be more specific about the page I wanted to print.
How can I set the page size?
The ShowDialog method of the PrintDialog class in WPF is declared to return nullable bool (i.e. bool?), which is consistent with the documentation details:
"true if a user clicks Print; false if a user clicks Cancel;
or null if a user closes the dialog box without clicking Print or Cancel." from http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.showdialog.aspx
However, in the code below, no matter how I close the dialog, (I tried the X and Alt-F4) I can never make it return null. Unless I press the Print button, it is always false. This is my test code:
PrintDialog pd = new PrintDialog();
bool? result;
result = pd.ShowDialog();
Do you get the same behaviour? Is the documentation wrong or am I misinterpreting it or not testing correctly? Perhaps this is OS related, I am running Windows 7 Enterprise.
Thank you.
L
Greetings,
I have a problem with printing in WPF.
I am creating a flow document and add some controls to that flow document.
Print Preview works ok and i have no problem with printing from a print preview window.
The problem exists when I print directly to the printer without a print preview. But what is more surprisingly - when I use XPS Document Writer as a printer
everyting is ok, when i use some physical printer, some controls on my flow document are not displayed.
Thanks in advance
Important thing to note : You can use XpsDocumentWriter even when printing directly to a physical printer. Don't make the mistake I did of avoiding it just because you're not creating an .xps file!
Anyway - I had this same problem, and none of the DoEvents() hacks seemed to work. I also wasn't particularly happy about having to use them in the first place. In my situation some of the databound controls printed fine, but some others (nested UserControls) didnt. It was as if only one 'level' was being databound and the rest wouldn't bind even with a 'DoEvents()' hack.
The solution was simple though. Use XpsDocumentWriter like this. it will open a dialog where you can choose whichever installed physical printer you want.
// 8.5 x 11 paper
Size sz = new Size(96 * 8.5, 96 * 11);
// create your visual (this is a WPF UserControl)
var template = new PackingSlipTemplate()
{
DataContext = new PackingSlipViewModel(order)
};
// arrange
template.Measure(sz);
template.Arrange(new Rect(sz));
template.UpdateLayout();
// print to XpsDocumentWriter
// this will open a dialog and you can print to any installed printer
// not just a 'virtual' .xps file
PrintDocumentImageableArea area = null;
XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area,);
xps.Write(template);
I found the OReilly book on 'Programming WPF' quite useful with its chapter on Printing - found through Google Books.
If you don't want a print dialog to appear, but want to print directly to the default printer you can do the following. (For me the application is to print packing slips in a warehouse environment - and I don't want a dialog popping up every time).
var template = new PackingSlipTemplate()
{
DataContext = new PackingSlipViewModel(orders.Single())
};
// arrange
template.Measure(sz);
template.Arrange(new Rect(sz));
template.UpdateLayout();
LocalPrintServer localPrintServer = new LocalPrintServer();
var defaultPrintQueue = localPrintServer.DefaultPrintQueue;
XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue);
xps.Write(template, defaultPrinter.DefaultPrintTicket);
XPS Document can be printed without a problem
i have noticed one thing:
tip: the controls that are not displayed are the controls I am binding some data, so the conclusion is that the binding doesn't work. Can it be the case that binding is not executing before sending the document to the printer?