Results 1 to 6 of 6
  1. #1
    west-wot is offline Member
    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    Default printing using java

    Hey guys

    i am new to java,

    i need to create a program that will send a document to a printer sadly i have never used java before

    whats the best way to create a printing program

    eventually it will be used as part of a jsf web app

    are there any tools like tutorials or anything out there that would help me

    what would you recommend i start with ?

  2. #2
    west-wot is offline Member
    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    Default Re: printing using java

    for example i have found the code below, what do i need to do to run this ?
    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.awt.print.*;
    import java.text.*;
    /**
    * The PrintText application expands on the
    * PrintExample application in that it images
    * text on to the single page printed.
    */
    public class PrintText implements Printable {
    /**
    * The text to be printed.
    */
    private static final String mText = 
    "Four score and seven years ago our fathers brought forth on this " 
    + "continent a new nation, conceived in liberty and dedicated to the "
    + "proposition that all men are created equal. Now we are engaged in "
    + "a great civil war, testing whether that nation or any nation so "
    + "conceived and so dedicated can long endure. We are met on a great "
    + "battlefield of that war. We have come to dedicate a portion of "
    + "that field as a final resting-place for those who here gave their "
    + "lives that that nation might live. It is altogether fitting and "
    + "proper that we should do this. But in a larger sense, we cannot "
    + "dedicate, we cannot consecrate, we cannot hallow this ground."  
    + "The brave men, living and dead who struggled here have consecrated "
    + "it far above our poor power to add or detract. The world will "
    + "little note nor long remember what we say here, but it can never "
    + "forget what they did here. It is for us the living rather to be "
    + "dedicated here to the unfinished work which they who fought here "
    + "have thus far so nobly advanced. It is rather for us to be here "
    + "dedicated to the great task remaining before us--that from these "
    + "honored dead we take increased devotion to that cause for which "
    + "they gave the last full measure of devotion--that we here highly "
    + "resolve that these dead shall not have died in vain, that this "
    + "nation under God shall have a new birth of freedom, and that "
    + "government of the people, by the people, for the people shall "
    + "not perish from the earth.";
    /**
    * Our text in a form for which we can obtain a
    * AttributedCharacterIterator.
    */
    private static final AttributedString mStyledText = new AttributedString(mText);
    /**
    * Print a single page containing some sample text.
    */
    static public void main(String args[]) {
    /* Get the representation of the current printer and 
    * the current print job.
    */
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    /* Build a book containing pairs of page painters (Printables)
    * and PageFormats. This example has a single page containing
    * text.
    */
    Book book = new Book();
    book.append(new PrintText(), new PageFormat());
    /* Set the object to be printed (the Book) into the PrinterJob.
    * Doing this before bringing up the print dialog allows the
    * print dialog to correctly display the page range to be printed
    * and to dissallow any print settings not appropriate for the
    * pages to be printed.
    */
    printerJob.setPageable(book);
    /* Show the print dialog to the user. This is an optional step
    * and need not be done if the application wants to perform
    * 'quiet' printing. If the user cancels the print dialog then false
    * is returned. If true is returned we go ahead and print.
    */
    boolean doPrint = printerJob.printDialog();
    if (doPrint) {
    try {
    printerJob.print();
    } catch (PrinterException exception) {
    System.err.println("Printing error: " + exception);
    }
    }
    }
    /**
    * Print a page of text.
    */
    public int print(Graphics g, PageFormat format, int pageIndex) {
    /* We'll assume that Jav2D is available.
    */
    Graphics2D g2d = (Graphics2D) g;
    /* Move the origin from the corner of the Paper to the corner
    * of the imageable area.
    */
    g2d.translate(format.getImageableX(), format.getImageableY());
    /* Set the text color.
    */
    g2d.setPaint(Color.black);
    /* Use a LineBreakMeasurer instance to break our text into
    * lines that fit the imageable area of the page.
    */
    Point2D.Float pen = new Point2D.Float();
    AttributedCharacterIterator charIterator = mStyledText.getIterator();
    LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
    float wrappingWidth = (float) format.getImageableWidth();
    while (measurer.getPosition() < charIterator.getEndIndex()) {
    TextLayout layout = measurer.nextLayout(wrappingWidth);
    pen.y += layout.getAscent();
    float dx = layout.isLeftToRight()? 0 : (wrappingWidth - layout.getAdvance());
    layout.draw(g2d, pen.x + dx, pen.y);
    pen.y += layout.getDescent() + layout.getLeading();
    }
    return Printable.PAGE_EXISTS;
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: printing using java

    What is it exactly that you're trying to print?
    There are frameworks that might help if it's some sort of report, for example.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    west-wot is offline Member
    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    Default Re: printing using java

    Just a word document or a pdf document

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: printing using java

    In which case the last post in this thread at coderanch should cover it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    west-wot is offline Member
    Join Date
    Dec 2012
    Posts
    16
    Rep Power
    0

    Default Re: printing using java

    Thank you very much, i am sorry but i am extremly new to all this, how would i go about using the code ? how would i be able to run it

    Java Code:
    // create the input stream  
    InputStream in = null;  
    try {  
    log.debug("preparing input stream");  
    in = getFileTobePrinted();  
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;  
      
    // find the printing service  
    log.debug("fetching print service");  
    AttributeSet attributeSet = new HashAttributeSet();  
    attributeSet.add(new PrinterName("lq2170", null));  
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attributeSet);  
      
    // create the print job  
    log.debug("creating print job");  
    DocPrintJob job = services[0].createPrintJob();  
    Doc doc = new SimpleDoc(in, flavor, null);  
      
    // monitor print job events  
    log.debug("preparing print job monitor");  
    PrintJobWatcher watcher = new PrintJobWatcher(job);  
      
    // print it  
    log.debug("start printing");  
    job.print(doc, null);  
      
    // wait for the print job is done  
    log.debug("waiting for the printing to finish");  
    watcher.waitForDone();  
      
    log.debug("done !");  
    } finally {  
    if (in != null) try { in.close(); } catch(Exception e) {}  
    }
    also how does this code know what to print ?

Similar Threads

  1. printing in java
    By sar009 in forum Advanced Java
    Replies: 0
    Last Post: 07-07-2012, 08:39 PM
  2. java printing
    By makasoft in forum New To Java
    Replies: 1
    Last Post: 05-19-2012, 01:27 AM
  3. Replies: 0
    Last Post: 12-26-2011, 03:52 PM
  4. Replies: 9
    Last Post: 08-19-2011, 12:59 AM
  5. printing string backwards and printing every other
    By droidus in forum New To Java
    Replies: 22
    Last Post: 03-10-2011, 09:17 AM

Tags for this Thread

Posting Permissions

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