Link to home
Start Free TrialLog in
Avatar of jjjkkklll
jjjkkklll

asked on

Printing to a text file.

I have an MDI doc/view application.  My main view is a formview and is basically five edit boxes on top of each other. To print I had to build a virtual page that puts the contents of the five boxes all on one page in paragraph form. In other words, the only place you see all the information on one page is as it comes out of the printer.

I'd like to be able to "print to file" to a text file, so the file (when opened) looks (basically) like what prints out.  Right now, it will print-to-file as a .prn file (no other file types are offered), which won't work. I'm assuming that printing to a text file is easier than "saving as" to a text file since the print function is already building the virtual page.

Is there an easy way to set this up?  I'm on the verge of abandoning this feature.  Thanks for any help.  

Jim Weiss
Avatar of Vinayak Kumbar
Vinayak Kumbar

Hi,

U want to write the five edit boxes contents into the text file. right?. Then what is the problem?

U can use the CFile class for it. Do something like this.
When U want to print to a file

CFile file;
if(!file.Open("d:\\MyPrint.txt", CFile::modeCreate | CFile::modeReadWrite)
{
  AfxMessageBox("Unable to open the printer file");
  return ;
}

CString l_strEditContent;

//Get the first edit control content
GetDlgItem(IDC_EDIT1)->GetWindowText(l_strEditContent);
(l_strEditContent += '\n';

//Write to file
file.Write((l_strEditContent, l_strEditContent.GetLength());//U may have to convert this to char *

//Get the second edit control content
GetDlgItem(IDC_EDIT1)->GetWindowText(l_strEditContent);
(l_strEditContent += '\n';

//Write to file
file.Write((l_strEditContent, l_strEditContent.GetLength());//U may have to convert this to char *

.....
.....

Thats it.

Try it out.
VinExpert
Avatar of Zoppo
Hi jjjkkklll,

>I'm assuming that printing to a text file is easier than "saving as" to a text file
>since the print function is already building the virtual page.
'Printing' to a text file cannot be done in the same manner as printing to a device context, because a textfile has no coordinates or something which lets you position the texts in any way

The only way is to implement a function which saves the text to a text file in a manner which looks as similar as possible to the output of the printer.

How to write to a file VinExpert showed already ...

To override the default file selection (the .prn file dialog) for printing to file do something like this:

// override view's OnPreparePrinting()
BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
BOOL bRet = DoPreparePrinting(pInfo);

 if ( bRet )
 {
  if ( pInfo->m_pPD->m_pd.Flags )
  {
   // print to file selected, open file dialog to select text file
   CFileDialog dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "Textfile(*.txt)|*.txt|Any File(*.*)|*.*||" );
   if ( dlg.DoModal() == IDOK )
    PrintToTextFile( dlg.GetPathName() ); // this should be the function you have to write which saves text to the file with given filename
   return FALSE; // return FALSE here to avoid calling of OnPrint
  }
 }
 return bRet;
}

hope this helps,

ZOPPO
Avatar of jjjkkklll

ASKER

Okay, it's become painfully obvious I can't use the same structure I used to create the virtual page for my printer device to create a text file. I will have to build it separately and make it look like the printed page.  Let's all forget the "print to file" concept for now.

Zoppo, it seems your code will add a "Text file (*.txt)" option to the "Save As" list in CFileDialog when the user chooses "Print to file." Let's say we do away with the "Print to file" idea and want to offer the option to save it as a text file in the "Save As" dialog (chosen directly from the File menu). My feeling is the code for this is similar to what you gave me, but may need to be moved and modified.  Where would I put code to build and save (using CFile per VinExpert) the contents of the edit boxes as a text file?  In other words, how can I give the user the choice in the "Save As" dialog of creating *.cpw or *.txt, whereby the first option saves it in the usual way and the text option builds another virtual page and saves it as a text file?  And can the user name the new text file in the "Save As" dialog?  By the way, I wouldn't want the box to pop up if they were simply saving a default *.cpw file.  Thanks for your help.

Jim Weiss
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Looks great.  Thanks.