开发者

Creating something printable in C#

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 wonde开发者_如何学编程ring in the future, here is a link to a great and simple tutorial: http://www.mikesdotnetting.com/Category/20

Thanks again!


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/


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.


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


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/


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 ;)


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.


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.


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;


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜