Can I add some hidden text with paragraphs in MSWord - c#

I am developing a MS Word add-in using c#. The code processes some very large input file and converts it to a word document. I am able to generate data and store it in heading 1, 2, ... normal etc styles.
Now I also want to store some hidden text (containing some self-generated information) with each of these paragraphs. This hidden text should by no means be visible to someone who opens this file in MSWord. But this hidden text must be retrievable to convert this word file back to the original file. Is it possible to insert some hidden fields with each paragraph (and I want to have multiple such fields - say about 4 to 5).
I am using OpenXml to create the word file.
I am very anxiously looking for some solution. Or if it is not possible to do so, then I must look into some altogether different solution.

Related

How to get a part of docx file as image, not whole page

I have been trying to insert a docx file contents into crystal reports. (In Crystal Reports if I insert OLE object and select a docx file, program imports only the used part of the word file, not the whole page.)
I have searched to convert Word document to image, but all I found was about getting a whole page or a specific image in a page. In my case I can have text and also line objects and maybe some graphic files in maybe 6 - 7 paragraphs. And only used part of the page(6 - 7 paragraphs) should be my image file.
Our customers are using this Word files as a header of the report. For now the only way I can do is to have screen shoot of the design. And it doesn't always look good. Is there a way to get this part as image?
Note: I can't use directly doc file in crystal reports, I need to have the data in SQL database. So it has to be graphic file.
You may find the Selection.CopyAsPicture method which works the same way as the Copy method. Basically you need to select the required content in a Word Document and then use this method to take a picture of selected content and keep it in a memory for further usage. For example, the following example copies the contents of the active document as a picture and pastes it as a picture at the end of the document:
Sub CopyPasteAsPicture()
ActiveDocument.Content.Select
With Selection
.CopyAsPicture
.Collapse Direction:=wdCollapseEnd
.PasteSpecial DataType:=wdPasteMetafilePicture
End With
End Sub
In C# you could use the same properties and methods, the Word object model is common for all kind of applications.

Edit XPS content

I have got an application that is supposed to send a formatted document to a printer with some barcodes.
I've made other applications that work with printers and print directly through the printserver by sending a xps file, so I thought I would try to see if I could make a .xps file, change the text and be done with it, however every article I can find on the net has to do with creating xps files and not changing them. I feel like it should be possible, and it would be nice not to have to resort to installing Office on the server and print through there. Then I might as well use Open XML and a .docx file.
It is very simple. Let's say I want to change the text INCNUMMER in a .xps file to "testing123". How would I go about that?
I have tried the whole unzip, open the xml, find the text, edit, rezip but I'm afraid there's too much about the .xps format I don't understand to make that work.
Best regards, Kaspar.
As you already know, an XPS file is just a ZIP archive containing a number of files and folders that have particular names and a defined structure.
At the root level there is a Documents folder which will typically contain just a single document folder named 1. Inside that is a Pages folder containing one or more .fpage files: these define the content of each page in the document.
Documents
1
Pages
1.fpage
2.fpage
etc
If you open up these .fpage files in a text editor you will see that they are just XML files. Each page is typically represented by a <Canvas> element that contains multiple <Path> and <Glyphs> elements (text is represented by the latter). However, even though <Glyphs> elements do have a UnicodeString attribute the value of that attribute cannot be changed in isolation.
Each <Glyphs> element also has an Indices attribute. If you remove this attribute altogether and change the UnicodeString attribute at the same time, this almost works. However, you will probably find that when viewing the file in the XPS Viewer application certain characters in the text are replaced by question mark symbols.
Font glyphs are embedded in the XPS file (odttf files in the Resources folder), and the software that generated the XPS file will only embed glyphs that are used in the source document. For example, this means that (for a given font) if you did not use the letter "A" in the source document, then the glyph for that letter will not be written to the resources of the XPS file. Hence if you change the UnicodeString attribute to include a letter "A" then that character will display as a question mark in the viewer because it has no glyph resource that tells it how that character must be drawn.
If you have control over the source document (the one that later gets converted to XPS) then I suppose you could include a piece of text containing all of the characters that you are likely to use, and set its colour to white so that it doesn't print, but I'm not sure whether the XPS printer driver would strip that text out anyway. If it didn't then you could probably do something like this:
Open the relevant .fpage XML file
Search all UnicodeString attributes of <Glyphs> elements to find the text you want
Replace that text with something else
Remove the Indices attribute from the changed <Glyphs> elements
Save the updated XML back to the file
Re-zip then change the extension from ZIP to XPS

Create a Word document from a template file c#

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.

How can i generate a word document docx with existing text as in a template, but fill in value thru a asp.net web form?

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

How to put text with headings on clipboard?

If I copy text (using the cursor etc. I don't mean programmatically) from a web page or Word document, and paste it in a Word document - Word knows what text is a heading, and what is simple text. I want to do the same thing (programmatically) - put text on the clipboard and specify that part of it is heading1, part heading2... and part is simple text.
I found this class to put html text (which can have headings) on the clipboard, but was wondering:
a) That's from January 2007. Perhaps there's a simpler way now.
b) HTML only allows up to 6 heading levels. (I actually tried h7 but Word didn't recognize it.) Perhaps there's some way to have unlimited heading levels like Word does.
I don't think the clipboard Handling had updates in the latest versions of .net framework.
I think that more complex updates/adding content to a word document may be achieved using ole automation or the open xml sdk.

Categories