Results 1 to 5 of 5
- 04-13-2011, 07:41 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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 :-
thanks in advance.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) { System.out.println("Success"); } else { System.out.println("failed"); } } catch (Exception pe) { System.out.println("error"); }
EDIT:- I just tried margins on JTextArea itself like this
Doesnt work for printing on paper though.Java Code:result_txt.setMargin(new Insets(-10, -10, -10, -10));
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.
- 04-13-2011, 11:38 AM #2
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
Small problem left now.
Alright now I have found this article/code
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.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); 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; } }
-
Duplicate thread deleted. Please only one thread per question.
- 04-14-2011, 06:05 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 3
- Rep Power
- 0
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.
- 04-14-2011, 07:09 PM #5
Cross posted
Similar Threads
-
printing string backwards and printing every other
By droidus in forum New To JavaReplies: 22Last Post: 03-10-2011, 09:17 AM -
About printing Arraylist on JSP page....
By vaibhavspawar in forum Advanced JavaReplies: 0Last Post: 08-13-2010, 06:52 AM -
Printing only one page problem
By AndreiDMS in forum Java 2DReplies: 0Last Post: 10-09-2009, 03:03 AM -
Problem with margins/spacing
By joseche in forum AWT / SwingReplies: 2Last Post: 12-26-2008, 08:22 PM -
Printing contents of a web page
By Java Tip in forum Java TipReplies: 0Last Post: 11-26-2007, 12:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks