How to change the font size and style in word document generated in C++

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>
using namespace 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;
}


THanks


Regards
Abbas
Last edited on
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.

THanks

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 bold italic underline goodbye

looks like this when saved from Wordpad and then opened in a plain text editor:
1
2
3
4
{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 hello \b bold\b0  \i italic\i0  \ul underline\ulnone  goodbye\par
  \par
}


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.




Last edited on
Another markup language you could use is LaTeX.
You could even use HTML, I guess.

Andy

HTML <font> color Attribute
http://www.w3schools.com/tags/att_font_color.asp

<HTML>
<HEAD><TITLE>My Results</TITLE></BODY>
<BODY>
<H1>My Results</H1>
<H2>
<FONT COLOR="red">Sum</FONT> of <FONT COLOR="blue">45</FONT> and 
<FONT COLOR="blue">55</FONT> is ... 
<FONT COLOR="green"><B>100</B></FONT><BR><H2>
</BODY>
</HTML>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace std;

void writeHtml(const char* filename, float a, float b, float c);

int main()
{
    const char filename[] = "addition.html";
    float a,b,c;
    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 "<< filename<<".... "<<endl;
    writeHtml(filename, a, b, c);
    return 0;
}

void writeHtml(const char* filename, float a, float b, float c)
{
    const char header[] = "<HTML>\n"
                          "<HEAD><TITLE>My Results</TITLE></BODY>\n"
                          "<BODY>\n";
    const char footer[] = "</BODY>\n"
                          "</HTML>\n";

    ofstream Save(filename);
    Save<<header;
    Save<< "<H1>My Results</H1>\n"
        << "<H2>\n"
        << "<FONT COLOR=\"red\">Sum</FONT> of <FONT COLOR=\"blue\">"<<a<< "</FONT> and \n"
        << "<FONT COLOR=\"blue\">"<<b<<"</FONT> is ... \n"
        << "<FONT COLOR=\"green\"><B>"<<c<<"</B></FONT><BR>"
        << "<H2>\n";
    Save<<footer;
}
Last edited on
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

CreateDOCX Sample Program
http://blogs.msdn.com/b/dmahugh/archive/2006/06/27/649007.aspx

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:

Freely Available Standards
http://standards.iso.org/ittf/PubliclyAvailableStandards/index.html
Office Open XML File Formats

Andy

What is a DOCX File?
http://pcsupport.about.com/od/fileextensions/f/docxfile.htm

Introducing the Office (2007) Open XML File Formats
http://msdn.microsoft.com/en-us/library/office/aa338205%28v=office.12%29.aspx

How to: Manipulate Office Open XML Formats Documents
http://msdn.microsoft.com/en-us/library/office/aa982683%28v=office.12%29.aspx

Etc
Last edited on
Topic archived. No new replies allowed.