Hi all,
I have windows form with certain labels,Textboxes and a button.
A pdf will be created when the button is clicked .Now I need to directly print the pdf to a printer without saving it or opening it.That is when the button is clicked the pdf should be created and print it directly.
What I have tried:
private void button1_Click(object sender, EventArgs e)
{
try
{
string lab7 = label7.Text.Length.ToString();
int countSpaces = label7.Text.Count(Char.IsWhiteSpace);
PdfDocument pdf = new PdfDocument();
PdfPage pdfPage = pdf.AddPage();
pdfPage.Orientation = PageOrientation.Portrait;
XGraphics graph = XGraphics.FromPdfPage(pdfPage);
XFont font = new XFont("Verdana", 10, XFontStyle.Regular);
graph.DrawString(dateTimePicker1.Text, font, XBrushes.Black,
new XRect(5.708661 * 72, 0.787402 * 72, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
graph.DrawString(label3.Text, font, XBrushes.Black,
new XRect(1.5748 * 72, 1.37795 * 72, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
graph.DrawString("***" + label5.Text + "***", font, XBrushes.Black,
new XRect(5.708661417 * 72, 2.165354331 * 72, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
if (countSpaces >= 9)
{
var firstSpaceIndex = label7.Text.IndexOf(" ", label7.Text.IndexOf(" ", label7.Text.IndexOf(" ", label7.Text.IndexOf(" ", label7.Text.IndexOf(" ", label7.Text.IndexOf(" ", label7.Text.IndexOf(" ", label7.Text.IndexOf(" ") + 1) + 1) + 1) + 1) + 1) + 1) + 1);
var firstString = label7.Text.Substring(0, firstSpaceIndex);
var secondString = label7.Text.Substring(firstSpaceIndex + 1);
graph.DrawString("***" + firstString, font, XBrushes.Black,
new XRect(1.06299 * 72, 1.771653543 * 72, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
graph.DrawString(secondString + "***", font, XBrushes.Black,
new XRect(0.393701 * 72, 2.12598 * 72, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
else
{
graph.DrawString("***" + label7.Text + "***", font, XBrushes.Black,
new XRect(1.06299 * 72, 1.771653543 * 72, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
}
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "PDF document (*.pdf)|*.pdf";
DialogResult result = dialog.ShowDialog();
string fileName = dialog.FileName;
if (result == DialogResult.OK)
{
pdf.Save(fileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is what I have tried.This will save the pdf to our computer.But I don't need that I need to directly print the pdf on button click.Any help will be really appreciated.Thanks in advance.