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: -
private: System::Void menuItem33_Click(System::Object * sender, System::EventArgs * e)
-
{//Print
-
printDialog1->AllowSomePages = true;
-
-
printDialog1->ShowHelp = true;
-
-
printDialog1->Document = printDocument1;
-
-
System::Windows::Forms::DialogResult result = printDialog1->ShowDialog();
-
-
if (result == DialogResult::OK)
-
{
-
printDocument1->Print();
-
}
-
}
-
-
private: System::Void printDocument1_PrintPage(System::Object * sender,System::Drawing::Printing::PrintPageEventArgs * e)
-
{
-
System::Drawing::Font* currentFont = richTextBox1->Font;
-
String* text = richTextBox1->Text;
-
-
System::Drawing::Font* printFont = new System::Drawing::Font(currentFont->FontFamily, currentFont->Size, currentFont->Style);
-
-
// Draw the content.
-
e->Graphics->DrawString( text, printFont, System::Drawing::Brushes::Black, 10, 10 );
-
-
}
-
-
private: System::Void menuItem34_Click(System::Object * sender, System::EventArgs * e)
-
{//PrintPreview
-
-
printPreviewDialog1->Document = printDocument1;
-
printPreviewDialog1->UseAntiAlias = true;
-
-
if (printPreviewDialog1->ShowDialog() == DialogResult::OK)
-
{
-
this->printDocument1->Print();
-
}
-
}
-
3 3142
Moving this to the .NET forum since it uses the .NET framework.
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. -
-
private: System::Void menuItem33_Click(System::Object * sender, System::EventArgs * e)
-
{//Print
-
-
// Allow the user to choose the page range he or she would like to print.
-
printDialog1->AllowSomePages = true;
-
-
// Show the help button.
-
printDialog1->ShowHelp = true;
-
-
printString = richTextBox1->Text;
-
myReader = new StringReader(printString);
-
-
result1 = printDialog1->ShowDialog();
-
-
if (result1 == System::Windows::Forms::DialogResult::OK)
-
{
-
printDocument1->Print();
-
}
-
-
myReader->Close();
-
}
-
-
private: System::Void printDocument1_PrintPage(System::Object * sender,System::Drawing::Printing::PrintPageEventArgs * e)
-
{//Print Document Page (Handles both print and print preview)
-
System::Drawing::Font* currentFont = richTextBox1->Font;
-
System::Drawing::Font* printFont = new System::Drawing::Font(currentFont->FontFamily, currentFont->Size, currentFont->Style);
-
-
float linesPerPage = 0;
-
float yPosition = 0;
-
int count = 0;
-
int leftMargin = e->MarginBounds.Left;
-
int topMargin = e->MarginBounds.Top;
-
int nextChar = 0;
-
String* line = "";
-
SolidBrush* myBrush = new SolidBrush(Color::Black);
-
-
//work out the number of lines per page using the MarginBounds.
-
linesPerPage = e->MarginBounds.Height / printFont->GetHeight(e->Graphics);
-
-
//iterate over the string using the StringReader, printing each line.
-
while((count < linesPerPage) && (nextChar >= 0))
-
{
-
//reading line by line
-
line = myReader->ReadLine();
-
-
//calculate the next line position based on the height of the font according to the printing device
-
yPosition = topMargin + (count * printFont->GetHeight(e->Graphics));
-
-
//draw on print preview, or print.
-
e->Graphics->DrawString(line, printFont, myBrush, leftMargin, yPosition);
-
count++;
-
}
-
-
//if there are more lines, print another page.
-
nextChar = myReader->Peek();
-
-
if (String::Empty && (nextChar < 0))
-
e->HasMorePages = false;
-
else
-
e->HasMorePages = true;
-
-
}
-
-
private: System::Void printPreviewDialog1_Load(System::Object * sender, System::EventArgs * e)
-
{
-
}
-
-
private: System::Void menuItem34_Click(System::Object * sender, System::EventArgs * e)
-
{//PrintPreview
-
-
printString = richTextBox1->Text;
-
myReader = new StringReader(printString);
-
printPreviewDialog1->UseAntiAlias = true;
-
printPreviewDialog1->Document = printDocument1;
-
result1 = printPreviewDialog1->ShowDialog();
-
-
if (result1 == System::Windows::Forms::DialogResult::OK)
-
{
-
printDocument1->Print();
-
}
-
-
myReader->Close();
-
}
-
-
private: System::Void pageSetupToolMenuItem_Click(System::Object * sender, System::EventArgs * e)
-
{
-
// Initialize the dialog's PrinterSettings property to hold user
-
// defined printer settings.
-
pageSetupDialog1->PageSettings = new System::Drawing::Printing::PageSettings;
-
-
// Initialize dialog's PrinterSettings property to hold user
-
// set printer settings.
-
pageSetupDialog1->PrinterSettings = new System::Drawing::Printing::PrinterSettings;
-
-
//Do not show the network in the printer dialog.
-
pageSetupDialog1->ShowNetwork = false;
-
-
pageSetupDialog1->Document = printDocument1;
-
// Sets the print document's color setting to false,
-
// so that the page will not be printed in color.
-
pageSetupDialog1->Document->DefaultPageSettings->Color = false;
-
-
printString = richTextBox1->Text;
-
myReader = new StringReader(printString);
-
pageSetupDialog1->Document = printDocument1;
-
result1 = pageSetupDialog1->ShowDialog();
-
-
// If the result is OK, display selected settings in
-
// ListBox1. These values can be used when printing the
-
// document.
-
if ( result1 == DialogResult::OK )
-
{
-
Object* results[] = {pageSetupDialog1->PageSettings->Margins, pageSetupDialog1->PageSettings->PaperSize, pageSetupDialog1->PrinterSettings->PrinterName};
-
listBox1->Items->AddRange( results );
-
-
}
-
-
}
-
-
I forgot to mention u need to add these: So you won't get any errors with the other part of the code. -
private: System::Windows::Forms::DialogResult result1;
-
private: System::Drawing::Font * printFont;
-
private: String * printString;
-
private: StringReader* myReader;
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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.
|
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()
|
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
| |
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...
|
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...
|
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...
|
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
|
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"
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |