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

Similar Messages

  • Problem downloading .doc, .xls file using servlet

    i have a servlet which downloads the files to client(browser). It works file for .txt file but .doc, .xls files are opend but they contain garbage.
    plz letr me know if u know problem!!!!!
    -amit

    Probably because you output them using the Writer you got from getWriter instead of using the OutputStream you can get from getOutputStream.

  • Creating .xls file Using Java I/O

    Hi,
    I am crearting a .xls file from a oracle database that support japanese charsets(i.e it has data in UTF-8 format). I am creating the file using below java statements:
    1. Writer out = new OuputStreamWriter(new FileOutputStream("temp.xls"));
    2. out.write(data);
    So using above statements I am reading data in UTF-8 format and writing it a file using UTF-8 format.
    But my created .xls files does not allow me to see japanese characters as they visible in a browser. But in place of temp.xls if I use temp.txt or temp.doc , then I am able to see the japanese characters when those files are view using TEXTPAD OR MS-WORD APPLICATIONS.
    I have japanese font installed on my machine. So I am not understanding where the actual problem is with my way of file writing or with MS-EXCEL APPLICATION ....
    Please suggest me some solution ....
    Thanks in advance......
    kumar.

    Actually the way u have told result in the same. Even the file is .csv or .xls I am not able to see some japanese characters in my excel application provided I have japanese fonts installed on my machine. But when using the same servlet if i create an .doc or .txt file i am able to see japanese characters when i open .doc or .txt using MS-WORD OR TEXTPAD LIKE APPLICATIONS.

  • How to open DOC/XLS files using UIWebView on iPhone 3.0?

    Hi,
    I am using the following code to display doc/xlst files on UIWebView. It's surprised that I can get the content of the file but cannot show in WebView.
    NSData *myData = [NSData dataWithContentsOfFile: fileDesPath];
    NSString *ext = [m_openFiles.fileExtension lowercaseString];
    NSString *mimeType;
    if ([ext isEqualToString:@"doc"]) {
    mimeType = @"application/msword";
    }else if([ext isEqualToString:@"xls"]){
    mimeType = @"application/vnd.ms-excel";
    }else if([ext isEqualToString:@"wma"]){
    mimeType = @"audio/x-ms-wma";
    }else if([ext isEqualToString:@"jpg"] || [ext isEqualToString:@"jpeg"]){
    mimeType = @"image/jpeg";
    }else if([ext isEqualToString:@"tif"]||[ext isEqualToString:@"tiff"]){
    mimeType = @"image/tiff";
    }else if([ext isEqualToString:@"htm"]||[ext isEqualToString:@"html"]){
    mimeType = @"text/html";
    }else if([ext isEqualToString:@"mov"]){
    mimeType = @"video/quicktime";
    }else if([ext isEqualToString:@"mp4"]){
    mimeType = @"video/mp4";
    }else if([ext isEqualToString:@"txt"]){
    mimeType = @"text/plain";
    }else{
    mimeType = [NSString stringWithFormat:@"application/%@", ext]; //pdf
    NSLog(mimeType);
    //Here, I can get the myData. The value is the content of the doc file.
    if (myData) {
    [self.myWebView loadData:myData MIMEType:mimeType textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:fileDesPath]];
    After loadData, it cannot show anything in webview!
    Can someone help me in this matter?
    Thanks,
    Message was edited by: Small Apple

    Hi Maurício -
    For your future reference and for the information of everyone who comes across this post in the future:
    We host all of our DataPlugins on our website at ni.com/dataplugins.  There are hundreds of file formats supported through the available DataPlugins, and there is also a link on that site where you can request a DataPlugin be written for file formats that aren't currently supported (such as custom *.CSV files).
    Derrick S.
    Product Manager
    NI DIAdem
    National Instruments

  • How to read the data from Excel file and Store in XML file using java

    Hi All,
    I got a problem with Excel file.
    My problem is how to read the data from Excel file and Store in XML file using java excel api.
    For getting the data from Excel file what are all the steps i need to follow to get the correct result.
    Any body can send me the code (with java code ,Excel sheet) to this mail id : [email protected]
    Thanks & Regards,
    Sreenu,
    [email protected],
    india,

    If you want someone to do your work, please have the courtesy to provide payment.
    http://www.rentacoder.com

  • How to print a text file using Java

    How can I print a text file using Java without converting the output to an image format. Is there anyway I can send the characters in the text file as it is for a print job? I did get a listing doing this ... but that converted the text to an image format before printing....
    THanks,.

    Hi I had to write a print api from scratch, and I did not convert the output to image. Go and read up on the following code. I know there is a Tutorial on Sun about the differant sections of the snippet.
    private void printReport()
         Frame tempFrame = new Frame(getName());
         PrintJob printerJob = Toolkit.getDefaultToolkit().getPrintJob(tempFrame, "Liesltext", null);
         Graphics g = printerJob.getGraphics();
                    //I wrote the method below for calculations
         printBasics(g);
         g.dispose();
         printerJob.end();
    }This alone wont print it you have to do all the calculations in the printBasics method. And as I said I wrote this from scratch and all I did was research first the tutorial and the white papers
    Ciao

  • How to print PDF files using java print API

    Hi,
    I was goign throw lot of discusion and reading lot of forums related to print pdf files using java api. but nothing seems to be working for me. Can any one tell me how to print pdf files using java api.
    Thanks in advance

    Mike,
    Can't seem to get hold of the example described in your reply below. If you could let us have the URL to get then it would be great.
    My GUI application creates a pdf document which I need to print. I want to achieve this using the standard Java class PrinterJob (no 3rd party APIs I'm afraid, commercial restraints etc ..). I had a stab at it using the following code. When executed I get the pretty printer dialog then when I click ok to print, nothing happens!
    boolean showPrintDialog=true;
    PrinterJob printJob = PrinterJob.getPrinterJob ();
    printJob.setJobName ("Contract.pdf");
    try {
    if (showPrintDialog) {
    if (printJob.printDialog()) {
    printJob.print();
    else
    printJob.print ();
    } catch (Exception PrintException) {
                   PrintException.printStackTrace();
    Thank you and a happy new year.
    Cheers,
    Chris

  • Printing PDF files using java

    Is there a class or utility that already does this?
    Thx in advance.

    Do you want to create PDF files using Java? If so, there is a library available at http://www.lowagie.com/iText/docs.html Check out this site. There are many more similar PDFGEnerator tools that you can use..

  • Checking PDF/DOC/XLS files for passwords

    Folks
    Wondering if anyone can shed any light on how to check PDF/DOC/XLS files passwords?
    I'm not trying to crack the password, I'm writing a program where people are uploading files, but I don't want people submitting files that require passwords to open them.
    Tom

    shashi_rajak wrote:
    Open Source PDF Libraries in Java
    http://java-source.net/open-source/pdf-libraries
    use any one of library given in the above page. then try to read the pdf file . if you are able to read then it is not password protected .Or invalid, or too new, or has other featues that those libraries might not support.

  • Cannot receive .doc & .xls files

    I have searched the board, and read that a number of owners receive doc & xls files with no problems. I do receive the e-mail sent to my iPhone, but when it tries to display the file, it always states ( This message has no content ). I use embarqmail, and nothing is blocked. I do receive the attached files in my regular e-mail. They are short, xls = 56K, doc = 19K. Have tried many times, with different files. Any help will be greatly appreciated. Thanks Guy

    what i have found is just that the edge data network is slow to actually download the attachment with the email... the email text comes in quickly but the attachment may take several minutes to actually download and show up...i have found that when i get and attachment i go into my inbox and just let it sit there for several minutes until the paperclip icon shows, then go into the email and the attachment should beavailable, if you go into the email before edge has downloaded it you might not ever get it on the phone... best to just wait until its there for sure...i have had to wait up to 30 minutes for the attachment to show after i received the email...hope this helps

  • How to Write in bold to a file using java I/O classes?

    Hi,
    Using I/O classes I want to prepare a .doc file in which some text will be there. I want that text to be formatted. Like some text I want to be bold. Some text to be italic. How can I write bold/Italic text to the file using java I/O classes.
    Thanks
    Prashant

    By .doc file, I'm assuming you mean an MS Word document, yes? (fyi, Word Perfect also used the .doc extension)
    The .doc format is proprietary to Microsoft and isn't documented by them. In order to output a file in .doc format you'd have to understand that format correctly, otherwise MS Word will spit out it's tongue at you, call you names, and maybe send the Microsoft Police to "audit" your PC Software Licensing.
    Fortunately for you there is an open source project to demangle the microsoft file formats. See http://jakarta.apache.org/poi/ and especially pay attention to the HDF project (Horrible Document Format).
    - David

  • How to gzip a file using java

    Friends,
    I want to gzip a file in unix using my java program, can I use Runtime.exec() method and run the gzip command to achieve this ?
    Is there any other way to zip/compress a file using java api ?
    Thanks

    Use [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html]java.util.zip.GZIPOutputStream along with [url http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileOutputStream.html]java.io.FileOutputStream

  • How to open/read file using Java in Unix?

    Hi Friends,
    Can you please help me out how to open/read file using java in unix os? I have create one text file in "/home/test.txt" in unix environment. How to open the same file & read using java code?
    - Hiren Modi

    http://java.sun.com/docs/books/tutorial/essential/io/index.html

  • Changing HTML to text file using java

    Hi,
    I am doing a project in which i have to read news articles from websites. I have tried XML but for that i need to know which tag has the article in it. Since i have to read from various websites so each site used different tags for different informations.
    Is there anyway that i can change an HTML file into a text file using java. Maybe some command that removes all the HTML tags and gives just the information. Is there anything else that anyone would like to recommend?
    Thanx
    yafis

    Maybe something like this:
    import java.io.*;
    import java.net.*;
    import javax.swing.text.*;
    import javax.swing.text.html.*;
    class GetHTMLText
         public static void main(String[] args)
              throws Exception
              EditorKit kit = new HTMLEditorKit();
              Document doc = kit.createDefaultDocument();
              // The Document class does not yet handle charset's properly.
              doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
              // Create a reader on the HTML content.
              Reader rd = getReader(args[0]);
              // Parse the HTML.
              kit.read(rd, doc, 0);
              //  The HTML text is now stored in the document
              System.out.println( doc.getText(0, doc.getLength()) );
         // Returns a reader on the HTML data. If 'uri' begins
         // with "http:", it's treated as a URL; otherwise,
         // it's assumed to be a local filename.
         static Reader getReader(String uri)
              throws IOException
              // Retrieve from Internet.
              if (uri.startsWith("http:"))
                   URLConnection conn = new URL(uri).openConnection();
                   return new InputStreamReader(conn.getInputStream());
              // Retrieve from file.
              else
                   return new FileReader(uri);
    }

  • How to read pdf files using java.io package classes

    Dear All,
    I have a certain requirement that i should read and write PDF files at runtime. With normal java file IO reading is not working. Can any one suggest me how to proceed probably with sample code block
    Thanks in advance.

    hi I also have the pbm. to read pdf file using JAVA
    can any body help meWhy is it so difficult to read the thread you posted in? They say: java.io is pointless, use iText. So why don't you?
    or also I want to read a binary encoded data into
    ascii,
    can anybody give me a hint how to do it.Depends on what you mean with "binary encoding". ASCII's binary encoding, too, basically.

Maybe you are looking for

  • Talking head with ppt slides... Best Practice?

    My video sequence is 1080i60 (1440x1080) and is about two hours long... The talent is green screened speaking to 60 slides that are off camera. My final output is going to be a combination of just the talent on the screen, just the slides on the scre

  • Project is not rendering

    My project is not rendering. The orange colored indicators in the timeline will not go away. The background task window was spinning for at least two hours without giving the percentage. Now- I don't know how- it just indicates 58 percent. It's not s

  • Migration from Photoshop Elements 5.0

    Can anyone give me some advice as far as importing an Elements 5 catalog? I've been using Elements (and Album before that) for quite a while and have extensively used collections and tags. Collections, in particular, and the order of photos and scans

  • JDBC 2.0 help

    try      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Test;User=usr;Password=secret"); Statement stmt = con.createStatement(ResultS

  • Here's a challenge please read and help.......

    I work for a company of a 1000 people and the owners bought everyone an ipod. the problem is that my boss wants me to record each persons serial number to their ipod and print it out for his records. i know that the serial number is on the back of th