I have a .doc document. In this document i have some blanks for data. For example:
"car_id" is the best car in "car_country".
I need to open this doc file and change this blanks ("car_id", "car_country") to data from some object.
How to do this?
I would use DocumentFormat.OpenXml.dll. You can find it here. OpenXMLSDKv2.msi will add assembly. You will just need to add reference to DocumentFormat.OpenXml. And the OpenXMLSDKTool.msi will install a usefull tool that will display xml structure of .docx (for example) document.
This web page has some very good samples of Word automation using C#. Specifically, look at section 6 in the web page for a Mail Merge example.
http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx
Related
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.
I have a working ASP.NET MVC web application to manage projects and customers. Now I want to generate a word file for some customers. In this file should be displayed some data about the customer. Every generated file should have the same data and the same design. So I want to craete a new Word Template with the fields and want to fill the placeholders programmatically.
My problem is that I couldn't find a clear way to do that. Does anybody know a good learning resources?
Try this page:
Building Office Open XML Files
Open XML files (docx) are ZIP packages containing XML files. In your case, I would create a copy of your original template, then use the System.IO.Packaging API to open the file and modify it. By opening, the correct XML file and replacing certain placeholders in XML, you should be able to achieve the result you want.
While trying to do the same I have found some libraries to create/edit DOC or DOCX in .Net
GemBox.Document
TemplateEngine.Docx
DocXTemplateEngine
Templater - Nuget
Spire.Doc - Nuget
for the new document generation from a template file (.dot) should be very easy, I think it's a parameter you specify in the word application file open or so, when you pass the path of the .dot file, telling word to create a new .doc based on that file and not edit the actual template document.
for form fields and bookmarks filling, lots of examples online on how to do it from C#, see here:
MS Word Office Automation - Filling Text Form Fields And Check Box Form Fields And Mail Merge
I need to Embed the Word Document in silverlight,and i need to have all the same functionality of Word Document.
Like Cut,Copy,Paste,Save,Save us,Formating Etc.
How can i Achieve this?.
Also Suggest me some links too.
SL4 comes with COM automation support mean if client machine has Word installed SL can use it to display work doc:
http://forums.silverlight.net/forums/p/185680/424357.aspx
http://www.silverlightshow.net/items/MS-Word-Mail-Merge-with-Silverlight-4-COM-Automation.aspx
If you are using SL3.. it will be a little daunting... may be you will have to find some RTE to display word in it.
Regards.
The problem with using the COM model to read the file is that you must run the Silverlight app out of the browser with Elevated privileges and the user mush have Word installed so not very useful if you want a web app.
However Word documents are storred as XML files inside a zipped file (rename file from name.docx to name.zip to see the files) so you could always write a class to read in the XML and display it inside a Rich Text Box and then after formatting write it out to a XML form, this will take a lot of effort.
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 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