Good Evening; I am working first time on word documents in c++. I just generated a word file in which i just simply add two numbers. Now i just want to change the font size, style and font color of a particular word 'Sum' in whole document. Can any body help me to let me know just how can i do this.
code is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
float a,b,c;
ofstream Save("addition.doc");
cout<<"Enter First Number: ";
cin>>a;
cout<<"Enter Second Number: ";
cin>>b;
c=a+b;
cout<<"Sum of "<<a<<" and "<<b<<" is "<<c<<endl;
cout<<"File is being saved by the name of addition.doc .... "<<endl;
Save<< "Sum of "<<a<<" and "<<b<<" is "<<c<<" ... ";
Save.close();
return 0;
}
You have not created a microsoft word document. Putting .doc at the end of the filename will not automatically change the format that the file is saved in.
Your program creates a text file that could just as well be opened in a simple text editor like notepad. Pure text doesn't have colours, font size and such stylistic features.
If you want to create microsoft word documents you should learn how the microsoft word document format works or use a library to help you. I have no experience working with this format other than saving through a word processor so I can't help you.
yah i got it, but i do not know how to generate the .docx file. i m totally new here in this work. so is there no one who could do help me in this regard.
Your best bet might be some sort of intermediate format such as rtf (rich text format) which can be widely understood and imported or exported from many programs such as Word.
An example, the following text:
hello bolditalicunderline goodbye
looks like this when saved from Wordpad and then opened in a plain text editor:
I'm sure there are libraries available to simplify the creation of such text. Or- if you want to do things the hard way (but possibly learn more than you ever wanted to know about rtf) you could study the specifications and create your own routines to generate the required tags.
Another, related option would be to output the required text as an HTML document with suitable tags and stylesheet to specify the formatting.
yah i got it, but i do not know how to generate the .docx file. i m totally new here in this work. so is there no one who could do help me in this regard.
If you are keen enough to give docx format a go, see
This is coded in C#, so you'll need to translate the code into C++. The System.IO.Packaging mechanism in the code is the .Net way of creating a zip file.
As that's what a docx file basically is: an XML doc conforming to the Office Open XML File Formats schema, plus some other stuff, all zipped up. As it's a zip file, rather than a compound storage document like a Word .doc (no x) file, you just need to be able to deal with zip and XML. (You can even open .docx, xlsx, etc with 7Zip, WinZip or extract the files with a command line zip.)
The Office Open XML File Formats standards (ISO/IEC 29500 part 1, ...) are available for free here: