Print using Print Document Control + Using CutePDF writer

Posted by Bubbly under C# category on | Views : 9304
Print using Print Document Control + Using CutePDF writer

This Code will write to a PDF file using CutePDF Writer software and print. While save it will directly print to pdf file with ease.
You can download CutePDF writer from this link :

This can also be used to write in english and hindi fonts together to a PDF file. Just need to replace font type to Mangal. And contents to be written must be in hindi.

bool[] bCode = new bool[95]; //barcode bit data array
bool bValidCode = false; //flag that indicates a valid code
const double dPrintPenSize = 3.0, //size of printing pen (33mm)
dPrintHeight = 20.0, //height of printing area (2.5cm)
dPrintXOffset = 2, //offsets used when printing
dPrintYOffset = 2;
const int iPrintWidth = 100, //width of printing area
iXOffset = 2; //X offset (padding)

PrintDialog pDia = new PrintDialog();
PrinterSettings ps = new PrinterSettings();
pDia.Document = printDocumentMessageBoxTest;
pDia.Document.DocumentName = "Your filename here";

ps.PrinterName = "CutePDF Writer";
ps.PrintToFile = false;
ps.PrintFileName = "C:\\" + pDia.Document.DocumentName + ".pdf";

// take printer settings from the dialog and set into the PrintDocument object
pDia.Document.OriginAtMargins = true;
ps.DefaultPageSettings.Margins.Left = 2;
printDocumentMessageBoxTest.PrinterSettings = ps;

// start the printing process, catch exceptions
catch (Exception exc)
MessageBox.Show("Printing error!\n" + exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

On print page event of Print document control pass all values like string to be printed, font, color, etc using Graphics.Drawing method.
Also include using System.Drawing.Printing; namespace

private void printDocumentMessageBoxTest_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
e.Graphics.DrawString("Your string here", new Font("Arial", 10), Brushes.Black, new PointF(iXOffset + (int)dPrintXOffset, (float)2 + (float)dPrintYOffset));

Comments or Responses

Login to post response