Results 1 to 5 of 5
  1. #1
    simplegoogly is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Set page margins when printing in java


    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 :-
    Java Code:
    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) {
                } else {
            } catch (Exception pe) {
    thanks in advance.

    EDIT:- I just tried margins on JTextArea itself like this
    Java Code:
    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
    simplegoogly is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Small problem left now.

    Alright now I have found this article/code

    Java 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);
        pj.setPrintable(new MyPrintable(), pf);
        if (pj.printDialog()) {
          try {
          } catch (PrinterException 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.drawString(str, 100, 100);
        Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf
            .getImageableWidth(), pf.getImageableHeight());
        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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

  4. #4
    simplegoogly is offline Member
    Join Date
    Apr 2011
    Rep Power


    Sorry for the duplicate thread. I just didnt know where this question will be appropriate.

    Some further information:-

    I am making a string which is properly formatted. You know having "\n" and all. But the print method I have used prints only one line and ignore all the "\n". I was just wondering what is the best way to implement printing a string (or JTextArea) onto a page which has margins set equal to either one-fourth-of-inch or zero.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

Similar Threads

  1. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 10:17 AM
  2. About printing Arraylist on JSP page....
    By vaibhavspawar in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2010, 07:52 AM
  3. Printing only one page problem
    By AndreiDMS in forum Java 2D
    Replies: 0
    Last Post: 10-09-2009, 04:03 AM
  4. Problem with margins/spacing
    By joseche in forum AWT / Swing
    Replies: 2
    Last Post: 12-26-2008, 09:22 PM
  5. Printing contents of a web page
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-26-2007, 01:37 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