-
Dec 21st, 2021, 07:32 PM
#1
Thread Starter
Banned
[RESOLVED] Breaking text into page size portions
I am using the following code stolen from this forum, but I'm only getting pages representing a portion of the original text. Can anyone help?
Code:
Private Sub SplitDocument()
Dim Document As String = RTB1.Text
Dim flags As TextFormatFlags = TextFormatFlags.Top Or TextFormatFlags.Left Or TextFormatFlags.WordBreak Or TextFormatFlags.NoPadding Or TextFormatFlags.TextBoxControl
Dim textSize As Size = TextRenderer.MeasureText(RTB1.Text, RTB1.Font, RTB1.ClientSize, flags)
Dim numberOfPages As Integer = CInt(textSize.Height / RTB1.ClientSize.Height)
If textSize.Height > RTB1.Height Then
RTB1.Text = Document
RTB1.Update()
Dim firstCharOfLastShownLine As Integer = RTB1.GetCharIndexFromPosition(New Point(0, RTB1.ClientSize.Height))
Dim visibleLines As Integer = RTB1.GetLineFromCharIndex(firstCharOfLastShownLine)
Dim totalLines As Integer = RTB1.GetLineFromCharIndex(RTB1.Text.Length - 1)
For p As Integer = 0 To numberOfPages - 1
Dim firstLineOfPage As Integer = (p * visibleLines)
Dim firstCharOfPage As Integer = RTB1.GetFirstCharIndexFromLine(firstLineOfPage)
Dim firstLineOfNextPage As Integer = (p + 1) * visibleLines
firstLineOfNextPage = If((firstLineOfNextPage > totalLines), totalLines, firstLineOfNextPage)
Dim lastCharOfPage As Integer = If((firstLineOfNextPage < totalLines), RTB1.GetFirstCharIndexFromLine(firstLineOfNextPage) - 1, RTB1.Text.Length)
Pages.Add(RTB1.Text.Substring(firstCharOfPage, lastCharOfPage - firstCharOfPage + 1))
Next
Else
Pages.Add(RTB1.Text)
End If
RTB1.Text = Pages(0)
End Sub
-
Dec 21st, 2021, 08:23 PM
#2
Re: Breaking text into page size portions
Have you actually debugged the code? If so, EXACTLY where and how does the behaviour of the code differ from your expectations? If not, do so and then answer the previous question.
-
Dec 21st, 2021, 08:37 PM
#3
Thread Starter
Banned
Re: Breaking text into page size portions
There are no errors during debugging.
It loads the document into separate "pages" as intended, but for only about half of the entire original document. A label displays the value of Pages.Count and it is less than it should be. Also, clicking the next button,
Code:
Private Sub NextBtn_Click(sender As Object, e As EventArgs) Handles NextBtn.Click
CurrentPage += 1
If CurrentPage <= Pages.Count - 1 Then
RTB1.Text = Pages(CurrentPage)
RTB1.Update()
End If
End Sub
, advances to the next page as expected, but if I click it until the current page = total pages, it is not near the end of the original document.
-
Dec 21st, 2021, 08:51 PM
#4
Re: Breaking text into page size portions
Originally Posted by Amerigoware
There are no errors during debugging.
I didn't ask you about errors, i.e. exceptions. Read what I actually wrote. I asked you exactly where and how the behaviour of the code differs from what you expect. You have provided a very general description of how but not a specific description of how or any description of where. I would suggest that you haven't actually debugged the code. Just running the project in the debugger is not debugging. You need to use the tools provided, which means at least setting a breakpoint and stepping through the code. You need to have a clear idea of what you expect to happen at each step and then you can see whether that actually does happen when you step. If the actual behaviour is different from what you expect then you have found an issue and you can describe that specifically, i.e. where it happened, what you expected and what actually happened. If you do that then you may find that you can fix the issue yourself but, if not, at least you can point us to the actual issue. ALWAYS debug thoroughly before posting questions online.
-
Dec 22nd, 2021, 03:29 PM
#5
Re: Breaking text into page size portions
How much of your document is missing? Half? More? Less? The last page? Ever other page? I have some guesses about what may be wrong but I'm not going to waste my time or yours going down that road if it's not the problem. You've walked into the Doctor's office and declared "It's hurts." The doc has asked "What hurts" and you've relied "A limb" ... narrows it but still no better than before. You haven't given us any indication about expectations vs reality. In my siganture is a link about Removing Eels from Your Hovercraft ... might be a good time to read it.
-tg
-
Dec 22nd, 2021, 03:43 PM
#6
Thread Starter
Banned
Re: Breaking text into page size portions
It turns out, I needed a '+ 1' at the end of 'NumberOfPages. I tried it on a whim and it worked, although I now get an extra blank page at times.
I'm sorry I don't know how to pose a question when I have no idea where the problem is coming from. Putting stops at every line in this situation gave no hint at all as I had no way of knowing in advance how many 'pages' I should have from a given text file.
-
Dec 22nd, 2021, 04:26 PM
#7
Re: [RESOLVED] Breaking text into page size portions
YEah, so that's what I thought... it's dropping that last little bit of your document. Your flaw is in the calculation of the numberOfPages ... the fact that you're doing it at all ... especially at the beginning. Don't. What you should be doing is looping WHILE you have text and incrementing numberOfPages as you go - or figure it out after you're done by using the .Size of Pages ... Then 1) you'll end up with the right number of pages, and 2) you also won't end up with blank pages.
-tg
-
Dec 22nd, 2021, 11:59 PM
#8
Thread Starter
Banned
Re: [RESOLVED] Breaking text into page size portions
Thanks. I'll look into that. Like I said, I snagged the code from elsewhere. For me, that makes it especially difficult to figure what's going wrong.
-
Dec 23rd, 2021, 09:00 PM
#9
Thread Starter
Banned
Re: [RESOLVED] Breaking text into page size portions
I thought this was resolved, but when trying different documents, I get different results. Sometimes it behaves as it should, other times, I get an extra page somehow showing the content of the first page. Very strange.
I'm trying to do as Techgnome suggested, but I can't seem to figure out how to know when the loop has reached the end of the document.
Code:
Dim P As Integer
Do While P < CInt(textSize.Height / RTB1.ClientSize.Height) 'Obviously this does the same as before. I have no idea what to put here.
Dim firstLineOfPage As Integer = (P * visibleLines)
Dim firstCharOfPage As Integer = RTB1.GetFirstCharIndexFromLine(firstLineOfPage)
If firstCharOfPage < 0 Then firstCharOfPage = 0
Dim firstLineOfNextPage As Integer = (P + 1) * visibleLines
firstLineOfNextPage = If((firstLineOfNextPage > totalLines), totalLines, firstLineOfNextPage)
Dim lastCharOfPage As Integer = If((firstLineOfNextPage < totalLines), RTB1.GetFirstCharIndexFromLine(firstLineOfNextPage) - 1, RTB1.Text.Length)
If RTB1.Text.Substring(firstCharOfPage, lastCharOfPage - firstCharOfPage).Trim <> "" Then
Pages.Add(RTB1.Text.Substring(firstCharOfPage, lastCharOfPage - firstCharOfPage))
End If
P += 1
Loop
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|