Generate PDF document based on Word document - c#

I’d like in an ASP.NET MVC application generate document (invoice for example) based on a word document than I use as template. I create a word document with :
Some fix data :
Company info
Logo
general layout
Some data to add in a specific place :
Invoice number
A section with a table, with some column (price, quantity, description, vat, …)
In the bottom the total with and without VAT
A date
In this document, I can create a kind of tag, example <PDF:InvoiceNumber> and replace it by the value by code, same for all the other "TAG"
Do you have an idea how I can do this? Did you experiment another way (suggestions are welcome)? I accept to use 3rd control free or commercial. The only restriction is … I don’t want install Microsoft Word on the server.
Thanks,

Use the OSS .net port of iText, iTextSharp (google or bing for it).
General process:
1) Create a PDF file of your existing Word Doc
2) Using Acrobat (or similar tool) add PDF form fields to the file as placeholders for the data you will insert into the form at run-time
3) Using iTextSharp 's PDF form-filling API, insert the data into the form-fields in the PDF
4) Save, print, return-as-stream-to-client's-browser-with-PDF-mime-type, etc. the resulting PDF

I realise you don't want to install MS-Word on the server, but you could consider installing it on a separate machine and then consider the approach in this article. It uses Web Services and is clean and efficient.
I wrote this article so the usual disclaimers apply.

Related

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.
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.
Wkthmltopdf might be an option.
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.
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.

editable word document attachment

Its a general scenario when we provide an option of attaching a file (MS .doc) to end user. This file is stored in DB as binary. When user try to access this attachment next time, we allow them to download it. Now, here I want to give a feature to user where he should be able to open this doc file on click, edit it and save it without downloading.
.doc is a binary format and not easy to work with - a library such as Aspose, as mentioned by Christian, is definitely the way to go.
However, if .DOCX is acceptable (and that's Office 2007 and higher), then you can achieve what you want in three steps:
Convert .docx to HTML
Convert Word to HTML then render HTML on webpage
Display the HTML using any rich text control of your choice
What is the best rich textarea editor for jQuery?
Finally, convert HTML back to .docx:
Convert Html to Docx in c#
You would have to "reinvent" Microsoft Office Online (look into your skydrive account). I am unsure if there are any "out of the box" libraries for that, but you could build a simple editing app by leveraging Aspose word (or some other library). But that would be far from simple.
Link to aspose: http://www.aspose.com/.net/word-component.aspx
Word will only open files that are locally stored. What you are looking for is something similar to editing items that SharePoint provides using the WebDAV interface.
You may be able to use this approach to support your requirement. You should be cautious about the security aspects of the solution unless you have fully authenticated access to the shared folder on the server.
I am not sure if a standalone MS Word Document editor exists. However, this can be done with using a combination of rich text formatting / converting tool (for example, the DevExpress ASPxHtmlEditor + Document Server):
Load binary data from a DB;
Import loaded data (MS Word content) as HTML content into the ASPxHtmlEditor;
Edit imported data via the WYSIWYG ASPxHtmlEditor;
Convert the edited HTML back to MS Word content;
Save the converted / edited MS Word content back to the DB.
I believe, it is possible to do something like this if you have such products (free or commercial analogs) in your project.

formatting and printing data using C# (NO DATABASE)

I have following data in variables
1) Patient Name,Age,Sex,Referred by Doctor.
2) TestName,Parameters(As an array) and its results
There are some variables which will be computed using formulas.
I need to send data to printer in the following format
http://www.orthoclinical.com/en-us/Documents/Sample%20lab%20report.pdf
Please point me how to implement the same.
PS: 1) I cant display data directly from Database. There are many derived parameters which will be calculated using formulas.
2)I'm newbie to C# and printing application. Please help me with pseudocode/code
have a look at itextsharp. This is a server side pdf library that uses a document object model to allow you to add your text and styling in a pretty easy manner.
main features:
itext is an open source library that allows you to create and manipulate PDF documents.
It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.
iText is available in Java as well as in .Net and Android.
usages:
Serve PDF to a browser
Generate dynamic documents from XML files or databases
Use PDF's many interactive features
Add bookmarks, page numbers, watermarks, etc.
Split, concatenate, and manipulate PDF pages
Automate filling out of PDF forms
Add digital signatures to a PDF file
available via nuget: Install-Package iTextSharp

Convert PDF document to Word document by programmatically without any third party tool (SSRS 2005)

I am using SQL Server Reporting Service 2005(SSRS 2005) to export report to Excel and PDF and VS2008. But now i want an option to Export to Word also, but it is not possible in SSRS 2005 report that i came to know after googling. Here problem is that I CAN'T USE SSRS 2008 REPORT. So i thought that i will follow the steps as....
-- Export to Word
1. Export to PDF
2. Convert that PDF to Word document
Even after so much of googling i didn't got the proper answer. I told once and even telling that i can't use any third party tools so don't give me wrong path.
There are many fundamental differences between PDF and Word making the approach you want highly undesirable as a general workflow. I'll give just one example: PDF typically does not store information about document structure - sentences, paragraphs, columns, tables... All it stores is the actual text at certain locations at a page. Word of course does have those concepts.
Is it possible to do what you want? Yes, to some extent. In the general case with guesswork and approximation. If you know which information you want to convert it might be possible to search for it in the PDF file generated by SSRS and then generate a Word file out of it. However, if SSRS allows export to text, XML, RTF or any other structure based file format (however slightly structure based), you'd have a much easier time.
If you insist on doing what you suggest here, you would have to:
1) Write code to take the PDF exported from SSRS and interpret it (find the textual content you want)
2) Recreate the necessary structural information from that information (what are paragraphs, where and what are the tables, what's the formatting etc...)
3) Write that into a file Word can read (or create a new Word document directly using automation).
This would be a considerable amount of work, but you have all of the necessary information as the PDF specification is freely downloadable from the Adobe web site and it contains all of the information you need.

Writing to a PDF with fields from SQL Server 2005

I'm running an asp.net 4 / c# site using SQL Server 2005 and want to write column values from within SQL Server into a PDF file which already has content inside it (I want to personalise a PDF voucher that you get from the logged in page on my site)
Is there an easy tutorial to look at or is this a much bigger job?
There are two parts here:
Read data from database (for example, using ADO.NET, Entity Framework, e.t.c.)
Write that data to a PDF document (using some free or commercial PDF tool, like iTextSharp, SharpPDF, XPDF, Docotic.Pdf, e.t.c)
You can look at this article that describes both parts in detail.
Well, I think you have several options out there.
For example, you can create a PDF document based on a template using iTextSharp.
In that case, the template is the existing voucher you mention. You'd have to replace the information inside it, by identifying the fields, or something like that.
Something similar could also be accomplished using PDFSharp.
Just pay special attention when using iTextSharp to its license agreement, since its AGPL, which means your software must be GPL/AGPL as well.
Now, if you want to avoid interacting with PDF objects, and APIs, you have one other alternative. Which is to create an RDLC local report, and directly export it to PDF from your codebehind. In this case, it wouldn't be based on an existing voucher, it would be created from scratch.

Categories