Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Print a txt file

Status
Not open for further replies.

since1984

Technical User
Mar 25, 2010
7
GR
Need you help
i want to create a button so when clicked it will print on paper content of data.txt thats saved in the project directory
 
So you want to read a text file and print it, correct?


James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Yes and if only print without opening and reading it.
 
without opening and reading it.
Hmmm. I don't know how to do that. I can tell you how to open and read it. Which version on Builder are you using?



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
thanks for interest and reply
c++ Builder 6
i know how to open and read
i just want to create a print file button when i click it automaticly prin the file
i created a form with questions and edit boxes for answers i found a way to send them to richedit and print them from there before sending to file but i also want to print by button
 
OK. I don't know which type of BCB (Borland C++ Builder) 6 this comes with (Personal, Pro, or Enterprise) but see if your help file mentions TPrintDialog or TPrinterSetupDialog.

James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Yes there is an PrintDlg function
but structure and handle must be created and im not good at this stuff can you help
 
Forgot to mention its the Enterprise im using
also got the code gear rad studio c++ builder 2007
 
The following code is from Borland C++Builder How-To: The Definitive C++Builder Problem-Solver by John Miano, Tom Cabanski, and Harold Howe. The book is out of print but still has some good info. It looks like the form that this code goes to has some menu options for font and printer set up.
Code:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  //Make sure the font dialog starts with the same font as the memo box
  FontDialog1->Font = mEdit->Font;
  //Make sure the printer starts with the same font as the memo box
  Printer()->Canvas->Font = mEdit->Font;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Font1Click(TObject *Sender)
{
  //If the user pressed OK on the font dialog
  if (FontDialog1->Execute())
  {
    //Change the font of the memo box
    mEdit->Font = FontDialog1->Font;
    //Change the font of the printer
    Printer()->Canvas->Font = FontDialog1->Font;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PrinterSetup1Click(TObject *Sender)
{
  //Bring up the print setup dialog box
  PrinterSetupDialog1->Execute();
  //Change the font of the printer just in case user changed
  Printer()->Canvas->Font = FontDialog1->Font;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Print1Click(TObject *Sender)
{
  int Iterations, Copies, LineOnPage, LinesPerPage;
  int StartLine, EndLine;
  int x, y, z, PageNumber;
  bool NewPageRequired;

  //If the user did not press OK on the printer dialog, return
  if (!PrintDialog1->Execute())
    return;

  //Change the font of the printer just in case user changed
  Printer()->Canvas->Font = FontDialog1->Font;

  //Printer will start at the top of a new page
  //So we don't have to issue a new page before printing the
  //first page
  NewPageRequired = FALSE;
  //Calculate number of lines that can fit on a page
  LinesPerPage = (Printer()->PageHeight /
    Printer()->Canvas->TextHeight("W"));

  //If the user did not ask for collation
  if (!PrintDialog1->Collate)
  {
    //Loop through all the pages once
    Iterations = 1;
    //Print as many copies of each page as the user requested
    Copies = PrintDialog1->Copies;
  }
  //User wants to collate
  else
  {
    //Loop through all the pages enough times
    //to make all the copies the user requested
    Iterations = PrintDialog1->Copies;
    //Print each page once per iteration
    Copies = 1;
  }

  //Set the title used in the print manager to identify this job
  Printer()->Title = "C++ Builder How-to Printer Example 1";
  //Start the printing job
  Printer()->BeginDoc();

  //Start with the first line
  LineOnPage = 0;

  //Outer loop runs only once unless user requested collation
  for (x = 0; x < Iterations; x++)
  {
    //Always start at the first page
    PageNumber = 1;
    //Loop runs until we run out of pages to print
    do
    {
      //Loop to print multiple copies of a page (if required)
      for (y = 0; y < Copies; y++)
      {
        //If a new page is required
        if (NewPageRequired)
        {
          //Print a new page
          Printer()->NewPage();
          //Reset the line count
          LineOnPage = 0;
        }
        //Must always print a new page
        //for pages after the first
        NewPageRequired = TRUE;

        //Calculate the memo line that prints at the top of this page
        StartLine = ((PageNumber - 1) * LinesPerPage);
        //Calculate the memo line that prints at the bottom of this page
        EndLine = LinesPerPage + ((PageNumber - 1) * LinesPerPage) - 1;

        //Asjust the ending line count for the last page (which may not be full)
        if (EndLine >= mEdit->Lines->Count)
          EndLine = mEdit->Lines->Count;

        //For each memo line that will appear on this page
        for (z = StartLine; z < EndLine; z++)
        {
          //Print out a line
          Printer()->Canvas->TextOut(20,
            Printer()->Canvas->TextHeight(mEdit->Lines->Strings[z]) * LineOnPage,
              mEdit->Lines->Strings[z].c_str());
          //Increment the line count
          LineOnPage++;
        }
      }
      //Required copies of page are printed
      //Increment the page number
      PageNumber++;
    //Keep going until we're out of lines to print
    } while (EndLine < mEdit->Lines->Count );
  }

  //Close the document
  Printer()->EndDoc();
}
//---------------------------------------------------------------------------



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top