How to change font in word document - c#

I'm wondering how can I change the font (+ color and some font style) for the whole word document (excluding the page headers, if it's possible) by using C# ?
If You know any libraries (also commercial) feel free to let me know.
(After changing the font, I'll convert that word document to PDF using ABCPdf)

Depending on the library you would loop through the content (Pages/Paragraphs etc.) and change whatever font attribute you want...
Since you don't say which type of application you build - in case it is a pure desktop application and Word is guranteed to be installed locally you can use Office Interop. BEWARE that Office Interop is NOT supported by MS in server-scenarios (like IIS, ASP.NET. Windows Service, WCF etc.).
IF Word is not guaranteed to be installed and/or your application is not a desktop application and your Word documents are always DOCX files you could use OpenXML SDK V 2 from MS (free).
Otherwise I can recommend Aspose.Words (commercial) - works with old DOC and current DOCX and is usable in any type of application (desktop, ASP.NET, Windows Service etc.).

Handling Office documents in .NET is a quite straight forward task. Have a look at Microsoft.Office.Interop.Word, it is pretty well documented. Here is another link on to how you can change a word document font style using .NET (the example is in VB but is easily convertible to C#)

Start by reading on Microsoft.Office.Interop.Word
(You can google some user-friendly examples, there are tons)
Later on, read about Text Formatting
Easy, isn't it?

Related

Automatically substitute a font with an embedded one

I'm writing a word add-in with C# and in this add-in I'd like to substitute a font with another that I have embedded from the resources.
I know there is a "font substitution" tool in Word which can automatically substitute a non-installed font for another which is installed on the computer. Since this is the exact opposite of what I want to do, I hope there is a way!
Worst case you can manipulate the docx from your AddIn using the OpenXML SDK then reload.
Beware though that if you choose to get your OpenXML package in memory rather than by saving the docx and manipulating that, there is an issue you may run into on large documents. I'll update this answer when I have the reference handy.

Implement MS Word functionality in C#

I am trying to reference MS Word in my C# program using the Microsoft.Office.Interop namespace. In Word, we can insert images in a document and use the "Remove Background" feature to specify a portion of the image and remove other parts. Is there a way to do this with the Interop namespace?? Or am I supposed to resort to manual methods??
Word allows to record a VBA macro in the background. Thus, you will find what properties and methods can be used to get the job done. Note, the Word object model is common for all kind of applications. See Create or run a macro for more information.
Open XML SDK is another alternative way to get the job done. But in some cases it is easier to automate Word than deal with XML. See Welcome to the Open XML SDK 2.5 for Office for more information.
Where do you run the code? A web server? ASP.NET?

is there any way to write one code that works with all possible office documents?

I'm writing a program that modifies word documents. Currently I have used Microsoft.Office,Interop.Word to work with Word document and it requiers Microsoft Office to be installed on users computer, but some my clients don't have MS Office, but they have Open Office.
So, which library should I use instead of Interop?
and also how can I make my code to be able to work with different word files, not only .doc and .docx, but also with other office program files?
currently I'm writing different code for every type of the document..
My program translates the documents from its original language to another, so it is very important for me to keep the formatting of the document in original format, that's why I used Interop.. but also I want my program to be useful for as many people as possible
I think you are not mentioning but, are you assuming all your clients use the same version of Office. To solve the issue of the office versions, you may want to look at this open source project: NetOffice http://netoffice.codeplex.com/ and do all your .doc and .docx file formats development in using that library.
For the OpenOffice or LibreOffice, I believe the best you can do is going into the projects website and download the SDK. For example, go here: http://api.libreoffice.org/examples/examples.html and you will find some examples in Java, Python, C++ to edit Text Document including odt files.
LibreOffice SDK download here: http://www.libreoffice.org/download/
And finally, there is also the OpenXML format (mentioned on another answer) which is:
ECMA Office Open XML ("Open XML") is an international, open standard for word-processing documents, presentations, and spreadsheets that can be freely implemented by multiple applications on multiple platforms.
And you can download also its SDK here: http://msdn.microsoft.com/en-us/office/bb265236.aspx
Hope that helps.
You will likely end up writing separate code to work with each file type. There may be some similarities within, say, Office products, but for the most part you're going to need an adapter for each type.
However, you could (and should) minimize the amount of duplicate code by placing the translation logic and other non-type-specific functions in a shared library that each adapter would then reference.
We are using aspose words. This supports DOC, DOCX, RTF and OOXML.
But it's not free.

Read/Write/Save MS Word Document in c#

I have to open a word document using c# and do some changes in that document and save it again. Document will have lot of tables and styling. For example I have to process that document page by page. I have to change all italics to normal and all caps to small letters and save to that document only the changes without affecting the styling and alignment or format of the document.
Is that possible in c# .net? Please let me know it there any tutorial available based on my requirement. Basically I am a Java developer recently moved to c#. I have googled for past 2 days, I didn't get any proper data.
Personally, I use Aspose.NET. But that component is not free. If you need something free, I can also recommend the Microsoft Open XML Library:
http://blogs.msdn.com/b/ericwhite/archive/2008/04/22/using-the-open-xml-sdk.aspx
I would not use Office Interop as Jim suggested. It's not very stable for server.

Do I need to have MS Word installed for creating Word documents from asp.net?

See title...
No.
You can use WordML (Word XML)
Word 2007 version
You can create Word 2007 documents using its XML format without the need of installing Word in your server.
This can be a starting point.
I've already +1'd Mitch's reply, but as an aside: Word isn't even supported for use in service applications; it is designed to be user-interactive. So installing Word, even if it worked, wouldn't leave you in a great place.
If you're just generating the documents from scratch the solutions so far proposed work well. My situation was that I had an existing template that I needed to use and substitute in my own text in a few places (mail merge, if you will). This was several years ago - prior to Office 2007 - but we ended up going with the Aspose library of components for this. I've used the Words and Cells (Excel) components to generate documents from templates and spreadsheets on the fly to download from web sites. The interfaces are a little clunky and can be inconsistent between the various products. The installer, frankly, is awful, but the products work pretty well and made it much easier to do what needed to be done.
Word recognizes rtf as intrinsic, and if your intended document can be constructed as whatever.rtf - which for all of its fancy formatting is plain ASCII markup - then you shd be able to write the document without Word installed.
To get the picture, create an example document and save it as an rtf file. Then view that file with an ascii text editor (like Notepad). You'll have to learn rtf syntax, but there's at least one handbook around on that.
AS
Just to add another potential solution for you, OfficeWriter is a Word/Excel API that lets you create documents and spreadsheets in ASP.NET without using Office:
http://www.officewriter.com

Categories