Click here to Skip to main content
15,920,468 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an ASP/VB page that outputs text (as a stream), for a report, that my users print (All of the text is inside of a PRE tag). I've modified the output to include css so that the page will break at the appropriate spots.

CSS
@media all
{
    .page-break{ display:none; }
}
@media print
{
    .page-break{display:block;page-break-before:always;}
}


I read in the stream, loop through each line and check for the text PAGE: I then prepend the following html code in front of the line that includes PAGE:

HTML
<div class="page-break"></div>
05/25/2012 12:55              PACKING LIST                 PAGE: 002


This works the way I want it to. The problem that I'm having is that I have two blank lines that are directly above the break point that I want to remove. I know that I can simply not write those blank lines, as I go through my for each, but here is where I'm having my problem. There are other areas in the output that has blank lines that I don't want to remove. So I can't simply remove every blank line that I come across. I only want to remove the blank lines that are directly above my page break.

Here is a portion of my code:
VB
dim obPageStream
set obPageStream = obPageData.Stream
dim bytes
dim bytetext
dim text
dim lines
dim line

do
    bytes = obPageStream.Read(8192)
    bytetext = BinaryToString(bytes)
    text = text + bytetext

loop while ubound(bytes) > 0 
    lines = Split(text, vbCrLf)
		
for each line in lines
    if InStr(line, "PAGE:") > 0 then
        if InStr(line, "PAGE: 001") > 0 then
            Response.Write(line + vbcrlf)
        else
            Response.Write("<div class=" + chr(34) + "page-break" + chr(34) + ">" + "</div>" + line + vbcrlf)
        end if
    else
        Response.Write(line + vbcrlf)
    end if
next

Response.End()


As you can see it's very simple. Any help that you all can offer would be greatly appreciated. Thank you very much!
Posted

Could you not buffer your output to a String object, then use
VB
Response.Write(strBuffered.Replace(vbCrLf & vbCrLf & "<div class="" page-break=""></div>", "<div class="" page-break=""></div>")

to remove the previous lines as you send it out?
 
Share this answer
 
VB
dim obPageStream
set obPageStream = obPageData.Stream
dim bytes
dim bytetext
dim text
dim lines
dim line
'We'll count how many empty lines we've seen so far
dim countEmptyLines = 0

do
    bytes = obPageStream.Read(8192)
    bytetext = BinaryToString(bytes)
    text = text + bytetext

loop while ubound(bytes) > 0
    lines = Split(text, vbCrLf)

for each line in lines
    if InStr(line, "PAGE:") > 0 then
        'In case we've detected a PAGE: tag let us just reset the counter and forget about all 
        ' previous empty lines
        countEmptyLines = 0;
        if InStr(line, "PAGE: 001") > 0 then
            Response.Write(line + vbcrlf)
        else
            Response.Write("<div class=" + chr(34) + "page-break" + chr(34) + ">" + "</div>" + line + vbcrlf)
        end if
    else
        if line = "" then
            countEmptyLines = countEmptyLines + 1 'Another empty line to remember
        else
            ' Here we are sure that the current line does not have PAGE: in it
            ' So we'll output all the dangling emtpy lines and reset our counter
            for idx = 1 to countEmptyLines
                Response.Write(vbcrlf)
            end for
            countEmptyLines = 0;
            ' and we also output the current line
            Response.Write(line + vbcrlf)
        end if
    end if
next

Response.End()



Hope that resolves your issue.

Regards,

Manfred
 
Share this answer
 
v2
Comments
jay1_z 19-Jun-12 16:06pm    
I think this solution would work better in case there is more than two lines that get returned in the future. Thank you guys very much for your help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900