views:

457

answers:

10

Hi everyone,

Just wondering if anyone could tell me of a simple way to create files for printing? At the moment I'm just scripting HTML, but I'm wondering if there isn't some easier way of doing it that would give me more control over what it being printed? Something along the lines of an Access printout, or Excel printout - where I could decide how to lay things out and almost "Mail merge" the details in via programming.

Basically, I want to create something for print that can have tables encasing it, and could be longer or shorter for each record depending upon the number of foreign keys (e.g. one staff member could have 10 jobs today, or just 3. I want to create a document that will generate and print).

Any ideas/advice/opinions? Thank you!

EDIT: Wow, thanks for all the responses! For this particular task, FlowDocuments seems to be the closest to what I'm actually after so I'll play with that. Either way I have several really good options now.

EDIT 2: After some playing, iTextSharp has become the choice for me. For anyone wondering in the future, here is a link to a great and simple tutorial: http://www.mikesdotnetting.com/Category/20

Thanks again!

+7  A: 

I would create a PDF file which can be viewed just about anywhere and will maintain formatting. Take a look here: http://itextsharp.sourceforge.net/

David Radcliffe
A: 

If you're looking for a very simple way to create docs (prob not the best, but it sure is easy), you can set up Word docs with bookmarks and insert data into the bookmarks through code, so I'm guessing this would work for Excel too (if they have bookmarks?):

EDIT: Here's a quick translation into c# (been tested in vb, but not c#):

Word.Application oWord = default(Word.Application);
Word.Document oDoc = default(Word.Document);

oWord = Interaction.CreateObject("Word.Application");
oWord.Visible = false;

oDoc = oWord.Documents.Add(Directory + "\\MyDocument.dot");

oDoc.Bookmarks("MyBookmark").Range.Text = strBookmark;

oDoc.PrintOut();
oDoc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
oDoc = null;
oWord.Application.Quit();
oWord = null;
smoore
Yeah, but sometimes you do not want Office installed... especially if your server is rendering docs on the fly.
Kris Krause
In his question he said "Along the lines of Access printout or Excel printout" so I'm guessing he has office. I know this is not the preferred way, but if you need to throw something together really quickly (like in 5 minutes) this makes it a lot easier.
smoore
Word documents in c# are much more difficult than in VB, for now. I would wait until c# 4 releases because it promises to provide much better support for this.
Chris Conway
Looks like if you're just inserting into a bookmark (which is what I'm doing) it's been exactly the same in both since VS 2005: http://msdn.microsoft.com/en-us/library/ad6ks7k0(VS.80).aspx
smoore
+1  A: 

We are currently using PDFSharp with great success -

http://www.pdfsharp.com/PDFsharp/

GDI+ or WPF ... all .NET, not COM or interop.

Oh, and its open source. Here is some sample code -

http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

Kris Krause
+3  A: 

There's always FlowDocuments. Check out the overview at MSDN http://msdn.microsoft.com/en-us/library/aa970909.aspx and see if they match what you want to do. They're pretty easy to print and can be serialized to xaml. Might not be exactly what you're after, but they're pretty useful.

wasatz
+1 for introducing me to FlowDocs!
smoore
Plus from me also! It great tool! I don't know that WPF have this one!
netmajor
A: 

XPS is MSFT's solution to print structured and formatted documents. I'm not saying to send someone an XPS, just to use the .NET support for printing via the XPS framework.

http://roecode.wordpress.com/2007/12/21/using-flowdocument-xaml-to-print-xps-documents/

It's very easy to do if you're doing WPF. Essentially an XPS is MSFT's PDF. Anyone running Vista or 7 can view/print them fine.

Bobby
That's a pretty bold statement to say that XPS is the best way or even the preferred way. I would disagree completely! PDF, in my opinion, is the preferred way because of its platform independence and more widespread use. While XPS will accomplish the goal for vista and 7 users, it's not the best way.
Chris Conway
Good luck on XP, Mac, or Linux though.
jvenema
I don't think I've ever received or downloaded an XPS document from anyone. Ever. It's usually: "Oh crap, I just printed to the Microsoft XPS Document Writer by mistake instead of CutePDF."
Pwninstein
I misspoke then, I wasn't saying to ship an XPS, just that you can use it's underlying functionality in .NET to ease the the printing of structured content.
Bobby
A: 

I have created printouts from web sites using Excel XML format. Basically this means you don't have to use the Office APIs to generate the document (which can be cumbersome and requires extra libraries on the web server). Instead, you can just take an XML template, use XPath, LINQ to XML, or other technologies to insert your data into the template, and then stream it to the user and they can print it.

Generating the template is easy. You just use Excel to create the document and then save it in the "XML Spreadsheet" format. The XML is a bit oppressive but it isn't terrible.

Documentation on the XML Spreadsheet format is here:
http://msdn.microsoft.com/en-us/library/aa140066%28office.10%29.aspx

Note that the documentation is for Excel 2002. The format does change in newer versions of Excel, but it is backwards compatible.

jkohlhepp
A: 

We use ActiveReports. It is very easy to define your layout and can print and export in a number of formats, pdf, rtf, excel, tiff, etc.

Chris Conway
A: 

If you use PDF or XPS generator, it still requires you to define the document composition very much like scripting your HTML, so I dont see that it gives you much more values other than the created file is in print ready format.

What you need is something that you can design a template and just filling in the blank, so I suggest that you either go for Word or Excel automation, otherwise look at some lightweight report generation library. I come across this and maybe it is worth checking out too. http://www.fyireporting.com/

Fadrian Sudaman
A: 

I've done the word and pdf generation things in the past and the support for pdf generation in pdfsharp (@Kris) is pretty good and I would use it ahead of office automation.

I hope I've not mis-read your needs but rather than exporting in a specific format and then firing the print feature I would these days re-consider plain old browser printing. In the past the awful limitations of browser printing meant that printing was a bad experience (no shrink-to-fit etc). But modern browsers have sufficient print support to be acceptable for simple jobs.

I've just checked printing this page in Firefox 3.5.8 and IE8 and both support shrink-to-fit and I reckon a simple print stylesheet will generate nice looking job sheets straight out of the browser as long as your (presumably internal) audience is guaranteed to have a modern browser.

amelvin
+1  A: 

Like David i also recommended I Text Sharp ;) It's relly easy to create pdf document with this ;) I use it in ASP.NET project. It have much of options to format pdf file, in my example i use basic ;) Example:

    string file = @"d:\print.pdf";  //path to pdf file
    Document myDocument = new Document(PageSize.A4.Rotate());        
    PdfWriter.GetInstance(myDocument, new FileStream(file, FileMode.Create));
    myDocument.Open();
    //data to save in pdf- unimportant!
    Opiekun obiekun = (from opiekunTmp in db.Opiekuns where opiekunTmp.idOpiekun == nalez.Dziecko.idOpiekun select opiekunTmp).SingleOrDefault();
    Dziecko dzieckoZap = (from dzieckoTmp in db.Dzieckos where dzieckoTmp.idDziecko == nalez.idDziecko select dzieckoTmp).SingleOrDefault();
    //some info about font
    BaseFont times = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
    Font font = new Font(times, 12);
    myDocument.Add(new Paragraph("--------------------------Raport opłaty--------------------------",font));
    myDocument.Add(new Paragraph("Data rozliczenia: " + (((TextBox)this.GridViewOplaty.Rows[e.RowIndex].Cells[8].Controls[0]).Text), font));
    myDocument.Add(new Paragraph("Płatnik: " + obiekun.Imie + " " + obiekun.Nazwisko, font));
    myDocument.Add(new Paragraph("Dziecko: " + dzieckoZap.Imie + " " + dzieckoZap.Nazwisko, font));
    myDocument.Add(new Paragraph(""));
    myDocument.Add(new Paragraph("Data             Podpis płatnika: " + obiekun.Imie + " " + obiekun.Nazwisko, font));
    myDocument.Add(new Paragraph(""));
    myDocument.Add(new Paragraph("  ...........       ................................."));
    myDocument.Close(); //we close the pdf and open
    System.Diagnostics.Process.Start(file); //and open our file if You want that ;)
netmajor