How to create *.docx files from a template in C# - c#

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

Related

Is there any way to store Autotext (GlossaryDocument) in docx instead of dotx

I need make word file with some autotext (generated from database)
Now I programmatically generate word document (docx) and template for it (dotx). Dotx contains list of autotext (in GlossaryDocument) and in docx file I paste relation on it:
documentSettingPart1.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri($"file:./{Path.GetFileName(dotxTemplate)}", UriKind.Relative) , relationId);
So If user save both files in the same directory and open docx, he can use autotext perfectly. But I looking for a way to realize it in one docx file because it's inconvenient for users to have two files and make sure they are in the same directory.
I tried add GlossaryDocumentPart in docx or change document type (ChangeDocumentType(WordprocessingDocumentType.Document)) but after that I see GlossaryDocument in open xml sdk, but when I open docx-file in Word there are not any autotext from this GlossaryDocument
Is there any way to make docx file that contains autotext in yourself?
A docx file cannot contain AutoText (Building Blocks). It is simply not supported. But why not save the document you're distributing as a template and the user can use it to create a new document whenever it's requiredf? That's what templates are for...
What is possible is to store the Word Open XML that represents the content to be re-used in (a) Custom XML Part(s). You'd need to code some kind of interface to enable the user to retrieve and insert this content. If the code should travel with the document, then as VBA - and it would then need to be a docm rather than docx file.
Given Word 2013 or newer, it's also possible to map/link a content control to a node in a Custom XML Part. But, again, this would require you to develop some kind of interface for the user.
Also possible would be a VSTO or Word JS API solution rather than VBA.

How can I unzip Office Open XML file in C# and modify its data and re-zip again?

From a console application written in C#, how can I :
extract an Office Open XML file,
Obtain the data part of it modify
the data and re-zip it again
My motivation is to save an excel file with the formats and use it to populate cells via a console application.
Is this possible to achieve, do I need a specific library to that provides Excel files manipulation (unzipping it, modifying it etc.)
For the zip-unzip part i think you can find easily many examples here.
To edit the excel file, I'd suggest you to have a look at Open XML SDK. With it, you can easily edit office files programmatically.
Hope it helps

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.

Change blank fields in .doc file. WPF

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

How Can I Embed a Word Document in Silverlight?

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.

Categories