Jump to content
Sign in to follow this  
meiya

Print Dokumen Dalam Java

Recommended Posts

salam semua... ade tak otai2 java dalam ni.. diharapkan dapat membantu... :rolleyes:

aku nak buat satu coding java ni.. die akan get HTML file/code dan kemudian die akan printkan output HMTL ni...

so masalahnye skarang ialah, aku boleh print tp die akan kelaur output HTML code to sendiri...

yang aku nak ialah die akan keluarkan output macam dalam web browser..

aku dah try search kat google, tp setakat ni tak jumpe lagi solution..

kalau ade sape2 terer java..leh la kasi idea kat aku :blush:

coding aku mcm ni :

public class BasicPrint {

public static void main(String[] args) {

try {

// Open the image file

String testData = "C:/new_page_1.html";

InputStream is = new BufferedInputStream(new FileInputStream(testData));

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

// Find the default service

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

System.out.println(service);

// Create the print job

DocPrintJob job = service.createPrintJob();

Doc doc= new SimpleDoc(is, flavor, null);

// Monitor print job events; for the implementation of PrintJobWatcher,

// see e702 Determining When a Print Job Has Finished

PrintJobWatcher pjDone = new PrintJobWatcher(job);

// Print it

job.print(doc, null);

// Wait for the print job to be done

pjDone.waitForDone();

// It is now safe to close the input stream

is.close();

} catch (PrintException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

static class PrintJobWatcher {

// true iff it is safe to close the print job's input stream

boolean done = false;

PrintJobWatcher(DocPrintJob job) {

// Add a listener to the print job

job.addPrintJobListener(new PrintJobAdapter() {

public void printJobCanceled(PrintJobEvent pje) {

allDone();

}

public void printJobCompleted(PrintJobEvent pje) {

allDone();

}

public void printJobFailed(PrintJobEvent pje) {

allDone();

}

public void printJobNoMoreEvents(PrintJobEvent pje) {

allDone();

}

void allDone() {

synchronized (PrintJobWatcher.this) {

done = true;

PrintJobWatcher.this.notify();

}

}

});

}

public synchronized void waitForDone() {

try {

while (!done) {

wait();

}

} catch (InterruptedException e) {

}

}

}

}

output yang aku dapat lak( yang aku nak output die mcm dalam browser tu, bukan html code ni) :

<html>

<body>

<div style="page-break-after:'always';

background-color:#EEEEEE;

width:400;

height:70">

testPrint</div>

ABCDEFGHIJK<p>

&nbsp;</p>

</body>

</html>

thanks in advanced :blush:

Share this post


Link to post
Share on other sites

oo.... kalau pakai JeditorPane ni mmg kena taruk ke url die tu...

kalau nak suruh die build frame dgn HTML code yang kita pastu boleh ke?

lgi satu aku nak terus print je, tanpa display HTML tu...

eniwei, thanks for reply :blush:

Share this post


Link to post
Share on other sites

0. boleh letak 'http://' atau 'file:///' atau HTML kod tu wrap dalam URL constructor (kalau tak silap aku la)

1. tak paham..

2. g: jeditor print

thank for reply...

masalah aku dah settle..aku draw balik gune java graphics.. :blush:

mmg JEditorPane boleh view HTML, tapi kalau aku setkan panel tu as visible-false (sebab mmg tak nak view pun HTML tu)

and convert to Graphics, masa print akan kuar kosong je...

so, tak nak aku gune graphic je... thanks for the idea

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...