I am trying to print a FlowDocument directly to a pdf using Microsoft Print to pdf. Each time I want to print it sets the printer to the last printer that was used, I need it to set to "Microsoft Print to PDF".
// Create a PrintDialog
PrintDialog printDlg = new PrintDialog();
// Create a FlowDocument dynamically.
FlowDocument doc = CreateFlowDocument();
doc.Name = "OrderReceipt"+orderNo;
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Save PDF");
Is this possible?
You might be using the default printer setting, which can be set to the last printer used:
https://support.microsoft.com/en-us/help/4028622/windows-10-how-to-set-a-default-printer
Have you tried calling PrintDialog.ShowDialog()? It pops up the printer options and lets you select which printer to send to and set preferences.
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Display printer options
if( printDlg.ShowDialog() ?? false )
{
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Save PDF");
}
Related
I am having trouble printing from a TextBox in WPF
My Textbox will only contain a number between 1 and 999
I wanted to print font size 72 and the text enclosed within a 4" box (so they can cut around the edges)
private void InvokePrint(String contentToPrint)
{
// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
// Display the dialog. This returns true if the user presses the Print button.
Nullable<Boolean> print = pDialog.ShowDialog();
if (print == true)
{
FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Printing Label");
}
}
I got the code from Microsoft's site but they are using an XPS printer. I want to use the default selected printer (usually an HP).
Any help would greatly be appreciated
So I am printing out some text from a textbox, which has wordwrapping enabled, but when I print out the document, the string that should be wordwrapped does not, and cuts offs on the page. I'd like to simply enable word wrapping for my printed document. Also is there a way to set the margins for the printed document, some of the text gets cut off from both the top and left margins. Thanks! Code attached below.
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight);
FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;
MessageBox.Show(page1.Height.ToString() + page1.Width);
GridType.Children.Remove(Textbox);
page1.Children.Add(Textbox);
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
document.Pages.Add(page1Content);
printDlg.PrintDocument(document.DocumentPaginator, "My first document");
page1.Children.Remove(Textbox);
GridType.Children.Add(Textbox);
Your textbox setting won't applied in PrintDoc .. you must recalculate all in your printdoc printpage event
I have my main form and then launch a new form. The new form is in the front. I send data to the printer which opens a print window popup. (prints to default, no printer to select) It then defaults back to my first form and I have to do a BringtoFront on the 2nd form after 1ms. This is an ok fix because it is only a small blip, but is there a way to hide that print window popup all together so that it just prints in the background?
// code to print
PrintDocument prnDocument;
string printername; //Get the default printer name.
prnDocument = new PrintDocument();
printername = Convert.ToString(prnDocument.PrinterSettings.PrinterName);
if (string.IsNullOrEmpty(printername))
throw new Exception("No default printer is set.Printing failed!");
prnDocument.PrintPage += new PrintPageEventHandler(prnDoc_PrintPage);
prnDocument.Print();
According to this SO post, you should be able to hide the print dialog by doing the following:
PrintDocument printDocument = new PrintDocument();
PrintController printController = new StandardPrintController();
printDocument.PrintController = printController;
Is this what you mean?
hello Friends Today I am trying to make small wpf app present for my wife. I want she can wrıte at runtime and print what she write in flowdocument. ı can do everthings at design time but ı want write ar run time here my solution probably ı must use ı ınotifyProperty changed or binding text of flowdocument to printDlg What should I do can you hellp me please
/// <summary>
/// This method creates a dynamic FlowDocument. You can add anything to this
/// FlowDocument that you would like to send to the printer
/// </summary>
/// <returns></returns>
private FlowDocument CreateFlowDocument()
{
// Create a FlowDocument
FlowDocument doc = new FlowDocument();
// Create a Section
Section sec = new Section();
// Create first Paragraph
Paragraph p1 = new Paragraph();
// Create and add a new Bold, Italic and Underline
Bold bld = new Bold();
bld.Inlines.Add(new Run("First Paragraph"));
Italic italicBld = new Italic();
italicBld.Inlines.Add(bld);
Underline underlineItalicBld = new Underline();
underlineItalicBld.Inlines.Add(italicBld);
// Add Bold, Italic, Underline to Paragraph
p1.Inlines.Add(underlineItalicBld);
// Add Paragraph to Section
sec.Blocks.Add(p1);
// Add Section to FlowDocument
doc.Blocks.Add(sec);
return doc;
}
private void print(object sender, RoutedEventArgs e)
{
// Create a PrintDialog
PrintDialog printDlg = new PrintDialog();
// Create a FlowDocument dynamically.
FlowDocument doc = CreateFlowDocument();
doc.Name = "FlowDoc";
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Hello WPF Printing.");
}
For WYSIWYG editing, WPF provides a ready-made control. The WPF RichTextBox control can edit XAML Flow Documents natively.
http://msdn.microsoft.com/en-us/magazine/cc163371.aspx
I am totaly forgot lıke you said FlowDocument in RichTextBox is Editable here ı Fınd the best solution you can print and save pdf,xps.format
/****************************************************/
// Handle "Save RichTextBox Content" button click.
private void SaveRTBContent(Object sender, RoutedEventArgs args)
{
// Clone the source document's content into a new FlowDocument.
// This is because the pagination for the printer needs to be
// done differently than the pagination for the displayed page.
// We print the copy, rather that the original FlowDocument.
MemoryStream s = new MemoryStream();
TextRange source = new TextRange(document.ContentStart, document.ContentEnd);
source.Save(s, DataFormats.Xaml);
FlowDocument copy = new FlowDocument();
TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd);
dest.Load(s, DataFormats.Xaml);
// Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog,
// and allowing the user to select a printer.
// get information about the dimensions of the seleted printer+media.
PrintDocumentImageableArea ia = null;
System.Windows.Xps.XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(ref ia);
if (docWriter != null && ia != null)
{
DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
// Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);
Thickness t = new Thickness(72); // copy.PagePadding;
copy.PagePadding = new Thickness(
Math.Max(ia.OriginWidth, t.Left),
Math.Max(ia.OriginHeight, t.Top),
Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),
Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));
copy.ColumnWidth = double.PositiveInfinity;
//copy.PageWidth = 528; // allow the page to be the natural with of the output device
// Send content to the printer.
docWriter.Write(paginator);
}
}
I have a WPF application and I use external library for generating documents. This library returns document as System.Drawing.Printing.PrintDocument. How can I print this document in WPF? I can use Print() method directly, but I need to allow user to select printer and settings. If I use WPF PrintDocument dialog, I can't set my document to it as in WinForms dialog.Document. Is there a way to convert old PrintDocument to some WPF friendly form?
WinForms way:
// get document for printing
PrintDocument document = exporter.GetPrintDocument();
System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
dialog.Document = document;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
document.Print();
}
WPF way:
System.Windows.Controls.PrintDialog dialog = new System.Windows.Controls.PrintDialog();
if (dialog.ShowDialog() == true)
{
// how to print old PrintDocument???
dialog.PrintDocument(...);
}
I also tried to open WinForms dialog in WPF but it is not possible. Dialog is just not shown.
Thanks for any help.
I found an answer. You have to set UseDialogEx dialog property to true.
MessageBox.Show(printDialog1.PrinterSettings.PrinterName);
printDialog1.PrinterSettings.PrintFileName = "A.txt";
MessageBox.Show(printDialog1.PrinterSettings.PrintFileName);
printDialog1.ShowDialog();
printDocument1.DocumentName = "A.txt";
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDocument1.Print();
}