简体   繁体   中英

Printing HTML in A4 and A5 paper format

Apparently a question about Printing HTML in A4 and A5 format is the exact same thing as asking about Printing RTF formatted text , so here's another question with the details a bit more laid out.

I am developing a C# (WinForms) program that should print orders. This program is written for the .NET 2.0 framework. The program has to be able to print in both A4 and A5 paper formats , without any user interferrance (no dialogs). The printed order should look exactly like it does in a webbrowser. You can check out this sample if you like to see what it should look like. As you can see, very basic stuff.

Here's what I have tried so far:

  • Asking a question on StackOverflow , got closed for being a duplicate. Never found the duplicate.
  • Print with the WebBrowser element. Cannot print A5, so that was no solution.
  • Put the HTML in RichTextBox and print that. It worked, but it didnt show the HTML like it's viewed in a webbrowser, which is a requirement. Code can be found here .
  • I've looked into converting to PDF before printing, but that is either expensive or just impossible to use (for me, as a programmer with little C# experience). These tools usually rely on software being installed on the users' computer (like Acrobat Reader for printing), which I'm trying to avoid.
  • Viewed about every relevant link in Google for at least 13 pages, no luck. I've been at this for about 2 and a half days now.

If someone has a ( free ) better way to print formatted HTML like it's viewed in the browser without user interferrence or external dependancies, please share. I really need this to work.

Please don't close this question, believe me when I say I actively searched for a solution or article that describes my problem. Some were relevant, but did not solve the problem that I'm having. I also used the advanced searched on this website, with no luck.

Thank you for taking the time to read this.

Note: When I say I never found the duplicate, I mean I never found the article that literally describes my problem.

You're going to have to bite the bullet and use a PDF library, there is no way you are going to be able to fully control the end printed result from HTML with so many different users, browsers and printers. I did printing to A4 for an internal business application, we had a very limited user base (maybe 5) and all printing to a specific printer, even then it was flaky at best. I don't believe there is a way to distinguish between A4 and A5 without user interference, ie they have to select paper size from the print options.

Suggest you take a look at PDF Sharp , Sharp PDF and iTextSharp , they are all OpenSource.

This can all be done from the server, ie nothing needs to be installed on the users machine. It should be possible to select the paper size using this (I am not 100% sure), but what the end user prints it on ultimately up to them.

I have done this successfully using the PrintHelper method described here . I used it to enable users in multiple locations to print barcode labels from a CMS system. The labels had strict requirements regarding layout, font size and positioning and all this was managed using HTML and CSS.

The PrinterHelper class works by passing it a webcontrol populated with the print data (I used a repeater to allow multiple labels) & the helper class builds a page in memory and opens the print dialog. You register your CSS like so:

pg.ClientScript.RegisterStartupScript(pg.GetType(),"LabelCSS","<link href=\"Styles/labelPrint.css\" rel=\"stylesheet\" type=\"text/css\" />");

One caveat though, it only worked with Firefox, and some settings had to be changed eg margins set to zero, but as the CMS required the use of that browser it wasn't a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM