PrintDocument: Fit to page - c#

I am using PdfiumViewer in order to print a PDF file:
using (PdfDocument document = PdfDocument.Load(pdfFileName))
{
using (PrintDocument printDocument = document.CreatePrintDocument())
{
printDocument.BeginPrint += PrintDocument_BeginPrint;
printDocument.EndPrint += PrintDocument_EndPrint;
printDocument.PrintPage += PrintDocument_PrintPage;
printDocument.QueryPageSettings += PrintDocument_QueryPageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.Print();
}
}
In my printDocument BeginPrint, EndPrint, PrintPage and QueryPageSettings events I am not doing anything, only making some logs to a file.
The PDF file is printed but the left and right margins of each page of the document are being cut off so I am trying to fit each PDF document page to the current default selected paper size in the default printer. How can I do this?

Finally, I have solved the problem by initializing the CreatePrintDocument constructor with PdfPrintMode.ShrinkToMargin:
using (PdfDocument document = PdfDocument.Load(pdfFileName))
{
using (PrintDocument printDocument = document.CreatePrintDocument(PdfPrintMode.ShrinkToMargin))
{
printDocument.BeginPrint += PrintDocument_BeginPrint;
printDocument.EndPrint += PrintDocument_EndPrint;
printDocument.PrintPage += PrintDocument_PrintPage;
printDocument.QueryPageSettings += PrintDocument_QueryPageSettings;
printDocument.PrintController = new StandardPrintController();
printDocument.Print();
}
}

Related

Printing from a preview (PrintPreviewDialog) without calling PrintPageEventHandler again, and recreating the PrintDocument

I'm certain I'm missing something here...
I create a PrintDocument (which is very time consuming 600-1200 pages), and then allow the user to preview it with PrintPreviewDialog.
However, when the user chooses to print the document (from the preview dialog) the print document gets created again. I just want the document that has been created, previewed, accepted by the user, to be printed. How do I print the created document directly?
The code is simply this...
PageSettings pageSettings = new PageSettings();
pageSettings.Margins.Top = 40;
pageSettings.Margins.Bottom = 40;
pageSettings.Margins.Left = 40;
pageSettings.Margins.Right = 40;
LoadChartsBook chartsBook = new LoadChartsBook(chartsData);
PrintDocument docToPrint = chartsBook.CreatePrintDocument();
docToPrint.DefaultPageSettings = pageSettings;
PrintPreviewDialog previewDialog = new PrintPreviewDialog();
previewDialog.Document = docToPrint;
previewDialog.ShowDialog();
Then the called code for CreatePrintDocument()
public PrintDocument CreatePrintDocument()
{
index.resetCounter();
coverPagePrinted = false;
currentChartIndex = 0;
pageNumber = 0;
PrintDocument printDocument = new PrintDocument();
printDocument.DocumentName = (index.totalEntries()+1).ToString() + ", of Load Charts Book: " + chartBookID;
printDocument.PrintPage += new PrintPageEventHandler(PrintLoadChartsBookEventHandler);
return printDocument;
}
Each chart is very processor intensive and hence time consuming to create, I need to avoid this apparent second call to CreatePrintDocument() but can't see why it's being called in again anyway.
Am I missing a setting in the preview dialog?
Any help appreciated.

How to send cut command in Thermal Printer using PrintDocument in c#

I am Using PrintDocument to print the billing content. But I am unable to send auto cut command. I am using the code below to print content.
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.PrinterSettings.PrinterName = "TestPrinter";
printDocument.Print();
printDocument.Dispose();
Can anyone help me which command I have to use for auto cut?
I have tried a combination of below two code blocks
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += PrintDocumentOnPrintPage;
printDocument.PrinterSettings.PrinterName = "TestPrinter";
printDocument.Print();
printDocument.Dispose();
and at the end, I am sending below commands it's working fine for me
string GS = Convert.ToString((char)29);
string ESC = Convert.ToString((char)27);
string documentName ="x"
string COMMAND = this.textBox.T`ext;
COMMAND = ESC + "#";
COMMAND += GS + "V" + (char)1;
RawPrinterHelper.SendStringToPrinter(this.textBox1.Text, COMMAND,documentName);

How to print word document using PrintDocument class

My requirement is to print a Word document using c#.
Since Interop Word is not recommended to use at server side, I would like to print Word file using PrintDocument class.
So, how to print Word document using c#?
I tried the below code, but it printed out 2 blank pages:
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "E:\\WordPrint\\Output\\TEST.docx";
printDoc.DefaultPageSettings.PrinterSettings.PrinterName = "Bullzip PDF Printer";
printDoc.DefaultPageSettings.PrinterSettings.Copies = 2;
printDoc.Print();
try
{
streamToPrint = new StreamReader ("C:\\My Documents\\MyFile.txt");
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler (this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
You can find more information on the msdn site:
https://msdn.microsoft.com/de-de/library/system.drawing.printing.printdocument(v=vs.110).aspx

Print multiple files with same printer setting

I am currently opening the PrintDialog where user can select printer setting and do the printing.
At the moment I am using below code
var files = Directory.GetFiles(sourceFolder);
foreach (var file in files)
{
var pdoc = new PrintDocument();
var pdi = new PrintDialog
{
Document = pdoc
};
if (pdi.ShowDialog() == DialogResult.OK)
{
pdoc.DocumentName = file;
pdoc.Print();
}
}
Is there a way to send all the files to the printer by using PrintDialog once. So the user can select the folder and set one print setting for all the documents inside the folder and then do the printing?
Try this sample code:
var files = Directory.GetFiles(sourceFolder);
if (files.Length != 0)
{
using (var pdoc = new PrintDocument())
using (var pdi = new PrintDialog { Document = pdoc, UseEXDialog = true })
{
if (pdi.ShowDialog() == DialogResult.OK)
{
pdoc.PrinterSettings = pdi.PrinterSettings;
// ************************************
// Pay attention to the following line:
pdoc.PrintPage += pd_PrintPage;
// ************************************
foreach (var file in files)
{
pdoc.DocumentName = file;
pdoc.Print();
}
}
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
string file = ((PrintDocument)sender).DocumentName; // Current file name
// Do printing of the Document
...
}

Printing two seperate documents without two print dialogues

I need to print 2 different copies of this receipt using the same printer, and with only one print dialogue. Right now, the first copy prints fine, but then the fax dialogue comes up for the second one, because that's my default printer.
How would I do both using one printer? Or is there a way to print to a non default printer without the print dialogue. In this case, the printer will never change.
Thanks!
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.BuildCustomerReciept);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
PrintDocument pdd = new PrintDocument();
pdd.PrintPage += new PrintPageEventHandler(this.BuildStoreReciept);
PrintDialog pddi = new PrintDialog();
pddi.Document = pdd;
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
pdd.Print();
}
Did you tried that?
...
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Specify the printer to use. You can check its name in control panel
pd.PrinterSettings.PrinterName = "NameofThePrinter";
pd.Print();
...

Categories

Resources