I am trying to make a word document in an asp.Net MVC application using OpenXML template document .
The main challenges for me are
How can i create a word document as an OpenXML template? In my word
document i have some paragraphs of texts and in every paragraphs i
have to fill information from data base like in word file there are
instances of text like
etc and these should be filled with actual data. But
i dont know how can i convert a normal word document as a OpenXML
template file .
How can i fill the values with data from db ? If i have a model say WordModel in hand with filled values of properties FirstName TotalAmount AmountUnit TotalCopies etc then how can i fill the details to template and allow user to download the file ?
What you want to do is called Mail Merge and means populating word documents that act as templates with your application data. There are some existing (commercial) solutions out there, but if you want to do it yourself using Open XML SDK, you need to set up some kind of tagging mechanism that you will use to tag certain parts of document where you want the data/text from the database should be placed. For Word documents you have the following options: Bookmarks, Content Controls, Merge Fields, or special text (<% FirstName %>). I would personally go with Content Controls as they offer the best user experience and they are pretty easy to parse and replace. So the templates would be ordinary word documents containing these tags and then you could use Open XML SDK to parse these templates, search the tags in them and replace them with your application data according to the tag's name/code/title. This a very abstract, high level picture of a mail merge processor. Of course system like this is not easy to implement and also note that using Open XML requires some learning. There was a similar question answered, but you can probably find many more - just google.
Related
I want to generate a word document (.docx) file, which is based on a template could be a word template or that the text can be stored in code.
Now the document generated should be based on the Input provided by the user using an ASP.net Web Form with Text Boxes to input the values.
These values will be taken in and placed in placeholder positions in the word template.
How can i use a word template in an ASP.net web project?
How can i take the values from the ASP.net webform and pass it to the word template, fill in the placeholder with the text and then generate the word document?
I have seen some examples where the text to be generated is created as HTML and then the file exported is given filetype msword to save as a word file. But in this case i wonder, how can i give page numbers and other header or footer values to the generated document.
So if i can use a word template, to just fill in the values the user wants and then generate the final document, then that's great.
Is there anything new to do this in Visual Studio 2012? I am trying to do this using C#.
You have several options for creating word documents -
Use word automation - this is using Ms Word directly. Not recommended if you're running on a server - it's slow and suffers from extensibility and other issues.
Use OpenXML - this is Microsoft's SDK to work with Office 2007+ file formats. There are several ways to accomplish your goal with OpenXML - like using bookmarks or content controls. Look at this SO question - How to replace content in template docx document and Open XML SDK 2.0 (Aug 09)? or this one - How to create *.docx files from a template in C# or google it (while not that difficult, the entire solution is more that a few lines of code and does require a bit of understanding)
Use third party tools or controls - I'm not really familiar with them.
You can create your template by creating a Word document ( including logos, header, footer, etc), putting the placeholders and save it as "Word 2003 xml" document.
Next, you can read the template.xml as a text file, replace the placeholders with the values, and response the entire string as a content type "application/msword"
The placeholder could be {MY_PLACEHOLDER1}
Before creating the template, you must deactivate ( in Word ) all related with Ortography, because the "bad ortography" highlightings are stored as a part of the xml document, and {MY_PLACEHOLDER1} can be stored as MY_PLACEHOLDER1 (the "{" and "}" characters are stored separated with their own word xml tags )
Regards,
You can use TemplateEngine.Docx for replacing the text in template document
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.
I have several Word templates and I wish to use these to dynamically create Word documents in my app. I wish to avoid using automation at all costs as this is no good. I know that I can use both HTML and XML to create word documents but I just don't know where to start with regards to using a template that may well have images in the footer or the header of a document.
I use the OpenXML SDK with Word 2007. After you get the hang of it, it's not so bad. I have several template docx files that I scan through to search and replace for placeholder strings with what I want, and then can stitch together multiple templates into one document if I want to. It's nice because I can start with docx files as the template and modify them while the whole time staying within the realm of the docx format. If an image is in the docx when you start modifying it, it'll be there after you re-save it after modification (provided you didn't programmatically remove it of course).
If you have more details with what you'll be doing, let us know.
You could use DocX. It's free, very easy to use, with nice tutorials and is feature reach. It works with only DOCX documents thou. Also development is currently on hold until the author will finish his semester. Here's detailed blog about it.
It has good example of using template in his Invoice Example.
MigraDoc http://www.pdfsharp.net/MigraDocOverview.ashx is a free utility for exporting PDF/Word/HTML files. I've not worked with it using templates as yet however, you could use the DDL files to persists a layout for your files to be re-used.
i want to develop such an application through which i can read book ,currently i am using the Richtextbox in flow document,i dont want to use the scroll ,i prefer the navigation style i.e prev page next page start and end ,book may contains images tables so and so ,and how do i import books in my application
How can i achieve?
Regards,
Aamir
You want to look at the FlowDocument, which is meant for documents with pages.
You'll need to write code to extract the text from the word or pdf document.
You can get out the text from a Word document using Word automation.
For pdf files you can possibly use the iTextSharp library:
http://itextsharp.sourceforge.net/
For other formats you might be able to use the source of FBReader as a sample:
http://www.fbreader.org/downloads.php
I have a "template" docx document which contains the desired layout, and wish to insert content using C#, but I cannot find a way to uniquely address specific sections of the document, such as paragraphs or tables.
What is the best way to uniquely identify elements in the document?
Thanks,
Matt Sharpe.
How is your template built? Does it use an underlying XML Schema loaded as part of the *.docx? Or are you using content controls off of the Developer ribbon, in which case each control is uniquely identified by a given tag name? Both of these approaches would make identifying certain sections of your document easier as you could control where tables or paragraphs would be.
Also, you may want to consider using the Open XML SDK 2.0 (uses .NET 3.5). It includes a handy Document Reflector tool that allows you to open up and inspect any Open XML document and shows how to generate the code for any element you click on.
Apart from that, to learn more about content controls you can check these posts:
Generating Microsoft Office Documents with the Open XML SDK
Creating Data-Bound Content Controls using the Open XML SDK and LINQ to XML
Can you use document variables/fields? Just go to Insert->Quick Parts->Fields->Doc Variable, enter name of variable.
example:
http://www.codeproject.com/KB/office/Fill_Mergefields.aspx
You could also just use placeholder text values like "##insert_first_name##" and then do a search and replace for those variables.
example:
Link
I think you are looking for either bookmarks, or content controls (on the Ribbon's Developer tab, code example here)
I've used Named Ranges in Excel for the same purpose.
There are several options to do this.
I have created a simple open-source library that will replace tags by values.
For example Hi {name} with data={name:"John"} will be replaced by Hi John.
Here it is :
https://github.com/edi9999/docxtemplater
Demo: http://javascript-ninja.fr/docxgenjs/examples/demo.html