troubleshooting Question

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

Avatar of Enid_JP
Enid_JP asked on
C#Microsoft Applications
2 Comments1 Solution1000 ViewsLast Modified:
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;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros