473,804 Members | 3,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing Multiple Pages Problem C++

50 New Member
After hours of trying I got it to print and print preview the richtextbox. I've been trying to figure out how to print multiple pages. If the richtextbox contains more than 1 page of data, than it just prints the 1st page and everything else just gets cutoff. If anyone can help me with C++ code, I would really appreciate it. I've looked at a lot of forums before posting this and most of the examples are in C# or VB. I'm thinking the problem lies in the PrintPageEvent although Im not 100% sure. In the meantime, I'm going to try to figure it out.

Using Visual Studio .NET language C++

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. private: System::Void menuItem33_Click(System::Object *  sender, System::EventArgs *  e)
  2. {//Print
  3.     printDialog1->AllowSomePages = true;
  4.  
  5.                 printDialog1->ShowHelp = true;
  6.  
  7.     printDialog1->Document = printDocument1;
  8.  
  9. System::Windows::Forms::DialogResult result = printDialog1->ShowDialog();
  10.  
  11. if (result == DialogResult::OK)
  12. {
  13.                 printDocument1->Print();
  14. }
  15. }
  16.  
  17. private: System::Void printDocument1_PrintPage(System::Object *  sender,System::Drawing::Printing::PrintPageEventArgs *  e)
  18. {    
  19. System::Drawing::Font* currentFont = richTextBox1->Font;
  20. String* text = richTextBox1->Text;
  21.  
  22. System::Drawing::Font* printFont = new System::Drawing::Font(currentFont->FontFamily, currentFont->Size, currentFont->Style);
  23.  
  24. // Draw the content.
  25. e->Graphics->DrawString( text, printFont, System::Drawing::Brushes::Black, 10, 10 );
  26.  
  27. }
  28.  
  29. private: System::Void menuItem34_Click(System::Object *  sender, System::EventArgs *  e)
  30. {//PrintPreview
  31.  
  32. printPreviewDialog1->Document = printDocument1; 
  33. printPreviewDialog1->UseAntiAlias = true;    
  34.  
  35. if (printPreviewDialog1->ShowDialog() == DialogResult::OK)
  36. {
  37.     this->printDocument1->Print();
  38. }
  39. }
  40.  
Jul 13 '07 #1
3 3142
weaknessforcats
9,208 Recognized Expert Moderator Expert
Moving this to the .NET forum since it uses the .NET framework.
Jul 13 '07 #2
bmerlover
50 New Member
I FEEL GREAT :). I found the solution. Search the internet deep enough and you will find the solution. Although I had to make changes and debug for another couple of days to make sure it was doing what I wanted it to do. The biggest problem is the Print Page Handler which took a very very long time to fix. This isn't perfect with all the options of printing like you would expect from office but it does print multiple pages, it also recognizes different fonts and different sizes with the exception it does not word wrap on a print.

Expand|Select|Wrap|Line Numbers
  1.  
  2. private: System::Void menuItem33_Click(System::Object *  sender, System::EventArgs *  e)
  3.          {//Print
  4.  
  5.             // Allow the user to choose the page range he or she would like to print.
  6.             printDialog1->AllowSomePages = true;
  7.  
  8.             // Show the help button.
  9.             printDialog1->ShowHelp = true;
  10.  
  11.             printString = richTextBox1->Text;
  12.             myReader = new StringReader(printString);
  13.  
  14.             result1 = printDialog1->ShowDialog();
  15.  
  16.             if (result1 == System::Windows::Forms::DialogResult::OK)
  17.             {
  18.                 printDocument1->Print();
  19.             }
  20.  
  21.             myReader->Close();
  22.          }
  23.  
  24. private: System::Void printDocument1_PrintPage(System::Object *  sender,System::Drawing::Printing::PrintPageEventArgs *  e)
  25.          {//Print Document Page (Handles both print and print preview)
  26.             System::Drawing::Font* currentFont = richTextBox1->Font;
  27.             System::Drawing::Font* printFont = new System::Drawing::Font(currentFont->FontFamily, currentFont->Size, currentFont->Style);
  28.  
  29.             float linesPerPage = 0;
  30.             float yPosition = 0;
  31.             int count = 0;
  32.             int leftMargin = e->MarginBounds.Left;
  33.             int topMargin = e->MarginBounds.Top; 
  34.             int nextChar = 0;
  35.             String* line = "";
  36.             SolidBrush* myBrush = new SolidBrush(Color::Black);
  37.  
  38.             //work out the number of lines per page using the MarginBounds. 
  39.             linesPerPage = e->MarginBounds.Height / printFont->GetHeight(e->Graphics);
  40.  
  41.             //iterate over the string using the StringReader, printing each line. 
  42.             while((count < linesPerPage) && (nextChar >= 0))
  43.             {
  44.                 //reading line by line
  45.                 line = myReader->ReadLine();
  46.  
  47.                 //calculate the next line position based on the height of the font according to the printing device 
  48.                 yPosition = topMargin + (count * printFont->GetHeight(e->Graphics));
  49.  
  50.                 //draw on print preview, or print. 
  51.                 e->Graphics->DrawString(line, printFont, myBrush, leftMargin, yPosition);
  52.                 count++;
  53.             }
  54.  
  55.             //if there are more lines, print another page. 
  56.             nextChar = myReader->Peek();
  57.  
  58.             if (String::Empty && (nextChar < 0))
  59.                 e->HasMorePages = false;
  60.             else
  61.                 e->HasMorePages = true;
  62.  
  63.          }
  64.  
  65. private: System::Void printPreviewDialog1_Load(System::Object *  sender, System::EventArgs *  e)
  66.          {
  67.          }
  68.  
  69. private: System::Void menuItem34_Click(System::Object *  sender, System::EventArgs *  e)
  70.          {//PrintPreview
  71.  
  72.             printString = richTextBox1->Text;
  73.             myReader = new StringReader(printString);
  74.             printPreviewDialog1->UseAntiAlias = true;
  75.             printPreviewDialog1->Document = printDocument1;
  76.             result1 = printPreviewDialog1->ShowDialog();
  77.  
  78.             if (result1 == System::Windows::Forms::DialogResult::OK)
  79.             {
  80.                 printDocument1->Print();
  81.             }
  82.  
  83.             myReader->Close();
  84.          }
  85.  
  86. private: System::Void pageSetupToolMenuItem_Click(System::Object *  sender, System::EventArgs *  e)
  87.          {
  88.                 // Initialize the dialog's PrinterSettings property to hold user
  89.                 // defined printer settings.
  90.                 pageSetupDialog1->PageSettings = new System::Drawing::Printing::PageSettings;
  91.  
  92.                 // Initialize dialog's PrinterSettings property to hold user
  93.                 // set printer settings.
  94.                 pageSetupDialog1->PrinterSettings = new System::Drawing::Printing::PrinterSettings;
  95.  
  96.                 //Do not show the network in the printer dialog.
  97.                 pageSetupDialog1->ShowNetwork = false;
  98.  
  99.                 pageSetupDialog1->Document = printDocument1;
  100.                 // Sets the print document's color setting to false,
  101.                 // so that the page will not be printed in color.
  102.                 pageSetupDialog1->Document->DefaultPageSettings->Color = false;
  103.  
  104.                 printString = richTextBox1->Text;
  105.                 myReader = new StringReader(printString);
  106.                 pageSetupDialog1->Document = printDocument1;
  107.                 result1 = pageSetupDialog1->ShowDialog();
  108.  
  109.                 // If the result is OK, display selected settings in
  110.                 // ListBox1. These values can be used when printing the
  111.                 // document.
  112.                 if ( result1 == DialogResult::OK )
  113.                 {    
  114.                     Object* results[] = {pageSetupDialog1->PageSettings->Margins, pageSetupDialog1->PageSettings->PaperSize, pageSetupDialog1->PrinterSettings->PrinterName};
  115.                     listBox1->Items->AddRange( results );
  116.  
  117.                 }
  118.  
  119.          }
  120.  
  121.  
Jul 19 '07 #3
bmerlover
50 New Member
I forgot to mention u need to add these: So you won't get any errors with the other part of the code.

Expand|Select|Wrap|Line Numbers
  1. private: System::Windows::Forms::DialogResult result1;
  2. private: System::Drawing::Font * printFont;
  3. private: String * printString;
  4. private: StringReader* myReader;
  5.  
  6.  
Jul 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
7184
by: John Sutor | last post by:
Does anyone know how to print multiple page documents? I can print from a test box or a one page document, but not one with multiple pages.
10
23325
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a PrintPage event of a PrintDocument object) to send some formatted text to the printer. However, if the lines are too long they run off the page rather than wrapping around. I'm sure I can spend the time and come up with a word wrapping algorithm but...
4
3733
by: Jay | last post by:
Still can't seem to find a solution to printing a lengthy datagrid on multiple pages including datagrid headeron each page. I am not using Crystal Reports or Reporting Services or VStudio. Any idea how this can be done? Any links, examples Thanks a lot.
2
8198
by: ray well | last post by:
hi, i need to print multiple pages on a printer. this is what i'm using now Sub Print() Dim PrintDoc As New PrintDocument AddHandler PrintDoc.PrintPage, AddressOf Me.PrintText Do While bPrintingNotDone PrintDoc.Print()
6
482
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having trouble getting it to print on multiple pages. The second page will not print at all. Thanks in advance
6
4092
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if the user wants all pages will produce 3 pages. I want to offer the user the ability to select via the print dialog that only pages 1 and 2 of it are printed or possibly pages 1 and 3 but not 2. At the moment I can produce all three pages...
0
1345
by: ben.agnoli | last post by:
I have written the below class for printing out large jpg files (1 page wide, multiple pages high) over multiple pages. The class will get instantiated multiple times at once by a windows service. For each page that the class prints, it gets visibly slower, when the third document, third page and onwards starts to print, it takes over 20 seconds a page, am I doing something wrong, or is this the wrong way to go about the task (could be...
18
11321
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing classes, for each of the checked rows in the GridView. This works fine in the Visual Studio 2005 development environment on localhost. But, when I move the page to the web server, I get the error "Settings to access printer...
0
2854
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the printing printing data and the actual database data in a recordset. the class looks like this: Public Class BRPDD 'Balance Report PrintingDocument Data Public totalReceiptsNum As Integer Public totalLicFee As Double
1
2903
by: charitha0 | last post by:
Hi, Can any one help in printing a html table content (dynamically generated which spreads accorss multiple pages ) in landscape by default. The following code does not work when we have a table with lot of rows which spans accross multiple pages. Nothing shows up in print preview <html> <head> <object id="factory" viewastext style="display:none" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9130
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.