Print documents using java

hai,
how can print the text(.txt) file or some message to the printer using Java?
pls answer it.

import java.awt.*;
import java.awt.event.*;
import java.awt.print.Printable;
import java.awt.print.*;
import javax.swing.*;
import java.awt.Graphics;
import java.text.*;
import java.awt.geom.Point2D;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.util.Vector;
public class Print implements Printable {
private AttributedString mStyledText = null;
private Vector msg = new Vector();
public Print( Vector msg ) {
this.msg = msg;
public int print(Graphics g, PageFormat format, int pageIndex) {
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();
for ( int i = 0; i < msg.size(); i++ ) {
mStyledText = new AttributedString((String)msg.get( i ) );
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();
if ( pageIndex > 0 ) {
return this.NO_SUCH_PAGE;
}else {
return this.PAGE_EXISTS;
try this class. the msg Vector contains the text lines that you would like to print.
Hope this helps.

Similar Messages

  • Print documents using java program.

    Java program printing documents in a printer.
    I want to do this in applet..
    but for the time being can atleast a java program do this???

    Of course it can.
    http://www.google.com/search?q=java+printing+tutorial

  • Printing a PDF document using Java 1.4 Printing API

    Hi,
    When I tried to print a PDF document using JAVA 1.4 Printing API, I got the following exception.
    Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
    at sun.print.Win32PrintJob.print(Win32PrintJob.java:290)
    at Printing.main(Printing.java:40)
    I am able to print the same PDF document using Acrobat reader.
    Is PDF format not supported in JAVA 1.4 printing API? or is something wrong in my code?
    here is the sample JAVA program that I was using
    import javax.print.*;
    import javax.print.attribute.*;
    import java.io.*;
    public class Printing {
    public static void main(String args[]) throws Exception {
    String filename = args[0];
    PrintRequestAttributeSet pras =
    new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
    PrintService defaultService =
    PrintServiceLookup.lookupDefaultPrintService();
    DocPrintJob job = defaultService.createPrintJob();
    FileInputStream fis = new FileInputStream(filename);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, flavor, das);
    job.print(doc, pras);
    Thread.sleep(10000);
    System.exit(0);
    Thank you
    Sumana

    Here's a pure Java solution (that works!):
    http://www.mycgiserver.com/~zhouwu/pdf/readme.html
    The caveats are:
    - prints only to the default printer
    - requires a properties file located in the home directory
    my workaround for the last:
         * Print a PDF file to the default printer (might consume lots of memory!).
         * <p>
         * <b>Required:</b>
         * <ul>
         * <li>a file "acrobat.properties" needs to be in the working directory. The content is:
    <table align="center" bgcolor="#E0E0E0" border=1 cellpadding="10" cellspacing="0"><tr><td><pre style="margin-top:0; margin-bottom:0">
    #com.adobe.acrobat.Viewer Properties
    #Wed Oct 29 20:34:05 PST 2003
    com.adobe.acrobat.AcceptedLicAgreement=true
    com.adobe.acrobat.Fax_Fine_Mode=true
    com.adobe.acrobat.Find\:FindAll=false
    com.adobe.acrobat.Find\:FindBackwards=false
    com.adobe.acrobat.Find\:FindWholeWord=false
    com.adobe.acrobat.Find\:MatchCase=false
    com.adobe.acrobat.Open_Dialog_Directory=C\:\\temp\\
    com.adobe.acrobat.Open_Dialog_File=itext.pdf
    com.adobe.acrobat.Print_Method_Known=true
    com.adobe.acrobat.Shrink_To_Fit=false
    com.adobe.acrobat.SitePreferencesURL=file\://localhost/C\:/pdf/acrobat-site.properties
    com.adobe.acrobat.Use_Print_Server=false
    com.adobe.acrobat.util.fontDirectories=C\:\\Winnt\\Fonts
    com.adobe.acrobat.util.fontIgnoreExtensions=.fon;.pfm;.ini;.lst;.txt;.doc;.ttmap;.z;.enc;.dir;.afm;.f3b;.pfa;.spd;.ps;.bepf;.map;.alias;.scale;.all;.upr
    </pre></td></tr></table>
         * <li>The JAR files PDFPrinter.jar, acrobat.jar and MRJToolkitStubs.zip in the CLASSPATH.
         * </ul>
         * <p>
         * Note: the file "acrobat.properties" is expected to be in the user's home directory. As this is not always feasible,
         * the system property "user.home" is mapped to "user.dir" for the time of execution.
         * @param fileName Name of PDF file to print.
         * @throws Exception on error.
         * @see <a href="http://www.mycgiserver.com/~zhouwu/pdf/readme.html">PDF Server (Silent) Printing</a>
        public void printPDF(String fileName) throws Exception {
            String oldHome = System.getProperty("user.home");
            System.setProperty("user.home", System.getProperty("user.dir"));
            PDFPrinter vi = new PDFPrinter();
            vi.activate();
            vi.setDocumentInputStream(new FileInputStream(fileName));
            vi.printAll();
            System.setProperty("user.home", oldHome);
        }//printPDF()I just need the following additional files in my CLASSPATH: PDFPrinter.jar, acrobat.jar and MRJToolkitStubs.zip. Despite the note at the site above that "printing quality is not very good", i can't agree: the print quality is quite good. Not perfect, but acceptable for most uses.
    (Tested on Windows 2000 SP4, J2SE 1.4.2_03, hp laserjet 2300dn)

  • Directly Printing RTF document using java

    Hi,
    can we directly print RTF document using java, without giving any preview or any other screen in between.
    Actually we created inventory system, in which when user create any sale information, it should print the bill for the same with single button click.
    Any sample code or help will be highly appreciated
    Thanks in advance

    Hi,
    can we directly print RTF document using java, without giving any preview or any other screen in between.
    Actually we created inventory system, in which when user create any sale information, it should print the bill for the same with single button click.
    Any sample code or help will be highly appreciated
    Thanks in advance

  • Writing to word document using java

    hi,
    i want to retrieve the values from database and then write to .doc file. how can i write to a word document using java?
    thanks in advance

    Google for "java write to word file"
    and behold the magic results

  • How to print documents using correspondence types?

    Sir/Madam,
    I am in the process of preparing the End User document for "How to print documents using correspondence types?  on client request.Plz give me the required stuff to prepare that.This is an urgent requirement.help me ASAP.
    Thanks in advance
    Regards
    Sap Guru
    [email protected]

    Hi
    use t.codes, FB12 & F.64
    Award points if it is useful
    Regards,
    A.Anandarajan

  • Hello, I have installed a new Laserjet Pro M1217nfl using Wi-Fi. My laptop prints everything e-mails, Word documents etc. My iPad 3 (latest issue) only prints documents using Wi-Fi so has clearly recognised the printer. How can I prin e-mails using iPad 3

    Hello, I have installed a new Laserjet Pro M1217nfl using Wi-Fi. My laptop prints everything e-mails, Word documents etc. My iPad 3 (latest issue) only prints documents using Wi-Fi so has clearly recognised the printer. How can I print e-mails? Thanks.

    In the email you want to print, on the top right there is an arrow. Click that and an option should appear that says print. That should work. Have you already tried that?

  • How To open a MS Word/Excel document using Java

    How do i open a MS-Word/Excel document using Java Code.

    Get SDK (which is freeware) at
    http://www.simtel.net/product.php?id=60701&sekid=0&SiteID=simtel.net
    http://shareware.pcmag.com/product.php?id=60701&SiteID=pcmag
    http://downloads.suntimes.com/product.php?id=60701&SiteID=suntimes
    There you will find examples for MSWord and Excel (example sources are packed with binaries).

  • Markup PDF document using Java

    I want to view/render and annotate/Markup PDF document using Java. How can I achieve it?
    On [http://www.java-junction.blogspot.com|http://www.java-junction.blogspot.com] it is mentioned rendering PDF using PDFRender tool. But how can I create annotate/Markup on PDF Document.
    Is there any Open Source Tool avaible for that?

    I want to view/render and annotate/Markup PDF document using Java. How can I achieve it?
    On [http://www.java-junction.blogspot.com|http://www.java-junction.blogspot.com] it is mentioned rendering PDF using PDFRender tool. But how can I create annotate/Markup on PDF Document.
    Is there any Open Source Tool avaible for that?

  • Im unable to print documents using Adobe.

    Hi all,
    Im unable to print documents using Adobe.
    Ive uninstalled and reinstalled Adobe Reader 3 times, since last week.
    I can open the document, and select print. I get the 'progressing' popup, then I get an Adobe pop-up that says 'The document could not be printed' Then I click ok, then I get another that says 'There were no pages selected to print'
    Ive checked for updates in Adobe, Ive turned anti-virus off while reinstalling, Ive reinstalled printer drivers. Still cant print.
    Im able to print from another computer.
    Does anyone have any suggestions? As this is a work computer and I need to print work requests often.
    Thanks

    Hi Laksamj,
    Please try the steps provided in the link mentioned below:
    http://helpx.adobe.com/acrobat/kb/troubleshoot-pdf-printing-acrobat-reader.html
    This can happen in following scenarios.
    1) if the connection to the printer you are trying to print to is not established.
    2) if the PDF is badly made or does not confirm to pdf standards.
    3) if you are printing to a plotter then the workaround is to uncheck the auto-rotate setting in plotter preferences directly.
    If still you experience the same issue, then provide us some more details like:
    1. OS you are using?
    2. Are you able to print with the other applications on your system?
    3. Is the issue file specific or with all pdf's on your computer?
    4. Printer model you are using?
    5. Were you facing similar issues with the previous versions of Reader?

  • BI Publisher Merge PDF and print documents through Java

    We need our reports in merged and printed (collated). Is this possible with BI Publisher . If yes, how?
    The reports will be called from Java. So far I found the Web Service provided by BI Publisher handy in printing and creating reports but not combine them.
    Thanks in advance.
    Edited by: 829489 on Mar 6, 2013 8:28 AM

    check this links . it may help you
    http://technology.amis.nl/2007/03/19/merging-pdf-documents-using-the-bi-publisher-api/
    https://blogs.oracle.com/xmlpublisher/entry/merge_and_secure_pdfs
    https://blogs.oracle.com/xmlpublisher/entry/file_attachments
    http://docs.oracle.com/cd/E10091_01/doc/bip.1013/b40017/T421739T479695.htm#T477455

  • How to open Word Document using java?

    Respected Sir/ Madam,
    I am doing my project in Network Security using java. I have to transfer the files from server to the client. Each transferred file should have some of the access privileges such as read, write and exeucte. If the transferred file is a word document means then automatically the client should open Microsoft word and if the file is having Read Permission alone then the save, Save As, cut,copy, paste options in the MS - Word should be disabled. This is similar for all the files that i have transferred. That is if the transferred file is excel, audio, video,txt file the corresponding application should be opened properly. How could I acheive this. If the file is having write permission then if the user clicks the save option then the contents of the file should be saved in the server machine not in the client machine, Could we achieve this???? Can anyone help me regarding this? Please give me your views about this query?

    Respected Sir / Madam,
    Thank you very much for your reply. Could we use Microsoft API inside java program to achieve my doubts?? Could U explain it more????

  • How to append child to XML document using Java

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
         <bean name="PinnacleReportsData"
              class="com.hsbc.webapp.report.view.GenericPinnacleReportsViewData"
              singleton="false">
              <property name="headers">
                   <value>Account|Cusip|Description|SecType|Sector|Holding|Price|MarketValue|SettleDate|Yield|SprdUST|AL|CFDur|OAS|ZVolOas|OADur|PrepayDur|VolDur|DV01|DVOAS01|KRDur10Y|PrepayModelCalled|CalcError|CalculationTime</value>
              </property>     
              <property name="noOfRows">
                   <value>20</value>
              </property>     
              <property name="rowdata">
                   <list>
                        <value>AA09b|CMO|8000|8000|62052MAD0|MFPLC_4A 2A 144A||8097|99-27+|5.18|0.25|200|0.00||15||||</value>
                        <value>AA09b|CMO|10000|10000|320277AD8|HASCO 2005 F II-A-2||9958|99-13|5.69|0.13|131|0.02||154|0.000|25|0.001|9864</value>
                        <value>AA09b|CMO|8000|2363|46071LAA8|IMT_03-3G A2||2378|100-01|5.18|0.14|33|0.00||2||||</value>
                        <value>AA09b|CMO|1270|312|40430HDW5|HASCO 2006-O II-A-1||309|99-00|8.98|0.16|5|0.01||3|0.000|1|0.001|296</value>
                        <value>AA09b|CMO|5000|5000|40430HFQ6|HASCO 2006-O M-3||4583|91-15+|8.99|1.16|534|-0.58|-2|-2644|0.016|712|0.080|367625</value>
                        <value>AA74b|ABS|8000|8000|62052MAD0|MFPLC_4A 2A 144A||8097|99-27+|5.18|0.25|200|0.00||15||||</value>
                        <value>AA74b|ABS|10000|10000|320277AD8|HASCO 2005 F II-A-2||9958|99-13|5.69|0.13|131|0.02||154|0.000|25|0.001|9864</value>
                        <value>AA74b|ABS|8000|2363|46071LAA8|IMT_03-3G A2||2378|100-01|5.18|0.14|33|0.00||2||||</value>
                        <value>AA74b|ABS|1270|312|40430HDW5|HASCO 2006-O II-A-1||309|99-00|8.98|0.16|5|0.01||3|0.000|1|0.001|296</value>
                        <value>AA74b|ABS|5000|5000|40430HFQ6|HASCO 2006-O M-3||4583|91-15+|8.99|1.16|534|-0.58|-2|-2644|0.016|712|0.080|367625</value>
                   </list>
              </property>
         </bean>
    </beans>               hi there i have a XML Document above,and i need to append values to the <list> using java.
    i have string values like
         <value>AA74b|ABS|5000|5000|40430HFQ6|HASCO 2006-O M-3||4583|91-15+|8.99|1.16|534|-0.58|-2|-2644|0.016|712|0.080|367625</value>to add, but failing to navigate to the <list>
    any help?

    1. Read the XML file into a DMO object, walk the DOM to find the list, insert your new entry as a child, write the DOM back to a file.
    2. If the XML is not in a file, but in a string, then you can do the same with string input and output.

  • Pattern matching for thumb print scanner using java

    I want to get some information about security system technology using java to develop my final year project. Please kindly assist me in searching some related information regarding thumb print scanner security system. Anyone who can help me in this, i will really appreciate it, thank you in advanced !

    This is going to be a tough project. Here are a couple of ideas
    1. Java, or whatever language you do it in probably won't matter that much.
    2. You need to find algorithms for matching fingerprints. There are probably some out there... You may need to pay to use them. There are also probably some books out that might help you figure out how to do this, or how other people have done this, or who to contact about doing this.
    3. Partnering up with someone, either in industry (like IBM maybe) or at the very least a professor is going to be important.
    4. Don't try to invent the wheel. Do your research and reuse as much as you can. Getting the algorithm + the scanner are going to be the hard parts

  • Print messages using Java or Jython

    Hi,
    In my ODI procedure I would like to print out some results to make it easier to check the results.
    Is there a way to print out messages in a step using java or jython?
    I tried out to print to stdout - that did not work...
    Thanks,
    Helmut

    Hi H,
    Sure.
    Below is the sample code snippet,
    import os
    ftrg = open('c:/temp/listdir.txt', 'w')
    try:
    __mydir = 'c:/odi'
    __mylist = os.listdir(mydir)
    __mylist.sort()
    __for dirOrFile in mylist:
    ____if os.path.isdir(mydir + os.sep + dirOrFile):
    ______print >> ftrg, 'DIRECTORY: %s' % dirOrFile
    ____else:
    ______print >> ftrg, 'FILE: %s' % dirOrFile
    finally:
    __ftrg.close()
    The above code will print the list of directories in a specified path to a file listdir.txt.
    P.S: If you want to try the code please replace __ with tab. Also, you can have a look at oracledi_jython_reference.pdf for more info.
    Thanks,
    G

Maybe you are looking for