Hi there,

My name is Tsjelle,
I'm a Bachelor in the Informatics.

I'm writing a program in vb.net that get's an xml-file as input and somewhere in the xml-file there is an attribute that's in fact an HTML-page. And it's that page I need to get printed.

I'm at the point that this HTML-page is in a string, i've tried all possible ways and just had a search of 3 days on the net, but i still can't find a simple solution.

It's a sort of confirmation of an order, the HTML-file.

If someone can help me?

Thanks in advance,

Tsjelle

Dani AI

Generated

Brief summary: the HTML snippet inside the XML is already available as a string (). Earlier replies suggested XSL (), questioned XML encoding () and floated rendering+printing via a browser control (). The most practical, low-dependency route on Windows/WinForms is to render that HTML with a browser engine and print the rendered document; the rest below gives a robust VB.NET pattern, common pitfalls, and alternatives.

Practical VB.NET pattern (WinForms WebBrowser, silent or dialog printing)

  • Ensure the HTML string has correct encoding and a valid head/body; if resources use relative URLs add a base href (see next paragraph).
  • Create an invisible WebBrowser, assign the HTML, wait for DocumentCompleted, then invoke printing. A minimal handler:
' Add COM reference "Microsoft Internet Controls" (SHDocVw)
Dim wb As New WebBrowser()
wb.ScriptErrorsSuppressed = True
AddHandler wb.DocumentCompleted, AddressOf wb_DocumentCompleted
wb.DocumentText = htmlString

Private Sub wb_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs)
    Dim ax = CType(CType(sender, WebBrowser).ActiveXInstance, SHDocVw.WebBrowser)
    ' ExecWB(6,2,...) => OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER (silent)
    ax.ExecWB(6, 2, Nothing, Nothing)
End Sub

Key cautions and troubleshooting

  • The WebBrowser control requires an STA thread and a message loop; running it from a console/background thread needs an STA thread with Application.Run or a hidden form.
  • Relative images/CSS often fail: inject a <base href="file:///C:/path/"> into the HTML head or save the HTML to a temp file and navigate to that file so resources resolve.
  • DocumentCompleted can fire before images finish loading; if prints are blank or missing assets, wait briefly after completion or inject a small onload script to notify when everything is loaded.
  • The WinForms WebBrowser uses the system IE engine; newer HTML/CSS may not render as expected unless the process emulation is adjusted or a modern engine is used.

Alternatives

  • For modern rendering or server-side generation, use headless Chromium (PuppeteerSharp), wkhtmltopdf, or an embedded Chromium (CefSharp) to render-to-PDF then print — these are more robust for CSS3 and silent output but require extra dependencies.
  • If the HTML in the XML is raw, prefer storing it inside CDATA or base64 so XML parsing is safe; extract with XmlDocument/XDocument before rendering.

This guidance complements the thread suggestions by giving a working VB.NET pattern, handling base paths and asset loading, and pointing to modern rendering alternatives for complicated layouts.

Recommended Answers

All 7 Replies

you have to search about java script print form

Do you have an example of the part of the XML file that has the html (I thought that the html tags would cause an error in the XML file)? You may need to read the XML into a dataset and then retrieve the element that has the html then remove anything that looks like an html tag and send it to the printer.

Hi man,

i don't understand well wahat are you said, please update with code to know in which part i will help you if i don't get the answer for you, if your problem when you retrive your xml to html you can do that with xsl and in this we'll not able to have any proplem with any tag.

Hey, I think that he is saying that the html is located within xml tags.

Yes, but you have to know that the XML File must related with XSL File
Like that

<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='Months.XSL'?>

And you design in your XSL html tags as you wish

Yes, I know how xml and xsl are related but how would an html document look nested in xml tags?

Why not just extract the HTML part from the XML file, display it in a hidden WebBrowser control and then print it from there?

That would be the same as printing a normal webpage from, say Internet Explorer using
File->Print. Right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.