Hi,
I am Prakash....
I have tried to print an MS Word file using the basic print utilities provided in JAVA.But while asking for printing through my own code i am getting proble for example..."The given flavour is not supported by utility" and still if i got an printout ,so.. it was not in well formate like the actual MS word File.Please help me to solve this problem.The folowing is the code that i have used to print MS Word File. -
package Temp;
-
-
import java.lang.reflect.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
import java.awt.print.*;
-
import java.awt.Graphics2D;
-
import java.io.*;
-
-
import javax.swing.*;
-
import javax.print.*;
-
import javax.print.attribute.*;
-
import javax.print.attribute.standard.*;
-
import javax.print.event.*;
-
-
public class BasicPrint {
-
JFrame frame;
-
JButton btn;
-
private boolean PrintJobDone = false;
-
-
protected void MakeGui() {
-
-
frame = new JFrame("PrintService");
-
-
btn = new JButton("Cancel Print Job");
-
btn.disable();
-
frame.getContentPane().add(btn, BorderLayout.SOUTH);
-
frame.pack();
-
frame.setVisible(true);
-
}
-
-
BasicPrint(String FileToPrint, String pMode) {
-
-
try {
-
-
MakeGui();
-
-
File baseDir = new File("d:/doc");
-
File outDir = new File(baseDir, FileToPrint);
-
-
// Open the image file
-
InputStream is = new BufferedInputStream(new FileInputStream(
-
outDir));
-
-
// Find the default service
-
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
-
-
if (pMode != null && pMode.equalsIgnoreCase("TXT"))
-
// flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF8;
-
flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_16LE;
-
else if (pMode != null && pMode.equalsIgnoreCase("PS"))
-
flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
-
else if (pMode != null && pMode.equalsIgnoreCase("PDF"))
-
flavor = DocFlavor.INPUT_STREAM.PDF;
-
else if (pMode != null && pMode.equalsIgnoreCase("JPG"))
-
flavor = DocFlavor.INPUT_STREAM.JPEG;
-
else if (pMode != null && pMode.equalsIgnoreCase("GIF"))
-
flavor = DocFlavor.INPUT_STREAM.GIF;
-
else if (pMode != null && pMode.equalsIgnoreCase("PNG"))
-
flavor = DocFlavor.INPUT_STREAM.PNG;
-
else if (pMode != null && pMode.equalsIgnoreCase("PCL"))
-
flavor = DocFlavor.INPUT_STREAM.PCL;
-
else if (pMode != null && pMode.equalsIgnoreCase("RAW"))
-
flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
-
-
System.err.println("* IMPRIMIR " + FileToPrint + " " + pMode + " "
-
+ flavor);
-
-
PrintService dservice = PrintServiceLookup
-
.lookupDefaultPrintService();
-
-
PrintService[] services = PrintServiceLookup.lookupPrintServices(
-
flavor, null);
-
-
if (services == null || services.length < 1)
-
services = PrintServiceLookup.lookupPrintServices(null, null);
-
-
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
-
aset.add(new Copies(1));
-
aset.add(OrientationRequested.PORTRAIT);
-
// aset.add(MediaTray.MAIN);
-
aset.add(Sides.ONE_SIDED);
-
aset.add(MediaSizeName.ISO_A4);
-
PrintService service = ServiceUI.printDialog(
-
(GraphicsConfiguration) null, 60, 60, services,
-
(PrintService) dservice, (DocFlavor) flavor, aset);
-
-
if (service != null) {
-
-
// Create the print job
-
final DocPrintJob job = service.createPrintJob();
-
-
Doc doc = new SimpleDoc(is, flavor, null);
-
-
// Monitor print job events; for the implementation of
-
// PrintJobWatcher,
-
-
PrintJobWatcher pjDone = new PrintJobWatcher(job);
-
-
if (job instanceof CancelablePrintJob) {
-
-
btn.addActionListener(new ActionListener() {
-
public void actionPerformed(ActionEvent evt) {
-
CancelablePrintJob cancelJob = (CancelablePrintJob) job;
-
try {
-
cancelJob.cancel();
-
} catch (PrintException e) {
-
// Possible reason is job was already finished
-
}
-
}
-
});
-
-
btn.enable();
-
}
-
-
try {
-
-
// Print it
-
-
job.print(doc, (PrintRequestAttributeSet) aset);
-
-
} catch (PrintException e) {
-
e.printStackTrace();
-
}
-
-
System.err.println("* Impresion Realizada - Esperando ..");
-
// Wait for the print job to be done
-
pjDone.waitForDone();
-
-
}
-
-
// It is now safe to close the input stream
-
is.close();
-
-
} catch (IOException e) {
-
e.printStackTrace();
-
} catch (Exception e) {
-
e.printStackTrace();
-
} finally {
-
-
try {
-
-
synchronized (BasicPrint.this) {
-
PrintJobDone = true;
-
BasicPrint.this.notify();
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
-
}
-
}
-
-
public synchronized void waitForDone() {
-
try {
-
while (!PrintJobDone) {
-
wait();
-
}
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
-
public static void main(String[] args) {
-
-
try {
-
//args[0]="t";
-
//args[1]="rr";
-
// if (args.length < 1) {
-
// System.err.println("\nSintaxis:\n\n java BasicPrint FileToPrint [pMode]\n");
-
// System.exit(0);
-
// }
-
-
BasicPrint bp = null;
-
-
//if (args.length < 2){
-
//bp = new BasicPrint(args[0], null);
-
-
-
//}else{
-
//bp = new BasicPrint(args[0], args[1]);
-
bp = new BasicPrint("prakashCV.doc","RAW");
-
//}
-
bp.waitForDone();
-
-
System.exit(0);
-
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
-
class PrintJobWatcher {
-
-
// true iff it is safe to close the print job's input stream
-
boolean done = false;
-
-
int lastEvent = 0;
-
-
PrintJobWatcher(DocPrintJob job) {
-
-
// Add a listener to the print job
-
job.addPrintJobListener(new PrintJobAdapter() {
-
-
public void printJobRequiresAttention(PrintJobEvent pje) {
-
lastEvent = pje.getPrintEventType();
-
System.err
-
.println("* La impresora requiere de su Atencion ! * "
-
+ pje);
-
// allDone();
-
}
-
-
public void printDataTransferCompleted(PrintJobEvent pje) {
-
lastEvent = pje.getPrintEventType();
-
System.err
-
.println("* Transferencia de datos a la impresora OK. * "
-
+ pje);
-
// allDone();
-
}
-
-
public void printJobCanceled(PrintJobEvent pje) {
-
lastEvent = pje.getPrintEventType();
-
System.err.println("* Trabajo de impresion CANCELADO ! * "
-
+ pje);
-
allDone();
-
}
-
-
public void printJobCompleted(PrintJobEvent pje) {
-
lastEvent = pje.getPrintEventType();
-
System.err.println("* Impresion completa OK. * " + pje);
-
allDone();
-
}
-
-
public void printJobFailed(PrintJobEvent pje) {
-
lastEvent = pje.getPrintEventType();
-
System.err.println("* ERROR en la Impresion ! * " + pje);
-
// allDone();
-
}
-
-
public void printJobNoMoreEvents(PrintJobEvent pje) {
-
lastEvent = pje.getPrintEventType();
-
System.err
-
.println("* No mas eventos de impresion * " + pje);
-
allDone();
-
}
-
-
void allDone() {
-
-
synchronized (PrintJobWatcher.this) {
-
done = true;
-
PrintJobWatcher.this.notify();
-
}
-
}
-
});
-
}
-
-
/** Description of the Method */
-
public synchronized void waitForDone() {
-
try {
-
while (!done) {
-
wait();
-
}
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
-
}
5 13674 Nepomuk 3,112
Recognized Expert Specialist
Hi prakashturkar! Welcome to bytes.com!
It's great to have you here!
When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags.
About your question: Java doesn't have any built in methods to cope with MS Word documents, so you'd better use a 3rd party library, like Apache POI. Here's a short example of how to use it to read a Word document. I hope, that helps.
Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!
Greetings,
Nepomuk
JosAH 11,448
Recognized Expert MVP
Why don't you just let the Desktop utility class handle the entire job?
kind regards,
Jos
Nepomuk 3,112
Recognized Expert Specialist
That class is part of the normal SE API for Java 1.6
kind regards,
Jos
Ah, that explains why I couldn't find it in the API for Java 1.5! ^^
Just in case, here's the link I was looking for.
Greetings,
Nepomuk
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: MouseHart |
last post by:
I've written a simple program in VB 6.0 to list all my MP3 files. To show
them on the screen I used an MSFlexGrid named TextGrid (which is not
associated with any table or text file) in the following code run by a
button (the files are all in the format "Artist - Name Of Song" in their
directories, which is why the code looks for a dash):
Private Sub Command1_Click()
Dim dashplace%, length%
For I% = 0 To File1.ListCount
FLIName =...
|
by: KohlerTommy |
last post by:
In my application I need to give the user the ability to print duplex if the
selected printer supports duplex printing. Many of the printer options do
not make much sense in my application, and many of the settings in the common
printer dialog would have a negative impact on my printing process. To
handle this strict printing constraint on which my application imposes I do
not want to show the common print dialog. I want to be able to...
|
by: hamil |
last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview
control, the PageSetup control, and the Print dialog control. The code
attached is a concatination of two examples taken out of a Microsoft book,
"Visual Basic,Net Step by Step" in Chapter 18.
All but the bottom two subroutines will open a text file, and then allow me
to use the above controls, example 1. The bottom two subroutines will print
a graphic file, example...
|
by: Ron |
last post by:
Hi All,
Is it possible to have Access print a report, identical to one that would
print to a printer, only print to a "standard" text file? I can't find it
in help and when I try to just print to a file (in the printer selection
screen of the print routines) it gives me a file named what I wanted, but is
just garbage. Can't read it.
TIA
ron
|
by: Tony Girgenti |
last post by:
Hello.
Developing a Windows Form program in VS.NET VB, .NET Framework 1.1.4322 on a
windows XP Pro, SP2.
Before printing a document, i want to set the font to a font that is only
available with the printer that i am printing to(Zebra TLP2844).
When i open Word and look at the fonts available for the default printer, it
does not show the fonts i want. If i cahnge the printer to the printer that
| |
by: ripal.soni |
last post by:
To Print file on your selected printer instead of default printer you
can write the following code
also you can find the complete solution in
http://ripalsoni.wordpress.com/2007/04/25/print-pdf-file-in-vbnet-by-giving-printer-name/
Dim pathToExecutable As String = "AcroRd32.exe"
Dim sReport = "C:Test.PDF" 'Complete name/path of PDF file
Dim SPrinter = "HP Officejet 5600 seriese" 'Name Of printer
|
by: anne marie via DotNetMonster.com |
last post by:
hello. im using ProcessStartInfo to print ms word files (coz this is the only
way i could print it). but it is being printed directly without even showing
the print dialog. is there a way to show the print dialog before i could
print the file? because at times, i need to change the printer settings
manually. i tried mixing
PrintDialog1.ShowDialog ()
with it but it's not working. after i change the printer settings to let's
say,...
|
by: nospam |
last post by:
Hello,
I have about two hundred individual RTF files to print from an
XP host. Word 2000 doesn't seem to have this feature, so I'm looking
for a way to print those RTF files from an ActivePython script. Would
someone have some working code handy?
Thank you.
|
by: riccardonews |
last post by:
Hi groups,
I would like to hook print driver for manage the file that the MS
windows'user want to print.
I don't want to manage EMF file that the print spooler generate..
I have generate a virtual print driver which this command:
"rundll32 printui.dll,PrintUIEntry /if /b "Virtual Printer Test" /f
"%windir%\inf\ntprint.inf" /q /r "lpt1:" /m "driver" /z /u"
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |