Efrain
Efrain

Reputation: 3364

Simple Template Based PDF Reports in .Net

My customer gave me some Word and Powerpoint documents which specify how certain 'reports' generated by our product are supposed to look like.

That means, I need to modify those documents (replace placeholders etc.) and then I need to export them as PDF.

How would you solve this problem in C# ?

TL;DR: Editing the office document is no problem at all, but exporting that document to PDF (using Interop) allegedly causes issues when running it as a web server application. That's the whole problem here.

Upvotes: 1

Views: 1952

Answers (4)

Jacquers
Jacquers

Reputation: 199

Wkthmltopdf might be an option.

Upvotes: 1

xaml
xaml

Reputation: 192

You can use report generators, like FastReport.Net for solving your problems. It can assign different data for placeholders and also allow export to PDF.

Upvotes: 0

MarkusEgle
MarkusEgle

Reputation: 3065

A completely different "report approach" could be, to save those office documents with the placeholders as mht (That's MHTML a web archive format). This could be done directly in MS Office or even programatically.

The placeholders could be easily exchanged by string search and replace. The mht files could directly be used to show the report instead of the PDF. A clear disadvantage of the mht format, is the HTML formatting. With PDF you have a clear and fix positioning.

We are using this kind of report creation. There are some flaws, but it works and the customer could edit the mht templates directly by right-click Open-With the prefered MS Office flavor.

Upvotes: 0

Karl Erlacher
Karl Erlacher

Reputation: 11

I agree that Interop is not suitable for document manipulation in server environment. I would approach this problem by preparing MS Word template documents with placeholders for data. Then I would use c# to load the data for the reports and merge the data with templates to get final documents (docx, pdf, xps or various image formats). There are 3rd party toolkits which make it quite easy. Here is the code used by one such toolkit needed for merging xml data with the template to get a pdf document:

XElement customers = XElement.Load("Customers.xml");
DocumentGenerator dg = new DocumentGenerator(customers);
DocumentGenerationResult result = dg.GenerateDocument("MyTemplate.docx", "MyReport.pdf");

You can of course also use free libraries and SDKs based on OpenXML but you should expect a steep learning curve, lots of debugging and lots of time invested.

Upvotes: 1

Related Questions