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
jjjkkklllAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:
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
0
ZoppoCommented:
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
0
jjjkkklllAuthor Commented:
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
0
ZoppoCommented:
Hi jjjkkklll,

you can do that somhow like this:

// override CDocument::OnFileSaveAs() by adding a message handler fo ID_FILE_SAVE_AS
void CCPWDoc::OnFileSaveAs()
{
 CFileDialog dlg (
  FALSE,
  "",
  "unnamed",
  OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  "CPW File (*.cpw)|*.cpw|Text File (*.txt)|*.txt||"
 );

 if ( dlg.DoModal() == IDOK )
  DoSave( (LPCTSTR)dlg.GetPathName() ); // this is a undocumented function
}

// override CDocument's virtual OnSaveDocument() function
BOOL CCPWDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
 if ( NULL != lpszPathName )
 {
  // check if type is *.txt
  CString fileName( lpszPathName );
  CString fileExt = fileName.Right( 4 );
  fileExt.MakeUpper();
  if ( fileExt == ".TXT" )
  {
   // save as Text file
   SaveToTextFile( lpszPathName ); // this should be the function you have to write which saves text to the file with given filename
   return TRUE;
  }
 }
 return CDocument::OnSaveDocument(lpszPathName);
}

Some further info on DoSave(): this is an undocumented virtual function of CDocument, called by both CDocument::OnFileSave() and CDocument::OnFileSaveAs(). Passing a NULL as argument forces a file dialog to be opened to select a new filename with CDocument::OnFileSaveAs(). Passing a valid filename to it causes the document to call CDocument::OnSaveDocument().

hope that helps,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjjkkklllAuthor Commented:
Looks great.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.