Question Printing multiple pages using DrawString many times

keb1965

Well-known member
Joined
Feb 9, 2009
Messages
103
Programming Experience
10+
I don't know if this is possible, but I presume that it is, I just have to work out the "how".

The dilemna is, that I have lots of data that I gather from multiple places and the data has to be printed in specific locations based on the size of the previous items printed. For example, I have one field that is retrieved from a multiline textbox control and may contain no lines or it may contain a hundred lines. Programmatically I can measure the string to see how much room it takes to print and adjust the fields after it up or down the requisite number so as to continue the printing without huge gaps in the words.

To get each of the 150 or so fields and format them properly on the many pages, I have to loop through several different controls. Since the data is being gathered from a loop, if I stop processing the loop when my page is full, the next page contains some of the same information, based upon which control was partially printed.

To combat this, I took an inefficient route that seemed to work, but alas, it too has flaws. I started by creating a "Y" property. Then as I incremented each bit of text to print in the Y Axis, I simply adjusted the "Y" property the requisite amount. In the property function, I check to see if the value is greater than 0 (top of the page) and less than the "Top Margin" if it is, I return the top margin to the next call to Y. Like so:

VB.NET:
   Private Property Y() As Integer
        Get
            If m_Y >= 0 And m_Y <= marginTop Then
                Return marginTop
            Else
                Return m_Y
            End If
        End Get
        Set(ByVal value As Integer)
            m_Y = value
        End Set
    End Property

For each of my text items this works wonderfully, except if I have a large body of text that spans past the bottom margin.

I am considering a while loop to handle the large bodies of text unless there is an easier way.

The easier way is what I am after ... after all, isn't that why we write code .. to make things easier?

I'd appreciate any insight.
 
Use the MeasureString method of Graphics, specify the bounding layoutArea size and utilize the charactersFitted parameter.
 
I suppose the problem is really much more complicated than simply measuring the string and putting it on a page. In fact, I use MeasureString when it is warranted and there are still issues I need to resolve.

Of the 150 fields that will be printed, most of them will be only a few words, and as such will fit nicely wherever I put them on the page, the problem I have is whenever the few fields that contain paragraphs is printed, I can't conceive of how to make the code differentiate between a small field and a paragraph field.

As it works now, if printing a single line field will cause the characters to extend beyond the bottom margin, e.HasMorePages is set to true, Y is incremented to the topmargin and the print function exits. When it is fired a second time, Y is decremented according to how many pages have already been printed and printing begins at the top margin.

However, try as I might, I cannot get all permutations to work properly. There is some logic fault and I cannot seem to resolve it.

I'll continue and hope serendipity steps in to help
 
Back
Top