Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
Looks like a simple thing for me initially. After diving in to this from few days am feeling the heat.

I have a xml+xsl=html string which needs to printed on server side. Am saving HTML string in to a file.
Now am facing a lot of problems with printing.

if am printing with process. am unable to stop print dialog. Code as follows

C#
Process printJob = new Process();
      printJob.StartInfo.FileName = localReportPath + filename + ".html";
      printJob.StartInfo.UseShellExecute = true;
      printJob.StartInfo.Verb = "printto";
      printJob.StartInfo.CreateNoWindow = true;
      printJob.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      printJob.StartInfo.Arguments = string.Format("{0} {1}", "Microsoft XPS Document Writer", "xx");
      //printJob.StartInfo.Arguments = "\"" + printerAddress + "\"" + " " + "Microsoft XPS Document Writer";
      printJob.StartInfo.WorkingDirectory = Path.GetDirectoryName(localReportPath + filename + ".html");
      printJob.Start();

But if am printing using PrintDocument object. The raw content of the file getting printed. some where i lost the connection. here is the code.

C#
StreamReader streamToPrint;
public void printdoc(string doc1)
{
    using (PrintDocument doc = new PrintDocument())
    {
        streamToPrint = new StreamReader(doc1);
        ///doc.PrintPage += this.html_PrintPage;
        doc.DocumentName = "Hello";
        doc.DefaultPageSettings.Landscape = false;
        doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
        doc.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
        doc.PrintController = new StandardPrintController();
        doc.PrinterSettings.PrintFileName = doc1;
        doc.Print();
    }
    //PrintDocument doc = new PrintDocument();
    //doc.PrintController = new StandardPrintController();
    //doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);

    //doc.Print();
}

public void doc_PrintPage(object sender, PrintPageEventArgs ev)
{
    Font printFont = new Font(FontFamily.GenericSerif, 12);
    float linesPerPage = 0;
    float yPos = 0;
    int count = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null;

    // Calculate the number of lines per page.
    linesPerPage = ev.MarginBounds.Height /
       printFont.GetHeight(ev.Graphics);

    // Print each line of the file.
    while (count < linesPerPage &&
       ((line = streamToPrint.ReadLine()) != null))
    {
        yPos = topMargin + (count *
           printFont.GetHeight(ev.Graphics));
        ev.Graphics.DrawString(line, printFont, Brushes.Black,
           leftMargin, yPos, new StringFormat());
        count++;
    }

    // If more lines exist, print another page.
    if (line != null)
        ev.HasMorePages = true;
    else
        ev.HasMorePages = false;
}


also can any one provide me a DOS CMD script for printing a html page or a html file on hard disk


Regards,
Pavan N
Posted

1 solution

Hi,

if you want to follow your first way with "printto" Verb in process the only way to cancel printing is to Kill() your process.

For second way do you had a view into your saved file and it's right encoded?
It seems you're using the doc_PrintPage method from msdn example, I would put
C#
Font printFont = new Font(FontFamily.GenericSerif, 12);
back before creating a new PrintDocument and declare the font
C#
printFont = new Font("Arial", 10);


Does this Code work?
C#
private void printButton_Click(object sender, EventArgs e)
   {
       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);
       }
   }


Well, exactly a similar CP Article.

Print HTML in C# with or without the web browser control and the print dialog[^]


With Best Regards
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900