I hope you can help me with this issue. I am trying to build an
application in C# that converts text from XML with an XSL to a HTML
file and prints it according to some printersettings that can be made
by an user interface. This application needs to run as an automatic
process, so user intervention is not preferred.
I managed to get the XML+XSL->HTML conversion working with the
following code:
Expand|Select|Wrap|Line Numbers
- public static StringWriter MergeXSL_XML(string xml, string xsl)
- {
- try
- {
- XPathDocument xPath = new XPathDocument(xml);
- XslTransform xTrans = new XslTransform();
- xTrans.Load(xsl);
- StringWriter strW = new StringWriter();
- xTrans.Transform(xPath,null,strW);
- strW.Close();
- return strW;
- }
- catch(Exception e)
- {
- MessageBox.Show(e.ToString());
- return null;
- }
- }
correct. The string writer contains the information I wanted as HTML.
But now the bottleneck, I cannot print the HTML.
On some sites and newsgroups people run a command through the shell
that executes the printer option. But this needs a temporary file on
the server, but that's not what I want. Some also use:
Expand|Select|Wrap|Line Numbers
- wbElement.QueryStatusWB(SHDocVw.OLECMDID.OLECMDID_PRINT);
I already managed to place the HTML in the Active X component
Expand|Select|Wrap|Line Numbers
- wbCom.Navigate("about:blank");
- mshtml.IHTMLDocument2 hDoc = (mshtml.IHTMLDocument2)wbCom.Document;
- hDoc.write(html);
requesting wbCom.Document I won't get my edited document), but that
is not the real problem. The real problem is that I cannot parse the
document to the printer. There is a print document method in the item:
Expand|Select|Wrap|Line Numbers
- hDoc.execCommand("Print",false,0);
will cause a empty document to be print out. But by pressing a button
to start the print will print out a document. I already tried to insert
a small sleep, but how long the sleep does not matter, an empty
document will be printed.
The thing that I want is that the document can be parsed to a printable
document so I can print it with the default print components of
C#/.NET. Casting does not work, I tried all the IHTMLDocument* classes
to find something useful, but none of them seem to have a usable
function to create a printable document for the printer components in
..NET. One of the casts I tried:
Expand|Select|Wrap|Line Numbers
- MessageBox.Show(hDoc.ToString());
- printDialog1.Document = (PrintDocument)hDoc;
- printDialog1.Document = (PrintDocument)hDoc;
- hDoc.execCommand("Print",false,0);
I need to change the default printer settings in windows. For example,
I need to change the paper source, I can request the sources via:
Expand|Select|Wrap|Line Numbers
- for(int i=0; i < printDocument1.PrinterSettings.PaperSources.Count;
- i++)
- {
- MessageBox.Show(printDocument1.PrinterSettings.PaperSources[i].ToString());
- }
Can someone help me with this problem?
Thanks in advance...