1

I have some trouble using the webbrowser control in C#, especially when I print it. The thing is, I have a Barcode in my html file and I have to print it (I use a font to create the code). When I open the html file with Firefox or any other Web browser, my barecode is good and I can scan it. But, when I open my file with my webbrowser in c#, or when I print it, the webbrowser ad 2 characters after the barecode. And, when I print the file, my document is not centered, it's like the webbrowser add a margin-left property. So my question is, is there any way to print an html file, using a webbroser, exactly how I see the html file when I use firefox or chrome for example. Here is the code I use.

curDir = Directory.GetCurrentDirectory();
webBrowserA4.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintDocument);
webBrowserA4.Url = new Uri(String.Format("file:///{0}/print.html", curDir));

private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).Print();
    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

EDIT: So, now, I have another problem, here is the screen of what my file looks like when I print it: https://i.stack.imgur.com/OTd1a.jpg As you can see, there is a "margin-left", and I can't remove it. I also want to remove this "page 1 of 1" and the Title.

Keenegan
  • 91
  • 7
  • Can you post your HTML page code. or only the portion you have the barcode in. could help other people figuring out why. BTW if you didn't know the web browser control is internet explorer so you should try printing with IE itself and also check default print margins. i don't think the web browser control use the default settings of IE for that. – Franck Sep 26 '13 at 11:49
  • My html code is generated by a Stingbuilder, and its look like
    Ík74Î
    in the html file. Then I use a css font property. When I open my html file with IE it's work, so I think that the problem come from my webbrowser control.
    – Keenegan Sep 26 '13 at 12:53
  • Don't forget IE have compatibility settings that render correctly stuff that are not in reality. May i suggest you create a simple bitmap by code and use the functionality to write text in picutres using your codebar font and then simply show that code bar as also oing so i would suggest gif format with monochrome color so it will be matters of couple bytes in size. – Franck Sep 26 '13 at 13:37
  • Graphics is the object and function is like .DrawText or .DrawString – Franck Sep 26 '13 at 13:38
  • 1
    @Keenegan, you're probably after `FEATURE_BROWSER_EMULATION`, [more info](http://stackoverflow.com/a/18802626/1768303). – noseratio Sep 26 '13 at 16:01
  • 1
    I was a bit busy, but I tried this http://www.clarionedge.com/windows/development/webbrowser-control-feature_browser_emulation.html and it seems to work, thanks Noseratio. I still have work, but if I find something I'll post it ;) Thanks – Keenegan Sep 27 '13 at 15:41
  • **EDIT**: So, now, I have another problem, here is the screen of what my file looks like when I print it: http://imgur.com/q7ovEA1 As you can see, there is a "margin-left", and I can't remove it. I also want to remove this "page 1 of 1" and the Title. – Keenegan Sep 30 '13 at 08:45

0 Answers0