i already can create a print to print a file in my windows forms. However, whenever i add this code:
printDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
I can't see the Orientation of the page become LandScape, it is still Portrait.
How do I make it LandScape as default? So, whenever i click PrintPreview or PrintFile, the Orientation of the page will become LandScape, not Portrait.
Here is the code:
private void PrintPreview(object sender, EventArgs e)
{
PrintPreviewDialog _PrintPreview = new PrintPreviewDialog();
_PrintPreview.Document = printDocument1;
((Form)_PrintPreview).WindowState = FormWindowState.Maximized;
_PrintPreview.ShowDialog();
}
private void PrintFile(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Test Page Print";
printDocument1.Print();
}
}
try setting the Landscape of PrintDocument as follows,
printDocument1.DefaultPageSettings.Landscape = true;
The pdfprinting.net library is excellent for implementing the print functionality and be productive. Here is the simple code snippet to set print orientation to landscape(pdfPrint.IsLandscape = true;)
var pdfPrint = new PdfPrint("demoCompany", "demoKey");
string pdfFile = #"c:\test\test.pdf";
pdfPrint.IsLandscape = true;
int numberOfPages = pdfPrint.GetNumberOfPages(pdfFile);
var status = pdfPrint.Print(pdfFile);
if (status == PdfPrint.Status.OK) {
// check the result status of the Print method
// your code here
}
// if you have pdf document in byte array that is also supported
byte[] pdfContent = YourCustomMethodWhichReturnsPdfDocumentAsByteArray();
status = pdfPrint.Print(pdfFile);
Related
I am currently developing a WPF application using C# language and .Net Framework 4.8
In the MainWindow I have the Print menu button to generate a Fixed Document like this
private void clPrintMenu_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
/* Margins is a User-defined structure to set Top, Right, Bottom and Left values
in Cm, Inches and Pixels */
Margins margins = new Margins(21, 29.7);
margins.Unit = Units.Pixel;
Size sz = new Size(margins.Left.Value, margins.Top.Value);
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = sz;
/* CashJournal is a UserControl designed as an A4 sheet the list below contains several
Cashjournal which represent multiple pages */
List<CashJournal> journals = CashJournal.PrintJournals;
foreach (CashJournal jrl in journals)
{
FixedPage page = new FixedPage();
page.Width = margins.Left.Value;
page.Height = margins.Top.Value;
FixedPage.SetLeft(jrl, 0);
FixedPage.SetTop(jrl, -20);
page.Children.Add(jrl);
PageContent content = new PageContent();
((IAddChild)content).AddChild(page);
document.Pages.Add(content);
}
/* The document is then passed to a window for preview */
CashPrintPreview dialog = new CashPrintPreview(selectedTab, document);
dialog.ShowDialog();
}
In the CashPrintPreview, the document is displayed in a DocumentViewer which has a Print button. I modifed the Print() CommandBinding to bind the function to my custom function PrintPreview
private void PrintView(object sender, ExecutedRoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
bool? rslt = dialog.ShowDialog();
if (rslt != true)
return;
/* This block is my problem */
PrintQueue queue = dialog.PrintQueue;
PrintCapabilities capabilities = queue.GetPrintCapabilities();
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
document.DocumentPaginator.PageSize = sz;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(queue);
writer.Write(document);
}
When I choose the XPS printer from the PrintDialog, the created file render perfectly as it appears in the preview. But when I choose the PDF printer from Adobe the document is not well scaled like too much margins on top and not enough margins on left.
How can I resolve this issue. Thanks.
PS. Please be explicit.
I am finally able to print a FixedDocument to PDF using the Acrobat printer. I just needed to pass my document to the PrintDocument function of the chosen printer:
private void PrintView(object sender, ExecutedRoutedEventArgs e)
{
PrintDialog dialog = new PrintDialog();
bool? rslt = dialog.ShowDialog();
if (rslt != true)
return;
dialog.PrintDocument(((IDocumentPaginatorSource)document).DocumentPaginator, "");
}
I am facing a problem to print using print document C#. When I am going to print and input 1 to 3 pages from 9 pages using printer setting and then command for print, this command cannot print 1 to 3 pages, this command print by default all documents. How can I fix this?
This is the code:
private void PrintPreview_Click(object sender, EventArgs e)
{
ToolStripButton b = new ToolStripButton();
b.Text = "print";
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Click += new System.EventHandler(this.button2_Click);
printPreviewDialog1.WindowState = FormWindowState.Maximized;
if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printPreviewDialog1.Show();
}
}
private void Print_Click(object sender, EventArgs e)
{
PrintDialog p1 = new PrintDialog();
p1.AllowSelection = true;
p1.AllowSomePages = true;
if (p1.ShowDialog ()== DialogResult.OK)
{
printDocument1.Print();
}
}
My boss wants me to create the window form that has printing function, but he wants to print the datagridview after preview.
So now I encourage the problem, I cannot print multiple set of paper or choose the printer or make any changes when click the print button on printpreviewdialog.When I click the button, it will direct print the paper. So I wish to join the printpreviewdialog and printdialog.
Why the printpreviewdialog and printdialog can only be used in different buttons? It's lack of usibility when needed to click one button to preview and click another button to print multiple set and make changes of printer.
Any one can help me?
printdialog
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
position = 0;
pageno = 1;
printDocument2.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
printDocument2.OriginAtMargins = true;
printPreviewDialog1.Document = printDocument2;
printPreviewDialog1.ShowDialog();
}
printpreviewdialog
printDocument3.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
printDocument3.OriginAtMargins = true;
//((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;
printPreviewDialog1.Document = printDocument3;
printPreviewDialog1.ShowDialog();
I know it is late, but i think someone will still need that.
As Hans Passant say, "print preview is heavily depended on printer and page settings."
But there is a print-button in printpreviewdialog, which is still reasonable for most cases. But that button directly prints to your default printer, and never shows a dialog.
If you want a print dialog from printpreview dialog, you can just manipulate ToolStrip of PrintPreviewDialog.
Here it goes (assuming you initialized printPreviewDialog1, printDialog1 and printDocument1 objects)
printPreviewDialog1.Document = printDocument1;
ToolStripButton b = new ToolStripButton();
b.Image = Properties.Resources.PrintIcon;
b.DisplayStyle = ToolStripItemDisplayStyle.Image;
b.Click += printPreview_PrintClick;
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
printPreviewDialog1.ShowDialog();
Using above code, you can remove default print button on ToolStrip of PrintPreview and replace it with a newly created "print button". This button now has an Click event handler, and by using it, you can show the PrintDialog.
private void printPreview_PrintClick(object sender, EventArgs e)
{
try
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ToString());
}
}
Works good...
Have one tip...
You can re-use the current icon by:
this.ToolStripButton.Image = ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).ImageList.Images[0];
The rest of the snippet:
{
this.ToolStripButton = new System.Windows.Forms.ToolStripButton();
this.ToolStripButton.Image = ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).ImageList.Images[0];
this.ToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.ToolStripButton.Click += new System.EventHandler(this.printPreview_PrintClick);
((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).Items.RemoveAt(0);
((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).Items.Insert(0, ToolStripButton);
}
private void printPreview_PrintClick(object sender, System.EventArgs ee)
{
try
{
this.printDialog.Document = printDocument;
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDocument.Print();
}
}
catch (System.Exception ex)
{
System.Windows.MessageBox.Show(ex.Message, ToString());
}
}
private System.Windows.Forms.ToolStripButton ToolStripButton;
The snippet provided by #AceIndy above, does not take into account if the user changes the default printer or its settings. This is how I solved that problem:
private void printPreview_PrintClick(object sender, EventArgs e)
{
try
{
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument.PrinterSettings = printDialog.PrinterSettings;
printDocument.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ToString());
}
}
How can I set the print Preview and print code to landscape orientation ?
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(700, 600);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Visible = false;
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage_1);
This did the trick
this.printDocument1.DefaultPageSettings.Landscape = true;
Won't this
var doc = new PrintDocument();
doc.DefaultPageSettings.Landscape = true;
do the trick?
It will probably take care of the print preview issue as well.
This works for me
private void button_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printDocument1.DefaultPageSettings.Landscape = true;
printPreviewDialog1.ShowDialog();
}
I am creating a PDF File in my application, and then I print it (works fine.)
When I print this pdf on another computer/Printer it doesn't look the same! I want it so that it always looks the same, on whichever printer I print on.
Maybe I have to set the borders? Like this:
PrinterSettings ps = new PrinterSettings();
ps.DefaultPageSettings.HardMarginX = 0;
ps.DefaultPageSettings.HardMarginY = 0;
But HardMargin is not writable. Have you guys got some ideas?
Try to set up this way:
PrintDocument printDocument1 = new PrintDocument();
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = "Printer name";// optional
//printerSettings.PrinterName = "HP Officejet J6400 series";
printDocument1.PrinterSettings = printerSettings;
printDocument1.PrintPage += printDocument1_PrintPage;
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
// in the dialog, you can set up the paper size, etc.
printDialog1.UseEXDialog = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
Handler function:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//This print form a rich textbox, but you can render pdf here.
//e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
//e.Graphics.PageUnit = GraphicsUnit.Inch;
}