Creating, Opening and Printing a Word File from C++

Printing to a file in C

You can use the fprintf() function, which is quite similar to printf() in the way it works.

Here is an example:

FILE *fp;
int myInt = 5;
fp = fopen("Output.txt", "w");// "w" means that we are going to write on this file
fprintf(fp, "This is being written in the file. This is an int variable: %d", myInt);
fclose(fp); //Don't forget to close the file when finished

The output on your file would be this:

This is being written in the file. This is an int variable: 5

Worth to mention that opening the file using w as parameter will destroy the file's content every time you open it.

Print word document using c#

Quick tip (not relevant to your topic but actually C#): there's no need to write out optional parameters as you did above, you can use ParameterName: parameter to specify a parameter to a optional parameter.

Quick answer: use Document.PrintOut() method to print the current document. For more details about the parameters, you can take a look at MSDN site and this site for a hand-on tutorial.

Here is a simple demo:

public class YourClass : Form
{
private Word.Application word = new Word.Application {Visible = false};
private Word.Document doc;
// where did you get this file name?
private string fileName;

private void Count()
{
// as you mentioned, you open your word document here
doc = word.Documents.Open(fileName, ReadOnly : readOnly, Visible: isVisible);
}

// in your button click handler, just call PrintOut() function
private void ButtonClickHandler(object sender, EventArgs e)
{
// if doc == null, open the document
if (doc == null)
{
// here i assume fileName has been assigned
doc = word.Documents.Open(fileName, ReadOnly : readOnly, Visible: isVisible);
}

doc.PrintOut();
}
}

C#: print out Word document without saving it in advance document.PrintOut()

If the problem is that the document is closing before the print job has completed, then the best approach is to turn off background printing, at least for the duration of code execution.

winword.Options.PrintBackground = false;

Background printing was introduced in order to allow the user to continue working while a print job was processing. This is fine for the user, but a problem for code such as that in the question.

How can an OpenOffice document be created in C++

Here's a newly released C library called libOPC which has the same intent as the Open XML SDK, but can be used in all of Linux/Windows/Mac/etc. You can read about it here: libOPC version 0.0.1 released and get the code from CodePlex (be sure to check the documentation page for demo videos).

How can I print a file from the command line?

Since it is a .docx-file Microsoft Word is probably the best program to do the task.

I would have a look at the [command line arguments] to Word:

Have a look at the following switches:

/q, /n, /mFilePrintDefault and /mFileExit

(/q and /n explained in the page above, and /mXxxx refers to macros. Have a look att google.)

Example:

WINWORD.EXE your_document.docx /mFilePrintDefault /mFileExit /q /n

The following page seems to explain how to convert it to PDF.

Reading .docx in C++

The easiest way is to use Word to do this. It has limitations on licensing.

The SO question Creating, opening and printing a word file from C++ has some good references.

Edit:

According to these questions/answers can unzip the Open XML file and process the XML file directly:

  • How can I read a Word 2007 .docx file?

If you use .NET there are more (C#) questions to read:

  • How to grab text from word (docx) document in C#?
  • How to load MS Word document in C# (.NET)?
  • How can I programatically use C# to append multiple DOCX files together?


Related Topics



Leave a reply



Submit