473,396 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Printing a unknown text string to a printer (˝ to 3 pages long.... )

Hi,

I'm reading Beginning Visual C-Sharp by wrox, great book by the way.

In the book the describe how I can print, and it works great, but what
if I has like 300 lines, that wont fit on a single page, so how do I
spilt it up....

The draw a rectangle and writes into that, but its seem to me that the
allready knows how much spade it will take on the page, and I dont.. it
could be a half page, 3 pages... So how can I calculate how much space
horizontal my text will need???

Need more info, just ask....

best regards
Mikael Syska
Nov 17 '05 #1
3 2013
Generally, when printing text you need to know the layout of the text on a
line for line basis. You should be responsible for positioning the text
according to any leading (this refers to line spacing and is pronounced
"ledding") criteria you may have. If you know the leading you can output a
limited number of lines before ending the page and getting the printdocument
to start a new page. This also implies that you have laid out individual
lines yourself by measuring the word widths or even the character widths
individually.

Most examples of text printing are very simplistic and only cover the
blasting out of single font text to an area smaller than a single page.
Unfortunately this tends to hide the real complexity of performing this
exacting task.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Mikael Syska" <ne****@syska.dk> wrote in message
news:el**************@TK2MSFTNGP15.phx.gbl...
Hi,

I'm reading Beginning Visual C-Sharp by wrox, great book by the way.

In the book the describe how I can print, and it works great, but what if
I has like 300 lines, that wont fit on a single page, so how do I spilt it
up....

The draw a rectangle and writes into that, but its seem to me that the
allready knows how much spade it will take on the page, and I dont.. it
could be a half page, 3 pages... So how can I calculate how much space
horizontal my text will need???

Need more info, just ask....

best regards
Mikael Syska

Nov 17 '05 #2
Hey Bob and thanks for the fast reply,

This aint good, but I will just have to print one page, for each text
string, and be sure that it can fit on a single page or something like
that, I just wanted to know if there were any smart way of doing this.

If you have any links convering this subject I will be very happy, maybe
then I can think of some smart way of doing this....

best regards
Mikael Syska
Bob Powell [MVP] wrote:
Generally, when printing text you need to know the layout of the text on a
line for line basis. You should be responsible for positioning the text
according to any leading (this refers to line spacing and is pronounced
"ledding") criteria you may have. If you know the leading you can output a
limited number of lines before ending the page and getting the printdocument
to start a new page. This also implies that you have laid out individual
lines yourself by measuring the word widths or even the character widths
individually.

Most examples of text printing are very simplistic and only cover the
blasting out of single font text to an area smaller than a single page.
Unfortunately this tends to hide the real complexity of performing this
exacting task.

Nov 17 '05 #3
Well you need to set e.HasMorePages to true until all pages are printed:

private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
if (e.PageSettings.PrinterSettings.PrintRange
== PrintRange.SomePages)
{
this.firstPage= e.PageSettings.PrinterSettings.FromPage;
this.lastPage= e.PageSettings.PrinterSettings.ToPage;
// simulate non printed pages
while (printEngine.CurrentPageNumber < this.firstPage)
{
e.HasMorePages= printEngine.PrintPage(this.document,
printEngine,
e.Graphics,
e.MarginBounds,
true);
}
// print a page
e.HasMorePages= printEngine.PrintPage(this.document,
printEngine,
e.Graphics,
e.MarginBounds,
false);
// exit print cycle
if (printEngine.CurrentPageNumber > this.lastPage)
{
e.HasMorePages= false;
printEngine.Reset();
}
}
else
{
e.HasMorePages= printEngine.PrintPage(document,
printEngine,
e.Graphics,
e.MarginBounds,
false);
}
}
You can get the array of lines from a textbox and then try to fit the
lines to a page:

// break out lines that overflow
foreach (string line in arrayUnfittedLines)
{
if (g.MeasureString(line,font,Int32.MaxValue,stringFo rmat).Width
<= pageWidth)
{
arrayLines.Add(line);
}
else
// line does not fit on page
// need to break up line into word tokens
// and then place tokens into lines that fit in page margin
{
string[] arrayTokens= line.Split(space); // tokenize line
int index=0;
while (index < arrayTokens.Length)
{
StringBuilder buffer= new StringBuilder();
// at least one token on line, ? may be clipped
buffer.Append(arrayTokens[index]+space);
index++;
while(index< arrayTokens.Length &&
g.MeasureString(buffer.ToString()+arrayTokens[index],
font,
Int32.MaxValue,
stringFormat).Width <= pageWidth)
{
buffer.Append(arrayTokens[index]+space);
index++;
}
// end of tokens or end of line
arrayLines.Add(buffer.ToString());
}
}

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris | last post by:
Hi, I found this code to send print direct to printer. It works perfect. Imports System Imports System.Text Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> _...
5
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of...
16
by: cyranoVR | last post by:
This is the approach I used to automate printing of Microsoft Access reports to PDF format i.e. unattended and without annoying "Save As..." dialogs, and - more importantly - without having to use...
5
by: Tom | last post by:
I am converting an old application that was printing directly to a specialized printer device (i.e. a special label printer). It was doing this by opening a file with the file path of 'LPT1:' and...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
6
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
0
by: John Smith | last post by:
Hello, I have 7 different crystal reports that need to be collated. Since I want to end up with a page of each (which all together make a single report), I created a blank main report and then...
1
by: billelev | last post by:
Here is some code that I have adapted slightly. It allows a report to be printed to a specific location. It works by calling SaveReportAsPDF and specifying the access report name, and the root...
1
by: Glenn | last post by:
I am writing a program for field work that will use a receipt printer. I need to be able to adjust the page settings prior to printing depending on how much needs to be printed. I have been able...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.