I have an byte array of an doc file loaded in memory.
I would like to set custom meta data properties of my Word document then save it back to disk. This will be done on a server so I can’t use VTSO to write an add in.
I need to do this in a 2003 word document. so I can't use open xml.
Have you seen Open XML SDK 2.0? Or you can use System.IO.Packaging API directly.
Related
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.
Trying to learn how to use OOXML to replace tokens win a word document by following this tutorial
Everything has been going pretty well until I hit this
Next, create the custom XML file containing the data for this document, and store it in the package.
Store the custom XML in memory and add placeholders for the actual data.
I'm having trouble discerning where to store the custom XML and what he means by storing it in memory.
The author also continues to refer to the file as package, so does that mean I should keep the word Template in .zip format?
Next, create the custom XML file containing the data for this document, and store it in the package.
In context, I would suggest the package is the file containing the xml data, which when you load it or display it becomes a document.
Storing it in memory, means loading an object with the data(i.e. xmldocument)
I am not sure if this is possible and every where I have searched, I cannot find a clear answer. I am saving a Microsoft Word document to a SQL Server 2008 table. Basically just converting the file to a Byte[] and writing that to the table. This word document is a "template" file. The file is a form that the user needs to fill out. What I am wondering, is after reading that file from SQL Server and before opening it up for the user, is there a way to autopopulate some fields in the form for the user? For example, if I know the address of the user already, can I autopopulate the address field in the template for them?
I know that using Microsoft.Office.Interop.Word, I can search the document for bookmarks and insert data at the bookmark. However, as far as I know, you cannot use Microsoft.Office.Interop.Word to open a Byte[].
Is there anyway to complete what I was looking for?
If you want to use OpenXML, then you can do it like this,
//Load your byte[] array into memory stream and then
WordprocessingDocument doc = WordprocessingDocument.Open(stream, true);
You can do what you are trying to achieve using OpenXML without installing word on the server side..More resources on OpenXMl can be found on http://openxmldeveloper.org/. And the open xml sdk can be downloaded from here.
I think the general steps would be to
1) Save the file to the local hard drive of the user with a file name based on the template but with a .doc extension.
2)Open the file with interop, but keep it invisible.
3)Populate the fields with bookmarks.
4)Show it to the user.
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
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