Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Set page margins when printing in java

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Set page margins when printing in java

    Hello,

    I wanted to set page margins and reduce them to accommodate the whole table I am creating.

    This is the code I am using to print :-
    JTextArea result_txt = new JTextArea(""); 
            JScrollPane sp_result_txt = new JScrollPane(result_txt);
            result_txt.setFont(new Font("Courier", Font.PLAIN, 11));
            result_txt.setText("some string generated dynamically");
     
    try {
                boolean complete = result_txt.print();
                if (complete) {
                    System.out.println("Success");
     
                } else {
                    System.out.println("failed");
                }
            } catch (Exception pe) {
                System.out.println("error");
            }

    thanks in advance.

    EDIT:- I just tried margins on JTextArea itself like this
    result_txt.setMargin(new Insets(-10, -10, -10, -10));
    Doesnt work for printing on paper though.

    On a closer look in the dialog box which pops open for print, the margins are pre-set to 1 inch on all top, bottom, left and right.

    If it is possible even zero inch margins will be good.

    Thanks again.


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Set page margins when printing in java

    Alright now I have found this article/code

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.print.PageFormat;
    import java.awt.print.Paper;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
     
    public class MainClass {
      public static void main(String[] args) throws Exception {
        PrinterJob pj = PrinterJob.getPrinterJob();
     
        PageFormat pf = pj.defaultPage();
        Paper paper = new Paper();
        double margin = 9;
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
            - margin * 2);
        pf.setPaper(paper);
     
        pj.setPrintable(new MyPrintable(), pf);
        if (pj.printDialog()) {
          try {
            pj.print();
          } catch (PrinterException e) {
            System.out.println(e);
          }
        }
      }
    }
     
    class MyPrintable implements Printable {
      String str;
      public MyPrintable(String getStr)
      {
         str = getStr;
      }
      public int print(Graphics g, PageFormat pf, int pageIndex) {
        if (pageIndex != 0)
          return NO_SUCH_PAGE;
        Graphics2D g2 = (Graphics2D) g;
        g2.setFont(new Font("Serif", Font.PLAIN, 36));
        g2.setPaint(Color.black);
        g2.drawString(str, 100, 100);
        Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
            .getImageableWidth(), pf.getImageableHeight());
        g2.draw(outline);
        return PAGE_EXISTS;
      }
    }

    Seems to work but then it only prints one line. I think the "\n" in str is neglected totally. How to get around this now. Also if there is a better way to print a JTextArea and specify margins too let me know. Thanks.

Similar Threads

  1. Downloading web page with java script enabled through Java
    By mashimaro in forum Java Networking
    Replies: 1
    Last Post: November 13th, 2010, 09:28 PM
  2. [SOLVED] Printing Data in JAVA
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2010, 01:22 PM
  3. Java layer over a page?
    By millhugz in forum Java Theory & Questions
    Replies: 7
    Last Post: July 1st, 2010, 01:23 PM
  4. control third party page in java.
    By kirti in forum Member Introductions
    Replies: 1
    Last Post: May 2nd, 2010, 03:33 PM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread