471,306 Members | 838 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Printing HTML from memory (no user interaction)

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
  1. public static StringWriter MergeXSL_XML(string xml, string xsl)
  2. {
  3. try
  4. {
  5. XPathDocument xPath = new XPathDocument(xml);
  6. XslTransform xTrans = new XslTransform();
  7. xTrans.Load(xsl);
  8. StringWriter strW = new StringWriter();
  9. xTrans.Transform(xPath,null,strW);
  10. strW.Close();
  11. return strW;
  12. }
  13. catch(Exception e)
  14. {
  15. MessageBox.Show(e.ToString());
  16. return null;
  17. }
  18. }
The XML and XSL are read from a file or database. This is working
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
  1. wbElement.QueryStatusWB(SHDocVw.OLECMDID.OLECMDID_PRINT);
But that does not give any satisfying results.
I already managed to place the HTML in the Active X component

Expand|Select|Wrap|Line Numbers
  1. wbCom.Navigate("about:blank");
  2. mshtml.IHTMLDocument2 hDoc = (mshtml.IHTMLDocument2)wbCom.Document;
  3. hDoc.write(html);
The item shows the HMTL, but I cannot request the data later (when
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
  1. hDoc.execCommand("Print",false,0);
But calling it shortly after the placement of the HTML in the document
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
  1. MessageBox.Show(hDoc.ToString());
  2. printDialog1.Document = (PrintDocument)hDoc;
  3. printDialog1.Document = (PrintDocument)hDoc;
  4. hDoc.execCommand("Print",false,0);
Another problem is that it seems I cannot change the printer settings,
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
  1. for(int i=0; i < printDocument1.PrinterSettings.PaperSources.Count;
  2. i++)
  3. {
  4. MessageBox.Show(printDocument1.PrinterSettings.PaperSources[i].ToString());
  5. }
But I am not able to change them

Can someone help me with this problem?

Thanks in advance...

Jul 21 '05 #1
0 1134

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by prakash.guru | last post: by
2 posts views Thread by news.ntlworld.com | last post: by
3 posts views Thread by sheldonlg | last post: by
1 post views Thread by dwij2u | last post: by
2 posts views Thread by =?Utf-8?B?RmlsaXBwbw==?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.