Print/PrintPreview xml transformed file/html file from Windows form(c#)

Posted on 2009-12-16
Last Modified: 2012-05-08
I have an XML document that uses a schema and styesheet and can be displayed as HTMl in a browser;
it contains tables, etc.
I am trying to display the file using the PrintPreview dialog; but a blank document is displayed.
Am I missing some step here?
Result.html is what I want to display in the printpreview dialog and then print.
The code is as follows:
private void PrintBtn_Click(object sender, EventArgs e)
            printPreviewDialog1.Document = printDocument1;

        private void ReadDocument()
            string tempPath = System.IO.Path.GetTempPath();
            string srcDocName =  tempPath + "Result.xml"; //original XML file
            string destDocName = tempPath + "Result.html";
            // Load the style sheet.
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(tempPath + "Result.xsl");
            // Execute the transform and output the results to a file.
            xslt.Transform(srcDocName, destDocName);// Result.XML is transformed to Result.html
                       printDocument1.DocumentName = destDocName;
            using (FileStream stream = new FileStream(srcDocName, FileMode.Open))
            using (StreamReader reader = new StreamReader(stream))
                documentContents = reader.ReadToEnd();
            stringToPrint = documentContents;

        void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
            int charactersOnPage = 0;
            int linesPerPage = 0;
            // Sets the value of charactersOnPage to the number of characters
            // of stringToPrint that will fit within the bounds of the page.
            e.Graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);
            // Draws the string within the bounds of the page.
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);
            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);
            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);
            // If there are no more pages, reset the string to be printed.
            if (!e.HasMorePages)
                stringToPrint = documentContents;
Question by:Enid_JP

    Author Comment

    Or is it possible to open the IE browser and then open the browser's print preview dialog?

    Accepted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now