Print a file txt in java
Hi
I want to print a file txt in java. I use this code, but the printer receive an empty page. Why???
This is the code: (the fileLocation is right, I've checked this).
private void printFileTextPlain(String fileLocation){
// Attribute Set storage
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// We're looking for PostScript-capable printers
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// Return list of printers capable of printing PostScript
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
// Get the default printer
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
// Display the print dialog with default printer selected
// --->>> CON DEFAULT PRINTER PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, null, flavor, pras);
// If user selected "OK"...
try{
if (service != null) {
// Create a print job
DocPrintJob job = service.createPrintJob();
InputStream is = new BufferedInputStream(new FileInputStream(fileLocation));
//JarInputStream jis = new java.util.jar.JarInputStream(new FileInputStream(new File(fileLocation)));
//jis.getNextEntry();
// Create storage for attributes
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(is, flavor, das);
// Print the job
job.print(doc, pras);
// Wait a few seconds for job to spool
Thread.sleep(10000);
is.close();
catch (IOException e) {
JOptionPane.showMessageDialog(null, "Errore nella generazione del file di stampa","Attenzione", JOptionPane.ERROR_MESSAGE);
//log error
log.error("printFile(): "+e.getMessage(),e);
catch (PrintException pe) {
JOptionPane.showMessageDialog(null, "Errore nella generazione del file di stampa","Attenzione", JOptionPane.ERROR_MESSAGE);
//log error
log.error("printFile(): "+pe.getMessage(),pe);
catch (InterruptedException ie) {
JOptionPane.showMessageDialog(null, "Errore nella generazione del file di stampa","Attenzione", JOptionPane.ERROR_MESSAGE);
//log error
log.error("printFile(): "+ie.getMessage(),ie);
}
There is no speciall handling needed for unicode chars. Problam seems that the viewer that you are using to view the file does not support unicode or the font it is using is not a unicode fond which support chinese chars.
If you switch to a unicode supported viewer and chinese supported unicode font then you should ge the chars properly
Similar Messages
-
How to print pdf file by using java print API ? I am trying with this code
import java.io.FileInputStream;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
public class PDFPrint {
static public void print(InputStream inputStream, PrintService printService) throws PrintException {
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(MediaSizeName.ISO_A4);
attributes.add(new Copies(1));
print(doc, attributes, printService);
}//print()
static public void print(Doc doc, PrintRequestAttributeSet attributes, PrintService printService) throws PrintException {
if (printService == null) {
printService = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("The Printer Name is :"+printService.getName());
DocPrintJob docPrintJob = printService.createPrintJob();
System.out.println("Before Print Start()");
docPrintJob.print(doc, attributes);
}//print()
public static void main(String args[])
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
String file="c:/BackUp/file.pdf";
FileInputStream fis=new FileInputStream(file);
System.out.println("Before Print() called ..");
print(fis,defaultPrintService);
System.out.println("After Printing....");
I am using this code to print pdf file. But when I try this one automatically the printer starting with print ascii codes with infinite loop.
I am using jdk1.4,Acrobat 8.0, Windows environment.
Can u help me in this regard'
Thank u
graniimport java.io.FileInputStream;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
public class PDFPrint {
static public void print(InputStream inputStream, PrintService printService) throws PrintException {
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(MediaSizeName.ISO_A4);
attributes.add(new Copies(1));
print(doc, attributes, printService);
}//print()
static public void print(Doc doc, PrintRequestAttributeSet attributes, PrintService printService) throws PrintException {
if (printService == null) {
printService = PrintServiceLookup.lookupDefaultPrintService();
System.out.println("The Printer Name is :"+printService.getName());
DocPrintJob docPrintJob = printService.createPrintJob();
System.out.println("Before Print Start()");
docPrintJob.print(doc, attributes);
}//print()
public static void main(String args[])
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
String file="c:/BackUp/file.pdf";
FileInputStream fis=new FileInputStream(file);
System.out.println("Before Print() called ..");
print(fis,defaultPrintService);
System.out.println("After Printing....");
I am using this code to print pdf file. But when I try this one automatically the printer starting with print ascii codes with infinite loop.
I am using jdk1.4,Acrobat 8.0, Windows environment.
Can u help me in this regard'
Thank u
grani -
Hello,
I need to print RTF files from a java program. I am reading RTF files in my program and after modification need to send them to a network printer for printing.
Please let me know if there are any tutorials/books available on this.
Thank you very much,
Regards,
Vaishalicheck these urls. may help u some extent.
http://java.sun.com/docs/books/tutorial/2d/printing/index.html
http://www.javaworld.com/javaworld/jw-03-2001/jw-0302-print.html
http://java.sun.com/products/java-media/2D/forDevelopers/sdk12print.html -
How find a word in txt from Java??
Hi everybody...somebody knows how to find an especific word that is located in file txt from java somebody has the code???
Thanks in advanceHere is the code for finding a word in txt file from Java.
import java.io.*;
public class FindTextFromFile
public static String getString(String fileName)
StringBuffer fileContent = new StringBuffer(); //appending long String objects repeatedly is very costly
try
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
String line = "";
while((line = fileReader.readLine())!=null)
fileContent.append(line);
fileContent.append(System.getProperty("line.separator")); //size of line.separator=2
catch (IOException e)
e.printStackTrace();
return fileContent.toString();
public static int[] getIndicesOf(String fileName, String findWord)
String fileContent = getString(fileName);
int[] indices = new int[(int)(fileName.length()/findWord.length())]; //max possible occurances
int index=0, from=0, incr=0;
while ((index=fileContent.indexOf(findWord, from))!=-1)
indices[incr++]=index;
from=index+findWord.length();
indices[incr]=-1; //marking the end of search result storing
return indices;
public static void main(String[] args)
String fileName = "rawTextFile.txt";
String findWord = "is";
System.out.println(getString(fileName));
System.out.println();
System.out.print("Occurences of '"+findWord+"' found: ");
int[] indices = getIndicesOf(fileName, findWord);
for (int i=0; indices!=-1 && i<indices.length; i++)
System.out.print(indices[i]+" "); -
How to print PDF file from java application?
Hi,
I have a java application that needs to print an PDF files. Could any one provide me links to tutorial/sample codes for doing this?http://onesearch.sun.com/search/onesearch/index.jsp?qt=print+pdf+files&subCat=&site=dev&qp=&chooseCat=javaall&col=developer-forums
-
Printing problem with ads on as java when printing large files
hi all
we have an wd for java application running on as java 7.0 sp18 and adobe document service if we print
small files everything works fine with large files it fails with the following error (after arround 2 minutes)
any ideas
#1.5^H#869A6C5E590200710000092C000B20D000046E16922042E2#1246943126766#com.sap.engine.services.servlets_js
p.server.HttpHandlerImpl#sap.com/tcwddispwda#com.sap.engine.services.servlets_jsp.server.HttpHandlerImp
l#KRATHHO#8929##sabad19023_CHP_5307351#KRATHHO#63a60b106ab311de9cb4869a6c5e5902#SAPEngine_Application_Thr
ead[impl:3]_15##0#0#Error#1#/System/Server/WebRequests#Plain###application [webdynpro/dispatcher] Process
ing HTTP request to servlet [dispatcher] finished with error.^M
The error is: com.sap.tc.webdynpro.clientserver.adobe.pdfdocument.base.core.PDFDocumentRuntimeException:
Failed to UPDATEDATAINPDF^M
Exception id: [869A6C5E590200710000092A000B20D000046E1692201472]#Hello
on which support package level is the java stack ?
kr,
andreas -
How to convert a file from pdf to txt in java
hi,
in my project ,I want to convert a file from pdf to txt using java api. If anyone
have any idea about it plz help me.Google has several ideas. :)
-
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 advanceMike,
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 -
Send a File *.txt to a Printer since Store Procedures
Hello everybody.
We have a problem.. because we need to send a File *.txt to a Printer since a Store Procedure.
Do you know how can we do in order to solve this problem.
Please send me an email ([email protected]) if you have an idea about it.
Thanks for all
Luis CastroLuis,
Your going to have to give ALLOT more information about what your wanting, Don't worry if your english is ... short, but please give as much detail about what your wanting as I and (probably everyone else) don't understand what your wanting.
Tyler -
How to print *.PRN file without print Dialog from Java
hi friends
I need to print *.PRN file from java code.*
I need to print directely without open printer dialog box when i select .PRN file then it should able to direct print to default printer.
Here is the Dos command to directely print the PRN file
COPY C:\\PrintFile.prn /B \\java103\\HPLaserJ
(source: http://filext.com/faq/print_from_prn_file.php)
i try to run that dos command through Process p = Runtime.getRuntime().exec("COPY C:\\PrintFile.prn /B \\java103\\HPLaserJ");
But is show that
Here is the LOG of ERROR
\\java103\HP LaserJet M1120 MFP
java.io.IOException: Cannot run program "COPY": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Print2Printer.GetDefaultPrinter.main(GetDefaultPrinter.java:17)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
Here is the Source
import java.io.IOException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
public class GetDefaultPrinter {
public static void main(String args[]){
PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(printer.getName());
try {
Process p = Runtime.getRuntime().exec("COPY C:\\PrintFile.prn /B \\java103\\HPLaserJ");
System.out.println(p);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();you need to specify which program you want to execute in Runtime.getRuntime().exec(). So try this one
try {
Process p = Runtime.getRuntime().exec("cmd.exe COPY C:\\PrintFile.prn /B \\java103\\HPLaserJ");
System.out.println(p);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} -
How to print a text file contents using java.
Using Input and Output streams I can add and retrive the contents to/from text file. But how to send this file contents to the printer or how to print the file.
Example from my code:
private void printErrorReport()
PrintJob pjob = getToolkit().getPrintJob(m_frame,null,null);
if (pjob != null) {
Graphics pg = pjob.getGraphics();
if (pg != null) {
String s = detailsArea.getText();
printLongString (pjob, pg, s);
pg.dispose();
pjob.end();
// Utility method needed
void printLongString (PrintJob pjob, Graphics pg, String s) {
int pageNum = 1;
int linesForThisPage = 0;
int linesForThisJob = 0;
// Note: String is immutable so won't change while printing.
if (!(pg instanceof PrintGraphics)) {
throw new IllegalArgumentException ("Graphics context not PrintGraphics");
StringReader sr = new StringReader (s);
LineNumberReader lnr = new LineNumberReader (sr);
String nextLine = " ";
int pageHeight = pjob.getPageDimension().height;
pageHeight -= 20;
Font helv = new Font("Arial", Font.PLAIN, 12);
//have to set the font to get any output
pg.setFont (helv);
FontMetrics fm = pg.getFontMetrics(helv);
int fontHeight = fm.getHeight();
int fontDescent = fm.getDescent();
int curHeight = 0;
try {
do {
nextLine = lnr.readLine();
if (nextLine != null) {
if ((curHeight + fontHeight) > pageHeight) {
// New Page
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
pageNum++;
linesForThisPage = 0;
pg.dispose();
pg = pjob.getGraphics();
if (pg != null) {
pg.setFont (helv);
curHeight = 0;
curHeight += fontHeight;
if (pg != null) {
pg.drawString (nextLine, 0, curHeight - fontDescent);
linesForThisPage++;
linesForThisJob++;
} else {
System.out.println ("pg null");
} while (nextLine != null);
} catch (EOFException eof) {
// Fine, ignore
} catch (Throwable t) { // Anything else
t.printStackTrace();
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
System.out.println ("pages printed: " + pageNum);
System.out.println ("total lines printed: " + linesForThisJob);
} -
I want to print .ps file from java
Hello
i want to print .ps file using javax.print api and this give following error
sun.print.PrintJobFlavorException: invalid flavor
and my code is as below
public PrintPS() {
/* Construct the print request specification.
* The print data is Postscript which will be
* supplied as a stream. The media size
* required is A4, and 2 copies are to be printed
//DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(2));
aset.add(Sides.TWO_SIDED_LONG_EDGE);
aset.add(Finishings.STAPLE);
System.out.println("HI");
/* locate a print service that can handle it */
PrintService pservices =
PrintServiceLookup.lookupDefaultPrintService();
System.out.println("Hello "+pservices);
DocFlavor[] flavors = pservices.getSupportedDocFlavors();
for (int i = 0; i < flavors.length; i++) {
System.out.println(flavors.getRepresentationClassName());
/* if (pservices.length > 0) {*/
System.out.println("selected printer " + pservices.getName());
/* create a print job for the chosen service */
DocPrintJob pj = pservices.createPrintJob();
try {
* Create a Doc object to hold the print data.
* Since the data is postscript located in a disk file,
* an input stream needs to be obtained
* BasicDoc is a useful implementation that will if requested
* close the stream when printing is completed.
FileInputStream fis = new FileInputStream("hexagon.ps");
Doc doc = new SimpleDoc(fis, flavor, null);
/* print the doc as specified */
pj.print(doc, aset);
* Do not explicitly call System.exit() when print returns.
* Printing can be asynchronous so may be executing in a
* separate thread.
* If you want to explicitly exit the VM, use a print job
* listener to be notified when it is safe to do so.
} catch (IOException ie) {
System.err.println(ie);
} catch (PrintException e) {
System.err.println(e);
plz help me1. Turn the router, iPad and printer off.
2. Turn on the router and then wait 30 seconds to let it complete its start-up process.
3. Turn on printer and then wait 30 seconds to let it complete its start-up process.
4. Turn on your iPad and test print. -
How to print a word document using java?
Hi All,
is it possible to write a java program that takes input and directs it to printer.
I specifically want it for word document.
i already have a program that works for images but not for word document.
Plz guide me where i am wrong or help me with some links or code snippets.
Any kind of help will be really appreciated.
// program is below
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
public class Convert{
* @param args
Convert(){
DocFlavor psFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
FileInputStream fis = null;
// Creates a new attribute set
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
try{
fis = new FileInputStream("d:\\print\\new.txt");
System.out.println("doc taken...");
if (fis == null) {
System.out.println("No File");
return;
}catch(Exception e){
System.out.println("No Such Doc..."+e);
PrintService printService[] = PrintServiceLookup.lookupPrintServices(psFlavor, aset);
//System.out.println("Default Printer: ");
PrintService services = null;
try{
services = PrintServiceLookup.lookupDefaultPrintService();//ookupPrintServices(psFlavor, aset);
}catch(Exception e){
System.out.println("print exp ");
e.printStackTrace();
System.out.println("Default Printer: "+ services.getName());
System.out.println(services+" services taken..."+services.isDocFlavorSupported(psFlavor));
PrintService service = ServiceUI.printDialog(null, 250, 250, printService, services, psFlavor, aset);
if (service != null) {
DocPrintJob job = service.createPrintJob();
try {
DocAttributeSet d = new HashDocAttributeSet();
// Doc doc=new
Doc doc = new SimpleDoc(fis, psFlavor, d);
System.out.print("doc prepared...");
System.out.println("printing starts...");
job.print(doc,aset);
System.out.println("printing done...");
catch (Throwable pe) {
pe.printStackTrace();
}else{
System.out.print("else...");
public static void main(String[] args) {
// TODO Auto-generated method stub
new Convert();
}anamupota wrote:
JoachimSauer wrote:
Desktop.print() is the simplest way. Everything else will get tricky really fast.I wonder if one could print .doc file though.Desktop.print() simply tells the OS to do whatever it usually does to print the file. If Word is installed, it should start word with the file as an argument and whatever parameter it takes to tell Word to print it.
If you don't have anything installed that can view/print words files (or the correct entries are missing in the registry), then it won't work, of course. -
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
SumanaHere'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) -
hello, i am trying to use the javax.print package to print a file. The following is my code:
import java.net.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
class t {
public static void main(String args[]) throws Exception{
DocFlavor htmlFlavor = DocFlavor.URL.TEXT_HTML_US_ASCII;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
PrintService[] matchingServices =
PrintServiceLookup.lookupPrintServices(htmlFlavor,aset);
if (matchingServices.length == 0) {
System.out.println("No Service Available");
return;
PrintService service = matchingServices[4];
URL url = new URL("http://www.google.com");
DocPrintJob pjob = service.createPrintJob();
SimpleDoc doc = new SimpleDoc(url,htmlFlavor,null);
try {
pjob.print(doc,aset);
} catch (PrintException e) {
System.out.println(e);
} // end main
when i run the code, it returns me no available printing service. when i change the print service look up arguements to null, i.e.:
PrintService[] matchingServices =
PrintServiceLookup.lookupPrintServices(null,null);
when the program reaches the pjob.print(doc,aset) part, it returns an invalid doc flavor exception
actually, when i change the doc flavor to plain/text, and try to print a .txt file, the same problem occurs.
can anybody tell me what's the problem. thanks a lot!Well, I finally got the solution.
The problem wasn't the class AttributedString but the class LineBreakMeasurer. The printing works fine splitting the text into lines as I thought. I would have preferred another smarter solution, like using the class BreakIterator and LineBreakMeasurer but, it doesn't seem to work properly.
Thanks anyway ;)
Maybe you are looking for
-
Squeaking when my HP LaserJet Pro 400 color MFP M475 feeds from Tray 1
There is a low pitched squeaking when my HP LaserJet Pro 400 color MFP M475 feeds from Tray 1. We use this tray for printing individual envelopes. Is there a part that I should replace or clean? Trays 1 & 3 feed with no noise.
-
I have a macbook pro 15.4 inch silver keys it's a 2007 model, please help me I would like to find one with the APPLE cut out. BUT not necessary Please help me. I've tried ebay and am lost what else to do thank you
-
How to include link with parameter in marketing emails?
Hi, my client wants to send out personalized marketing emails to customers with a link back to the BC site that have a parameter on the end that passes the customer's email address the message was sent to. Then the landing page can parse that email o
-
Dear All We are not able to create new substitution step through T. Code GGB1/OBBH in Dev. 500 / 400. While inserting step, system gives ABAP dump. Same error comes in Dev. 200 also. Please look into this. This has happened due to some updation on 06
-
I am having problems with my Out Box. I have a Yahoo Mail address and I understand that Apple provide the Out Box service. Last week I tried to send an E Mail to my son. Each time I tried to send the message I was asked to prove that I was a human