Results 1 to 15 of 15
  1. #1
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default Can you print to a file using page break?

    Hey Guys,
    Im very rusty to java programming as i touched the bases of it 8 years ago so please excuse my lack of knowledge in this area. I am helping my mother print something to a text file using a while loop. However everytime it prints in the loop, the next part always go down 1 line. Is there a way to add a page break? Sorry a little hard to describe but ill explain below.

    Heres my code:
    class test
    {
    public static void main(String args[])
    {
    File date = new File("c:\dates.txt");
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try
    {
    fis = new FileInputStream(date);
    bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);
    FileWriter fstream = new FileWriter("Output.txt");
    BufferedWriter out = new BufferedWriter(fstream);

    while (dis.available() != 0)
    {
    out.write("Today's date is "+dis.readLine());
    out.newLine();
    out.write("Testing 4 5 6 ");
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    out.newLine();
    }

    out.close();
    fis.close();
    bis.close();
    dis.close();
    }
    catch (Exception e)
    {
    System.err.println("Error: " + e.getMessage());
    }
    }
    So basically the program reads in dates.txt, and gets read and outputted. Dates.txt bascially has a list of dates and the while loop goes through the whole txt file and keeps printing until theres no dates left. Reason why i have so many newLine() is cause i want to create spaces so that i get enough space to print it twice on 1 paper and then i want the next page to print the same way. The problem is that on the 2nd page, it gets printed 1 line less and then 3rd page, 2 lines less...and so on. I tried deleting/adding another newLine() but its never gets 15 pages printed correctly (exactly every 2 on a page). Is there a command I can use to set a page break?

    I am using notepad to print this.

    Thanks in advanced.

  2. #2
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Try writing the formfeed character '\f'

    Form feed is an ASCII control character. It forces the printer to eject the current page and to continue printing at the top of another.

    Java Code:
    out.write('\f');
    or

    Java Code:
    out.write("one page\fanother page");

  3. #3
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the quick reply...that didnt work...seem to have messed up my print format actually.

  4. #4
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Lightbulb

    You can write a document in HTML-code and then import it with MSWord.
    There is a special trick to tell Word to start a new page:
    XML Code:
    <html>
    <head>
    <style type="text/css">
    //.....
    br.newpage {
    	mso-special-character:line-break;
    	page-break-before:always;
    }
    //.....
    </style>
    </head>
    <body>
    <p>One page</p>
    <br class="newpage" />
    <p>Another page</p>
    </body>
    </html>
    In fact I write my Word docs in HTML all the time!

  5. #5
    Spike_CT is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by kahoots View Post
    Thanks for the quick reply...that didnt work...seem to have messed up my print format actually.
    Notepad doesn't support formfeeds. Try opening the txt in Word or OpenOffice.

  6. #6
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks Arnold for your suggestion..but im very very bad with java so html scaries me.

    Spike...that worked!!! it took it in msword. Now the down side is that it prints each date on 1 page (because of the while loop)...is there an easy way i can print 2 dates on 1 page?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Is there a way to add a page break
    What is a "page break"? Files don't have pages. Printing on paper would.
    is there an easy way i can print 2 dates on 1 page
    What is a "page" when you are writing to a file?

    Are you trying to create a text file that can be printed by some other program with lines in the text file being at desired places on each page?
    What are your choices of programs that print? Many have print formatting capabilities.

  8. #8
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks guys...i got it to work, i just added another print date on the bottom before the loop ended and it was able to print both dates.

    Thanks again!

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kahoots View Post
    Thanks Arnold for your suggestion..but im very very bad with java so html scaries me.

    Spike...that worked!!! it took it in msword. Now the down side is that it prints each date on 1 page (because of the while loop)...is there an easy way i can print 2 dates on 1 page?
    That happens because you print a form feed character (\f) each time the body of that while loop is executed and each while-body pass prints half of the page. A bit of programming takes care of that; here's a bit of pseudo code:

    Java Code:
    boolean printFF= false;
    while (<another date can be read>) {
       print(<date>);
       if (printFF)
          print(<a form feed character>);
       else 
          print(<half a page of lines>);
       printFF= ~printFF;
    }
    The last line toggles the truth value (true or false) of the printFF boolean variable.

    kind regards,

    Jos

  10. #10
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    quick question...instead of having 20 of these, out.newLine(); is there a way i can skip 20 lines in a cleaner way?

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kahoots View Post
    quick question...instead of having 20 of these, out.newLine(); is there a way i can skip 20 lines in a cleaner way?
    A simple loop mayhap?

    Java Code:
    for (int i= 0; i < 20; i++)
       out.newLine();
    kind regards,

    Jos

  12. #12
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks Jos.

    I should have known that...

    Question, is there a way to format how out.write prints out?
    For example,
    out.write("a b c d e f");
    out.write(" [][][][][]1 2 3 4 5");

    I had to add a few spaces (forum doesnt show 5 spaces so i used [] to represent it) in front of "1" in order for it to print like this:
    a b c d e f
    1 2 3 4 5 6

    If i didnt add those spaces it would look like this:
    a b c d e f
    1 2 3 4 5

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kahoots View Post
    Thanks Jos.

    I should have known that...

    Question, is there a way to format how out.write prints out?
    For example,
    out.write("a b c d e f");
    out.write(" [][][][][]1 2 3 4 5");

    I had to add a few spaces (forum doesnt show 5 spaces so i used [] to represent it) in front of "1" in order for it to print like this:
    a b c d e f
    1 2 3 4 5 6

    If i didnt add those spaces it would look like this:
    a b c d e f
    1 2 3 4 5
    Sure, again you should've known that: print those spaces, your printer isn't psychic nor is Java. Be careful with proportional fonts (i.e. not every character has the same width as a space character).

    kind regards,

    Jos

  14. #14
    kahoots is offline Member
    Join Date
    Jul 2010
    Posts
    7
    Rep Power
    0

    Default

    Jos,
    Do you mean I have to use those spaces in order to get it output aligned?

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kahoots View Post
    Jos,
    Do you mean I have to use those spaces in order to get it output aligned?
    Yep, or tabs or whatever your printer is capable of to align two columns ...

    kind regards,

    Jos

Similar Threads

  1. iReport - Print on page bigger than A4
    By mobydickJavaCoder in forum Advanced Java
    Replies: 1
    Last Post: 05-26-2010, 10:55 AM
  2. How to break a text file into block.
    By kinnylaw in forum New To Java
    Replies: 1
    Last Post: 11-28-2009, 08:49 AM
  3. How to easily set page break as you like?
    By freezea in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-06-2009, 03:56 AM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. how to print more than one page
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •