Printing WPF - no paging, adjust page size instead - wpf

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?

Related

WPF printing - PrintableAreaWidth & Height stuck in landscape?

I'm printing a FlowDocument using the following code:-
var printDialog = new PrintDialog();
var result = printDialog.ShowDialog();
if (!result.Value)
{
return;
}
var pageWidth = printDialog.PrintableAreaWidth;
var pageHeight = printDialog.PrintableAreaHeight;
flowDoc.ColumnWidth = pageWidth;
flowDoc.PageWidth = pageWidth;
flowDoc.PageHeight = pageHeight;
var paginator = ((IDocumentPaginatorSource)flowDoc).DocumentPaginator;
printDialog.PrintDocument(paginator, "Name");
The print dialog shows the page as being A4 Portrait, however the values of printDialog.PrintableAreaWidth and .PrintableAreaHeight are 1122 & 793 respectively, i.e. landscape.
Changing the orientation or the paper size via the dialog has no effect on these values. What's going on?
Update
I've added a screenshot showing the PrintDialog properties. Notice how the PrintTicket property reflects the correct page size and orientation, yet the two PrintableArea... properties are the wrong way around.
I'm starting to think this is a "funny" with the printer/driver. I've tried printing to the "XPS Document Writer" printer, and the pages render correctly when I view the created file. (And if I view the PrintDialog's properties, the PrintableArea... properties correctly reflect an A4 portrait page).

Null values for PageMediaSize of a PrintTicket with Adobe PDF Creator using custom page size

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

Printing text in Silverlight that measures larger than page

I have a silverlight application that allows people to enter into a notes field which can be printed, the code used to do this is:
PrintDocument pd = new PrintDocument();
Viewbox box = new Viewbox();
TextBlock txt = new TextBlock();
txt.TextWrapping = TextWrapping.Wrap;
Paragraph pg = new Paragraph();
Run run = new Run();
pg = (Paragraph)rtText.Blocks[0];
run = (Run)pg.Inlines[0];
txt.Text = run.Text;
pd.PrintPage += (s, pe) =>
{
double grdHeight = pe.PrintableArea.Height - (pe.PageMargins.Top + pe.PageMargins.Bottom);
double grdWidth = pe.PrintableArea.Width - (pe.PageMargins.Left + pe.PageMargins.Right);
txt.Width = grdWidth;
txt.Height = grdHeight;
pe.PageVisual = txt;
};
pd.Print(lblTitle.Text);
This simply prints the content of the textbox on the page however some of the notes are spanning larger than the page itself causing it to be cut off. How can I change my code to measure the text and add more pages OR is there a better way to do the above where it will automatically create multiple pages for me?
There are several solutions to your problem, all of them under "Multiple Page Printing Silverlight" on Google. I was having a similar problem and tried most of them. The only one that worked for me was this one:
http://www.codeproject.com/Tips/248553/Silverlight-converting-to-image-and-printing-an-UI
But honestly you should look at Google first and see whether there are better solutions to your specific problem.
Answering your question, there is a flag called HasMorePages that indicates you need a new page. Just type pe.HasMorePages and you will see.
Hope it helps
First you need to work out how many pages are needed
Dim pagesNeeded As Integer = Math.Ceiling(gridHeight / pageHeight) 'gets number of pages needed
Then once the first page has been sent to the printer, you need to move that data out of view and bring the new data into view ready to print. I do this by converting the whole dataset into an image/UI element, i can then adjust Y value accordingly to bring the next set of required data on screen.
transformGroup.Children.Add(New TranslateTransform() With {.Y = -(pageIndex * pageHeight)})
Then once the number of needed pages is reached, tell the printer to stop
'sets if there is more than 1 page to print
If pagesLeft <= 0 Then
e.HasMorePages = False
Exit Sub
Else
e.HasMorePages = True
End If
Or if this is too much work, you can simply just scale all the notes to fit onto screen. Again probably by converting to UI element.
Hope this helps

PrintVisual to specific printer tray

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

WPF - Print content on same page

I have a requirement to print the WPF form contents on a Save button click. The content is plain text and will be name value pairs on each line. I don't need page breaks on consecutive Saves.
I have tried out the samples for PrintDialog, FlowDocument and FixedDocument and could not avoid the page break.
Is there any particular setting I am missing ?
Any alternative to PrintDialog ?
Code:
Paragraph myParagraph = new Paragraph();
myParagraph.Margin = new Thickness(0);
**myParagraph.BreakPageBefore = false;**
foreach (string line in textToPrint.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
{
myParagraph.Inlines.Add(new Run(line));
}
flowDocument.Blocks.Add(myParagraph);
DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator;
printDialog.PrintDocument(paginator, "Test Page");
A new Print job (Document) starts with a new page as far as I know. If you don't want page breaks gather all the values you want to print and then print them.

Resources