Printing a microsoft word doc using Java Print API

Hi,
I have to print a microsoft word doc.I am using Java Print API, but the code is printing only Hashcodes instead of the actual document.
Here is the code. Please let me know whats wrong in it.
CODE:::
public String print() throws Exception {
String realPath = getRealPath("/images/formLibrary/csaAddressContactRequestForm100.doc");
PrintRequestAttributeSet pras1 = new HashPrintRequestAttributeSet();
DocFlavor flavor1 = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = defaultService.createPrintJob();
FileInputStream fis1 = new FileInputStream(realPath);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc1 = new SimpleDoc(fis1, flavor1, das);
job.print(doc1, pras1);
Thread.sleep(10000);
System.exit(0);
return "";
}

By using an appropriate library. JText, whatever.
Google, man.I think Rene meant iText!Whatever. :) Never used it, I just remembered there was something named like that. Thanks.

Similar Messages

  • Access microsoft word doc using java

    how can i access microsoft word using java .
    I want to select text from the word in the same format as it is.
    if possible can u all help me

    By using an appropriate library. JText, whatever.
    Google, man.I think Rene meant iText!Whatever. :) Never used it, I just remembered there was something named like that. Thanks.

  • Printing a document from firefox web page or Microsoft Word doc ument used to work just fine. All of a sudden I was unable to print a Word document. I can still print a web page. Any suggestions?

    I have a document from Microsoft Word that will not print. It's as if the printer is not connected when I attempt with the Word document. I have gone in and deleted any pending documents in Microsoft Word. I have restarted computer, as well as the printer. The printer does not seem to be connecting or responding to the Word program (cannot find spooler) Any suggestions?

    I have a problem similar to those posted here, with one difference. With my printers newly installed, regardless of which one is selected as the default, when I try to print from Adobe Reader I receive the error message we're talking about : "Before you can perform a print related task...."
    Here's the fun part.
    If I click OK on the message, ALL MY PRINTERS GET DELETED and, I get the windows message that "Spooler subsystem app has encountered a problem and needs to close"! If instead of clicking OK I switch to Task Manager and end Adobe, I'm fine and can print from other programs.
    I can "reset" the problem by deleting the files in C:\Windows\System32\Spool\Printers and C:\Windows\System32\Spool\Drivers\w32x86, restarting the machine, and deleting then reinstalling the printers (which show up again after the restart).
    BUT, if I try to print from Adobe Reader again, I get the same cycle of problems.
    I am running Windows Vista and Adobe Reader 8.1

  • Print .PDT,.DOC,.XLS files using java print API

    Hi,
    I need to print different types of documents like pdf, word, excel files etc.
    Please let me know how to proceed on this.
    I tried using the following code, but it is printing all junk/html tags on the paper.
    Please let me know how to resolve this.
    Thanks in advance
    import java.io.File;
    import java.io.IOException;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    public class PrintDocument {
      static public void main(String args[]) throws Exception {
        try {
             PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
            DocPrintJob printerJob = defaultPrintService.createPrintJob();
            File pdfFile = new File("C:\\temp\\sample.doc");
            SimpleDoc simpleDoc = new SimpleDoc(pdfFile.toURL(), DocFlavor.URL.AUTOSENSE, null);
            printerJob.print(simpleDoc, null);
        } catch (IOException ie) {
          ie.printStackTrace();
        } catch (PrintException pe) {
          pe.printStackTrace();
    }

    Hi
    I would like to see if it was possible.  I thought it would be easy, as
    there is a standard batch processing sequence (Print 1st page of all) using
    Java that comes with Acrobat 7.  This allows you to print the first page of
    a number of files that you select when the sequence is run.  Its code is:
    /* Print 1st Page */
    /* This sequence prints the first page of
       each document selected to the default printer.
    this.print
    To my uninformed mind it seemed logical that the same code, slightly
    modified to print all pages, should work from within a form.
    Anyway, if there is a way to choose individual files, I would appreciate
    that.
    Thanks
    Rob

  • Can Java be used to parse Microsoft Word(.doc) files?

    Hi guys ,
    I want to know whether Java can be used to parse Microsoft Word(.doc) files for searching a string or for checking for grammatical errors, etc
    Thanks in advance.
    Avichal

    Hey man, anything and every thing can be done these days.
    About ur question doc is like all other normal text files with some extra features and extra character supports and other stuffs.
    If u neglect those parts and if u consider it to be a normal text file then its a much simpler job.
    Here is a code that searches for the key word in all the doc files, txt files, pdf files and html files
    in the mentioned folder and sub folders. Any way its a servlet u can change it to a normal program.
    It first check the file to know whether they are doc, pdf, html or txt files if yes then it will read the file and
    store the contents in the vector and parse the vector for the search string and display the result.
    Along with the result the below code will also display the time taken and the number of search string found in the document
    import java.io.*;
    import java.util.*;
    import java.net.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class search_local extends HttpServlet
         public void service( HttpServletRequest _req, HttpServletResponse _res ) throws ServletException, IOException
              long startTime = System.currentTimeMillis();          
              File RootDir     = new File( _req.getRealPath( "/docs/" ) );
              if ( RootDir.isDirectory() == false )
                   System.out.println( "Invalid directory" );
                   _res.setStatus( HttpServletResponse.SC_NO_CONTENT );
                   return;
              Vector kList = new Vector( 3 );
              StringTokenizer st = new StringTokenizer( _req.getParameter( "search_text" ), "+" );
              while ( st.hasMoreTokens() )
                   kList.addElement( st.nextToken().trim() );
              //- Run through list
              Vector toBeDone     = new Vector( 10 );
              Vector found     = new Vector( 10 );
              String dir[] = RootDir.list( new htmlFilter() );
              cDirInfo tX = new cDirInfo( RootDir, dir );
              toBeDone.addElement( tX );
              while (  toBeDone.isEmpty() == false )
                   tX = (cDirInfo)toBeDone.firstElement();
                   try
                        int x = 0;
                        for ( ;; )
                             File newFile = new File( tX.rootDir, tX.dirList[x] );
                             if ( newFile.isDirectory() )
                                  File t = new File( tX.rootDir, tX.dirList[x] );
                                  String a[] = newFile.list( new htmlFilter() );
                                  toBeDone.addElement( new cDirInfo( t, a ) );
                             else
                                  int freq = searchFile( kList, newFile );
                                  if ( freq != 0 )
                                       found.addElement( new cPage( freq, newFile ) );                              
                             x++;
                   catch( ArrayIndexOutOfBoundsException E ){}
                   toBeDone.removeElementAt(0);
                   dir     = null;
              long totalTime = System.currentTimeMillis()     - startTime;
              formatResults( found, kList, totalTime, _req.getRealPath( "/docs" ), _res );
         private void formatResults( Vector _fList, Vector _kList, long time, String _root, HttpServletResponse _res ) throws IOException
                 _res.setContentType("text/html");
              PrintWriter Out = new PrintWriter( _res.getOutputStream() );
              Out.println( "<HTML><HEAD><TITLE>Search results</TITLE></HEAD>" );
              Out.println( "<BODY><H3>Search Results</H3><BR>" );
              Out.println( "Keywords:<B> " );
              Enumeration E = _kList.elements();
              while ( E.hasMoreElements() )
                   Out.println( (String)E.nextElement() + " : " );
              Out.println( "</B><BR><BR><CENTER><HR WIDTH=100%></CENTER><BR>" );
              E = _fList.elements();
              cPage sPage;
              String link;
              while ( E.hasMoreElements() )
                   sPage = (cPage)E.nextElement();
                   link  = sPage.cFile.toString();
                   link  = "http://localhost/BugFix/docs/" + link.substring( link.indexOf( _root )+_root.length(), link.length() );
                   Out.println( "<FONT SIZE=+1><A HREF=" + link + ">" + sPage.cFile.getName() + "</A></FONT>" );
                   Out.println( "<FONT SIZE=-2>(" + sPage.freq + ")</FONT><BR>" );
              if ( _fList.size() == 0 )
                   Out.println( "<I><B>No sites found!</I></B><BR>");
              Out.println( "<BR><CENTER><HR WIDTH=100%></CENTER>" );
              Out.println( "<BR><FONT SIZE=-1>Time to complete: " + ((double)time/1000) + " seconds</FONT>" );
              Out.println( "</BODY></HTML>" );
              Out.flush();
         private int searchFile( Vector _klist, File _filename )
              //- Links the file
              int     frequency=0;
              try
                   DataInputStream In     = new DataInputStream( new FileInputStream( _filename ) );
                   String LineIn, token;
                   boolean bValid = true;
                   Enumeration E;
                   cLineParse lp;
                   while ( (LineIn = In.readLine()) != null )
                        lp = new cLineParse( LineIn.toUpperCase() );
                        while ( (token=lp.nextToken()) != "" )
                             if ( token.indexOf( "<" ) != -1 && (
                                   token.indexOf( "<A" ) != -1 ||
                                   token.indexOf( "<HE" ) != -1 ||
                                   token.indexOf( "<APP" ) != -1 ||
                                   token.indexOf( "<SER" ) != -1 ||
                                   token.indexOf( "<TEX" ) != -1  ))
                                  bValid  = false;
                             else if (     token.indexOf( "<" ) != -1 && (
                                            token.indexOf( "</A" ) != -1 ||
                                            token.indexOf( "</HE" ) != -1 ||
                                            token.indexOf( "</APP" ) != -1 ||
                                            token.indexOf( "</SER" ) != -1 ||
                                            token.indexOf( "</TEX" ) != -1  ))
                                  bValid  = true;
                             else if ( bValid )
                                  E = _klist.elements();
                                  String key;
                                  while ( E.hasMoreElements() )
                                       key     = ((String)E.nextElement()).toUpperCase();
                                       if ( token.indexOf( key ) != -1 )
                                            frequency++;
                   In.close();
              catch( IOException E ){}
              return frequency;
    class cPage extends Object
         public int     freq;
         public File cFile;
         public cPage( int _freq, File _cFile )
              freq = _freq;
              cFile = _cFile;
    //- End of file
    //----- Supporting classes
    class htmlFilter implements FilenameFilter
         public boolean accept(File dir, String name)
              File tF     = new File( dir, name );
              if ( tF.isDirectory() )
                   return true;
              int indx = name.lastIndexOf( "." );
              if ( indx == -1 )
                   return false;
              String Ext = name.substring( indx+1, name.length() ).toLowerCase();
              if ( Ext.equals( "html" ) ||
                    Ext.equals( "pdf" ) ||
                    Ext.equals( "txt" ) ||
                    Ext.equals( "doc" ) )
                    return true;
              return false;
    class cDirInfo
         public File     rootDir;
         public String[] dirList;
         public cDirInfo( File _r, String[] _d )
              rootDir     = _r;
              dirList = _d;
    class cLineParse
         String L;
         public cLineParse( String _s )
              L = _s;
         public String nextToken()
              String ns="";
              boolean bStart = false;
              for ( int x=0; x < L.length(); x++ )
                   if ( L.charAt(x) == '<' && ns.length() != 0 )
                        L = L.substring( x, L.length() );
                        return ns;
                   else if ( L.charAt(x) == '<' )
                        ns     = ns + L.charAt( x );
                        bStart = true;
                   else if ( L.charAt(x) == '>' ||
                               L.charAt(x) == '\r' ||
                         ( L.charAt(x) == ' ' && bStart == false ) )
                        ns     = ns + L.charAt( x );
                        L = L.substring( x+1, L.length() );
                        return ns;
                   else
                        ns     = ns + L.charAt( x );
              L = "";
              return ns;
    }

  • How to print my microsoft word 2010 document from my hp photosmart c4780

    I just installed microsoft professional office to my hp Pavilion g7-1167 dx Notebook PC.  I created a document using microsoft word, and when I print it, it only prints part of each page.  Do I need an update for my printer?  Or is there another way to fix this?

    Download and install the latest software for your printer from the "Support & Drivers" link at the top of this page.
    Say thanks by clicking "Kudos" "thumbs up" in the post that helped you.
    I am employed by HP

  • How to print a document in reverse order using Java Print API ?

    I need to print a document in reverse order using Java Print API (*Reverse Order Printing*)
    Does Java Print API supports reverse order printing ?
    Thnks.,

    deepak_c3 wrote:
    Thanks for the info.,
    where should the page number n-1-i be returned ?
    Which method implementation of Pageable interface should return the page number ?w.r.t. your first question: don't return that number but return page n-1-i when page i is requested; your document will be printed in reverse order. Your class should implement the entire interface and wrap the original Pageable. (for that number n your class can consult the wrapped interface; read the API for the Pageable interface).
    kind regards,
    Jos

  • Try print from Microsoft Word XP : printer (HP Oficejet 4500 G510n-z) lops off top of random lines

    try print from Microsoft Word XP: printer (HP Officejet 4500 G510n-z) lops off top of random lines

    CD didn't work. Download software, didn't work when it came to plugging into usb port, don't even get a BACK button, nothing. Printer not recognized by laptop. Did all the solutions/uninstall/install etc. Printer is working though, prints out the exact same thing each time I press the green button on the printer, but is not connected to laptop. So it's not the printer, it works.
    Now what?

  • Printing from Microsoft Word

    When I print my documents from Microsoft Word, they will never print with color.  Is there a place where I can change from printing only black and white to printing color as well?

    dumb question - are you trying to print a black and white document in color - sorry had to ask.....
    if you've changed your black and white document to color, try to save it first then try printing it.
    it should print in color if you can do a test page in color....
    ok - try this one - last resort....click on word - preferences - print and select - print background colors and images.

  • Adobe wont print or download word docs. It's taken over our SYSTEM!!!

    What happened to Adobe? Where have you gone? First of all, where is a decent support link that doesn't take you everywhere to avoid direct support or to ask a question?????
    I am a potential cusotmer for our organization. We have loaded the adobe 9 trial. Now, we cannot download ANY email attachments unless they are adobe. If we want to  download a word doc, the adobe page pops up wanting to convert to adobe before doing anything. That's problem #1
    Problem #2, word docs do not print. The same issue. COmes up wanting to convert to adobe, ok, like download, can live with that FOR NOW to get this done but when we convert to adobe, it will NOT print. Printing function goes away. We've tried saving the docs evereyway possible toprint but the vicious cycle starts all over everytime.
    Oh poor adobe, where have you gone?
    If you want this sale, you better provde the fix ASAP.

    The SPACE here is provided by Adobe, but this is NOT Adobe Support
    I'm not sure that Adobe itself even provides support for demo versions
    To get help here, you need to provide DETAILS about your computer and operating system and version of MS Word
    Someone MAY be able to help if you provide details

  • Why won't my officejet 4500 wireless print from microsoft word or excel?

    My new officejet 4500 wireless printer will not print from microsoft word or excel.  The paper goes through the printer but there is nothing printed on it.

    Hello.
    It seems like the black cartridge could be running out of ink. Since most of what is commonly printed from MS word and excel might be black text, this is the first reason I can figure out why your printer is not printing only from these applications.
    Check this document to see if your printer is performing good and to check if ink is coming out from all of the cartridges.
    Cheers.
    Wixma.
    I am an HP employee.
    Say thanks by clicking the Kudos star in the post.
    If my reply resolved your problem, please mark it as as Accepted Solution so that it can be found easier by other people.

  • 2-sided print in Microsoft Word for Mac 2011

    How do I turn ON 2-sided print in Microsoft Word for Mac 2011 version 14.3.5?

    Depending on what year Microsoft you have. If you have 2011, go to File + Print or Command P. Where you see copies & pages, click on the drop down box. Select Layout. Click box next to 2 sided.

  • How to invoke Default printer of my system by using java application

    I need to invoke default printer of my system by using java application ? could u plz help me out with sample code?

    VoodooMagic.getDefaultPrinter().print();
    http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html
    Look for more at Google.

  • Unable to convert Microsoft Word doc. to PDF in Words (there is no response)

    Unable to convert Microsoft Word doc to PDF in Words (Does not respond) or Create PDF from a Word doc. in Adobe Acrobat X Standard 10.1.1 with all updates installed. I receive apop-up saying "Missing PDF Maker Files: Dou you want to run the installer in Repair Mode"  I have done this several times. I have un-installrd and re installed the program twice. Still does not work. I'm running Windows 7 Home version and Microsoft Office XP 2002. This is a brabd new Acrobat program right out of the box. Suggestions Please.

    In WORD 2002, I believe you can only print to the Adobe PDF printer. I think that WORD 2003 is the first compatible with AA X. Check out http://kb2.adobe.com/cps/333/333504.html.

  • Printing from the Word to the pdf printer,

    Printing from the Word to the pdf printer,
    I wanted to print a Word document using the pdf printer but sounds this printer doesn’t provide a dialogue box to choose where to save the printed pdf file. It prints the document but couldn’t know where does the acrobat stick it!
    What might be the problem?
    Is there some option that controls this behaviour?
    Thank you for the help,
    Best
    Jamal

    If you select the properties (or preferences) when right clicking on the Adobe PDF printer, there is a Settings tab (if not select the Advanced button on the general tab) and select the settings tab. There are selections there for requestion the file name and other items related to PDF creation. Sorry if my selections are not clear, I am on the wrong computer to check the correct selections for you, so give it a shot and try various selections until you find the settings.

Maybe you are looking for