Printing HTML in A4 and A5 paper format - c#

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, i.e. 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, i.e. 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 e.g. margins set to zero, but as the CMS required the use of that browser it wasn't a problem.

Related

Creating PDFs Online

We are using Report Definition laguage (RDL) templates to define various reports in one of our Sharepoint applications. These reports are (then) saved as PDFs into various SharePoint Document Library's. One report in-particular renders, but is considered to be "failing" due to the styling needs of the report. So it appears RDL only understand "very simple" HTML.
For Example:
Trademark characters are not rendering as superscript (they render as normal text instead)
The ability to assign Line Height fails
The ability to assign Word Spacing fails (so printers "leading" requirements fail)
Both of these point to various marked Microsoft limitation for RDL's to interprint various HTML...of which we are now aware.
So...
I need a better tool...and we are scratching our heads on this one!
QUESTION:
What tools take-in HTML, understand CSS (well!) and can generate PDFs from C-Sharp objects?
Please keep in-mind I need the to PDF generator tools you recommend (below) to understand CSS and HTML.
NOTE:
I looked at the various other StackEchange sites to see if there is a better forum for this particular question, but this one was the only one that seemed to fit-the-bill. If you are a mediator, and feel this question is mis-placed, please feel free to move this question.
This HTML to PDF converter has the most accurate conversion of a complex html/css page. There is also a demo to try the conversion with your html
Maybe you can give Amyuni WebkitPDF a try. It is a Free component for converting HTML+CSS into PDF files. From the home page:
Directly convert HTML files into PDF without the use of a web browser or a printer driver
Convert HTML files into XAML/XPS for rendering within Silverlight
Integrate and deploy the HTML conversion feature within your applications
Generate either a single continuous PDF page or split the HTML into multiple PDF pages
Amyuni WebkitPDF is distributed as a library with a sample application, and sample code for C++ and C#.
Disclaimer: I currently work as software developer at Amyuni Technologies.
I only know a workaround for the "leading space" issue. This example "leads" the value with 10 spaces:
=space(10) & Fields!FieldName.Value
This should work for any renderer, I'll update this if I come around other tricks.
Have a look at Aspose.Pdf for .NET: http://www.aspose.com/categories/.net-components/aspose.pdf-for-.net/default.aspx

Quick & Easy PDF Viewer Ideas

This question has been asked several times but my situation is a little different.
I have a web application written in C# where I get a string value passed to this page I'm working on. This string value represents a filename of a PDF file I need to display on this page. I'm supposed to have a left panel where I have some information displayed, and a right panel showing the contents of a PDF file. I'm using a simple table here to separate the panels. All PCs should already have Acrobat Reader installed.
My question is simple. How can I display the contents of the PDF file within this table? I don't need anything fancy. It has to be free and simple enough for a newbie like me to set up. It could even be written in jQuery/AJAX, if there even is a way.
I've looked at a Webbrowser control within an ASP.NET page, but it looks way too complicated for a simple viewer. I looked at Webparts, but I'm not sure if that's doable in a non-Sharepoint environment. Suggestions?
Have you tried the object tag? More ideas are here Make PDF display inline instead of separate Acrobat Reader window

Is there a way to replace a text in a PDF file with itextsharp?

I'm using itextsharp to generate the PDFs, but I need to change some text dynamically.
I know that it's possible to change if there's any AcroField, but my PDF doen's have any of it. It just has some pure texts and I need to change some of them.
Does anyone know how to do it?
Actually, I have a blog post on how to do it! But like IanGilham said, it depends on whether you have control over the original PDF. The basic idea is you setup a form on the page and replace the form fields with the text you want. (You can style the form so it doesn't look like a form)
If you don't have control over the PDF, let me know how to do it!
Here is a link to the full post:
Using a template to programmatically create PDFs with C# and iTextSharp
I haven't used itextsharp, but I have been using PDFNet SDK to explore the content of a large pile of PDFs for localisation over the last few weeks.
I would say that what you require is absolutely achievable, but how difficult it is will depend entirely on how much control you have over the quality of the files. In my case, the files can be constructed from any combination of images, text in any random order, tables, forms, paths, single pixel graphics and scanned pages, some of which are composed from hundreds of smaller images. Let's just say we're having fun with it.
In the PDFTron way of doing things, you would have to implement a viewer (sample available), and add some code over a text selection. Given the complexities of the format, it may be necessary to implement a simple editor in a secondary dialog with the ability to expand the selection to the next line (or whatever other fundamental object is used to make up text). The string could then be edited and applied by copying the entire page of the document into a new page, replacing the selected elements with your new string. You would probably have to do some mathematics to get this to work well though, as just about everything in PDF is located on the page by means of an affine transform.
Good luck. I'm sure there are people on here with some experience of itextsharp and PDF in general.
This question comes up from time to time on the mailing list. The same answer is given time and time again - NO. See this thread for the official answer from the person who created iText.
This question should be a FAQ on the itextsharp tag wiki.

C# - Templated Printing from Object(s)

I'm in need of a solution to print or export (pdf/doc) from C#. I want to be able to design a template with place holders, bind an object (or xml) to this template, and get out a finished document.
I'm not really sure if this is a reporting solution or not.
I also don't want to have to roll my own printing / graphics code -- I'd like all display concerns handled in a template.
I initially think of this as something Crystal Reports can do (although I've never used CR), but I'm not sure if I'm abusing the system here -- I'm not really interested in binding ADO.NET datasets at the moment (screw datasets). Can Crystal deal with binding to objects?
Does SSRS or WPF play in this field too?
A subset of WPF-P is XPS which can be used to present your objects via databinding.
One of the best choices if you are already using WPF.
Google Keywords: XPS, FixedDocument, FlowDocument, WPF Printing
Might read through this thread:
http://groups.google.com/group/nhusers/browse_thread/thread/e2c2b8f834ae7ea8
Seems a lot of people like iTextSharp
http://itextsharp.sourceforge.net/
For Word docs, look into Word's Mail Merge feature and Word automation. I did this recently in a form letter printing project. Basically what I did was create a Word template file (file extension .dot) and in this template file I defined MergeFields in a standard form letter. My application queries a database for the records it needs to print and then for each record it returns it matches fields in the database with these merge fields and sends the result (the merged doc) to the printer.
It's working really well and if I had a link that gave a definitive explanation, I'd provide it (check back here, I'll see if I can't find the most useful ones). Hopefully I've provided enough keywords to let you find your own resources. I can go into more detail if you need.
I've never had to export PDF files but for a project I'm working on now I'll have to. For a free solution my research has lead to iTextSharp (like Will Shaver points out) but I've only done the initial investigations and I have found a few pay solutions I might end up resorting to.

Best way to print Invoices, Pick Tickets etc

Well, heres my scenario.
Client/Server winforms application with SQL Express as the DB. I need to be able to print invoice, packing slips etc..
i would like the customer to be able to modify the invoices. ie. be able to put their logo or change font sizes etc...basically format the display.
Things i have considered so far are.
1) Use a template engine (similar to codesmith or mygeneration) and use templates that output HTML. Then print the html page.
2) Use ReportViewer in local mode. I've heard that users can download a plugin for web dev express and edit the local report files. can anyone confirm this?
3) Use Reportviewer in remote mode.
I don't have much experience with ReportViewer so I'm not sure if i should use local or remote mode as well.
Those of you that have done this kind of thing before whats your recommendation?
After just completing a project with it, I would heartily recommend iTextSharp to create your invoices and other forms as PDFs. In addition to creating PDFs from scratch, you can also use it to fill in PDF forms and/or templates created with Acrobat (or even MS Office/OpenOffice). And it's free.
It's pretty easy to use in Windows apps or in ASP.Net applications. Most of the documentation and the books on it (iText in Action, for example) are about the original Java version, iText. However, there are tutorials and example code on the conversion process and, for the most part, all of the functions and libraries work the same in the .Net version, so adapting the book and reference code has been no problem.
I definitely learned the hard way that HTML and CSS are great for browsers (well, great except for the "every browser interprets it different" problem), but horrible for trying to generate consistent, attractive, and precise printed output and forms.
I'm personally using Aspose Words: they use word documents as templates, and I'm using Words bookmarks function to mark and retrieve the fields I need to fill.
Aspose works nicely with Tables (ie: you can add lines to a table, etc...) and sees Word documents as XML documents. You can then save the document as MSWord or PDF.
I wouldn't say it's the greatest library in the world, but it's definitely worth having a look :)
you can use Crystal Report for this. But first you need to scan the INVOICE and save it as an image,
Next is, on your crystal report, export the image on to it, and DRAG the fields to where they must print on the invoice (IMAGE SERVES AS YOUR GUIDE). Then after everything has been set-up, DELETE THE IMAGE and try it.
hope this helps.

Categories