How to print word document using PrintDocument class - c#

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

Related

Silent printing from WPF

I would like to disable this printing dialog:
What should I do to remove this dialog?
var dlg = new PrintDialog();
dlg.PageRangeSelection = PageRangeSelection.AllPages;
dlg.UserPageRangeEnabled = false;
var doc = new FlowDocument();
doc.PageWidth = 275;
doc.FontSize = 18;
doc.TextAlignment = TextAlignment.Center;
doc.Blocks.Add(new Paragraph(new Run("nXXXXXXXXXXXX")));
dlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Receipt");
You can use PrintDocument class instead of PrintDialog.See the Microsoft's implementation
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);
}

PrintDocument: Fit to page

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

Printing to a thermal printer with ASP.NET C# WebForms (4.5)

I have a requirement for a project at work to print to a thermal printer (specifically a Citizen CT-S651) that is attached to the computer via USB from ASP.NET C# Webforms. So far, I have tried a few things and did a lot of research. The main three things I have tried was using QZ Tray (formerly known as JZebra, found here https://qz.io/), however the non-free version costs too much right now, and the free version is too annoying in its popups to be of much use right now. I also tried to generate a PDF using MigraDoc (http://www.pdfsharp.net/), but when I try to print to the printer, it uses far too much paper before it even prints "Hello World" code for that shown below:
private void print()
{
Document document = CreateDocument();
document.UseCmykColor = true;
const bool unicode = false;
const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
pdfRenderer.Document = document;
pdfRenderer.RenderDocument();
//pdfRenderer.Save("C:/Users/jamesl/Documents/Visual Studio 2013/Projects/TestCheckScanner/TestCheckScanner/TestDocument.pdf");
MemoryStream stream = new MemoryStream();
pdfRenderer.PdfDocument.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
}
private Document CreateDocument()
{
Document document = new Document();
MigraDoc.DocumentObjectModel.Unit width, height;
width = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(80);
height = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(50);
Section section = document.AddSection();
section.PageSetup.PageHeight = height;
section.PageSetup.PageWidth = width;
section.PageSetup.PageHeight = height;
section.PageSetup.PageWidth = width;
section.PageSetup.LeftMargin = 0;
section.PageSetup.RightMargin = 0;
section.PageSetup.TopMargin = 0;
Paragraph paragraph = section.AddParagraph();
paragraph.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black; //Same as System.Drawing.Color
paragraph.AddFormattedText("Hello, World!");
return document;
}
Since that hasn't worked very well, I have gotten it to print a little bit using PrintDocument, however, the main problems I have with this way are twofold: 1. Formatting it print a proper receipt is going to be very tedious although if it is ends up being the best way with my current constraints I am all for it. 2. This method only works if I print from Visual Studio, when I try it on the test web app I have set up on a server, I get an error of "Settings to access printer 'CITIZEN CT-S651' are not valid." Here is my code for that:
private void print()
{
try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.PrinterSettings.PrinterName = "CITIZEN CT-S651";
pd.Print();
}
catch (Exception ex)
{
Response.Write(ex.Message);
Response.End();
}
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("Test Print", new System.Drawing.Font("Arial", 12), new SolidBrush(Color.Black), 60, 0);
e.Graphics.DrawString("Test Again", new System.Drawing.Font("Arial", 12), new SolidBrush(Color.Black), 60, 20);
}
Any help you can give on this matter would be awesome!

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

Unable to view MICR font in print preview

I have written a code to print a cheque using MICR font, every thing works fine but when coming to print preview I am getting normal text but while printing the document I am getting the required in MICR font. How can I show MICR font in print preview
This is my code
PrivateFontCollection PFC = new PrivateFontCollection();
PFC.AddFontFile(Server.MapPath("ADVMICR.TTF"));
FontFamily fm = new FontFamily(PFC.Families[0].Name, PFC);
Font PrintFont = new Font(fm, 12);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Specify the printer to use.
pd.PrinterSettings.PrinterName = "SnagIt 9";
PrintPreviewDialog ppdlg = new PrintPreviewDialog();
ppdlg.Document = pd;
ppdlg.ShowDialog();
Try this
ppdlg.PrintPreviewControl.Font = new Font("ADVMICR.TTF", 12);

Categories

Resources