Convert a text file to word document


Could you point me to a sample I could use to convert a text file to a word document.

I need to change chr(12) to page breaks and make sure that format is large enough to show each report line not wrapped.



Here is a sample code for the first part of you question:

private OpenFileDialog opendlg = new OpenFileDialog();

private void buttonConvert_Click(object sender, System.EventArgs e) {

// initialize OpenFileDialog

opendlg.FileName = "";

opendlg.Title = "Import from text file";

opendlg.InitialDirectory = Application.StartupPath;

opendlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

opendlg.CheckFileExists = true;

string text = null;

if(opendlg.ShowDialog()==DialogResult.OK) {

// if file opened ok then read contents to string

StreamReader sr = File.OpenText(opendlg.FileName);

text = sr.ReadToEnd();



if(text!=null) {

// if text file loaded succesfully

// then create new Document

Document doc = new Document();

// and new DocumentBuilder

DocumentBuilder db = new DocumentBuilder(doc);

// split the text on pagebreak symbols (0x12)

string[] pages = text.Split('\x12');

for(int i = 0; i < pages.Length; i++) {

// write text

db.Write(pagesIdea [I]);

// write break

if(i!=pages.Length-1) db.InsertBreak(BreakType.PageBreak);


doc.Save(System.IO.Path.GetFileNameWithoutExtension(opendlg.FileName) + ".doc");

MessageBox.Show("Text file imported successfully.");



As for the second part - it's a little bit tricky. You can try to avoid wrapping of lines by setting a page format or by reducing the font of paragraph.

I suggest you check the following articles:

  • Building Documents Dynamically | Inserting a Paragraph | Inserting Document Elements in Aspose.Word Wiki;
  • PageSetup Class in Aspose.Word API Reference.


Thanks, that worked great.

How can I suppress word from setting its own page breaks and only recognize page breaks that I send via code.


MS Word does not insert pagebreaks at will. It starts a new page if there is not enough place on a previous one. But that does not lead to PageBreak insertion in document structure and does not change document data in any way. Thus, it is a matter of rendering, not editing.


Ok, now I am trying to do a print preview and print document and the document does not print correctly. I am using an evaluation version of aspose for word would this create a problem with printing the document.


Evaluation version of Aspose.Word has full functionality of licensed version, excepts it insert some garbage text randomly in the document. Here you can find all information concerning evaluation version and licensing.


Well my document does no show up in print preview as expected and does not print as expected. It is a report which was generated by aspose.word. It appears to be correct in word document but does not preview or print correctly. Could I zip and email to you for your review?


Yes, you can add it to your post as an attachment (the attachment will be downloadable only for Aspose team members). Or you can send it in a private e-mail, using e-mail button in left bottom corner of the message frame.


Here it is zipped


There are no visible attachments in this thread. Try to reattach the file.


I’ll try again, I noticed that when doing a preview of post and then coming back to edit post that the attachment path is gone.


I have checked the document and it previews and prints normally. It has evaluation header and garbage text inserted every now and then, exactly as should be with unlicensed version. Maybe I have overlooked something. Please make a more specific description of the problems you are experiencing.



Please see attached bmp for what I get when I do print preview in word.




That’s really strange, but on my computer the document previews and prints normally. Hold on, I will try to check it on different computers and printers. I will also try to find if anyone has reported the issue before.


The problem seems to be in conflicting Page Setup and printer settings. Try going to File|PageSetup and click ok. The Word will probably report that your margins are incorrect or something like this.