public static void main(String[] args) { PrinterJob job = PrinterJob.getPrinterJob(); Book bk = new Book(); bk.append(new Printable() { public int print(Graphics g, PageFormat page, int index) { page.setOrientation(PageFormat.LANDSCAPE); g.drawString("Dette er en test", 100, 100); return 0;
/* The constructor initializes the pFormat and PJob variables. */ public DocumentRenderer() { pFormat = new PageFormat(); pJob = PrinterJob.getPrinterJob(); }
/* Method to get the current Document */ public Document getDocument() { if (jeditorPane != null) return jeditorPane.getDocument(); else return null; }
/* Method to get the current choice the width scaling option. */ public boolean getScaleWidthToFit() { return scaleWidthToFit; }
/* pageDialog() displays a page setup dialog. */ public void pageDialog() { pFormat = pJob.pageDialog(pFormat); }
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { double scale = 1.0; Graphics2D graphics2D; View rootView; // I graphics2D = (Graphics2D) graphics; // II jeditorPane.setSize((int) pageFormat.getImageableWidth(),Integer.MAX_VALUE); jeditorPane.validate(); // III rootView = jeditorPane.getUI().getRootView(jeditorPane); // IV if ((scaleWidthToFit) && (jeditorPane.getMinimumSize().getWidth() > pageFormat.getImageableWidth())) { scale = pageFormat.getImageableWidth()/ jeditorPane.getMinimumSize().getWidth(); graphics2D.scale(scale,scale); } // V graphics2D.setClip((int) (pageFormat.getImageableX()/scale), (int) (pageFormat.getImageableY()/scale), (int) (pageFormat.getImageableWidth()/scale), (int) (pageFormat.getImageableHeight()/scale)); // VI if (pageIndex > currentPage) { currentPage = pageIndex; pageStartY += pageEndY; pageEndY = graphics2D.getClipBounds().getHeight(); } // VII graphics2D.translate(graphics2D.getClipBounds().getX(), graphics2D.getClipBounds().getY()); // VIII Rectangle allocation = new Rectangle(0, (int) -pageStartY, (int) (jeditorPane.getMinimumSize().getWidth()), (int) (jeditorPane.getPreferredSize().getHeight())); // X if (printView(graphics2D,allocation,rootView)) { return Printable.PAGE_EXISTS; } else { pageStartY = 0; pageEndY = 0; currentPage = -1; return Printable.NO_SUCH_PAGE; } }
/* print(HTMLDocument) is called to set an HTMLDocument for printing. */ public void print(HTMLDocument htmlDocument) { setDocument(htmlDocument); printDialog(); }
/* print(JEditorPane) prints a Document contained within a JEDitorPane. */ public void print(JEditorPane jedPane) { setDocument(jedPane); printDialog(); }
/* print(PlainDocument) is called to set a PlainDocument for printing. */ public void print(PlainDocument plainDocument) { setDocument(plainDocument); printDialog(); }
/* A protected method, printDialog(), displays the print dialog and initiates printing in response to user input. */ protected void printDialog() { if (pJob.printDialog()) { pJob.setPrintable(this,pFormat); try { pJob.print(); } catch (PrinterException printerException) { pageStartY = 0; pageEndY = 0; currentPage = -1; System.out.println("Error Printing Document"); } } }
if (view.getViewCount() > 0) { for (int i = 0; i < view.getViewCount(); i++) { childAllocation = view.getChildAllocation(i,allocation); if (childAllocation != null) { childView = view.getView(i); if (printView(graphics2D,childAllocation,childView)) { pageExists = true; } } } } else { // I if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) { pageExists = true; // II if ((allocation.getBounds().getHeight() > clipRectangle.getHeight()) && (allocation.intersects(clipRectangle))) { view.paint(graphics2D,allocation); } else { // III if (allocation.getBounds().getY() >= clipRectangle.getY()) { if (allocation.getBounds().getMaxY() <= clipRectangle.getMaxY()) { view.paint(graphics2D,allocation); } else { // IV if (allocation.getBounds().getY() < pageEndY) { pageEndY = allocation.getBounds().getY(); } } } } } } return pageExists; }
/* Method to set the content type the JEditorPane. */ protected void setContentType(String type) { jeditorPane.setContentType(type); }
/* Method to set an HTMLDocument as the Document to print. */ public void setDocument(HTMLDocument htmlDocument) { jeditorPane = new JEditorPane(); setDocument("text/html",htmlDocument); }
public void setDocument(JEditorPane jedPane) { jeditorPane = new JEditorPane(); setDocument(jedPane.getContentType(),jedPane.getDocument()); }
/* Method to set a PlainDocument as the Document to print. */ public void setDocument(PlainDocument plainDocument) { jeditorPane = new JEditorPane(); setDocument("text/plain",plainDocument); }
/* Method to set the content type and document of the JEditorPane. */ protected void setDocument(String type, Document document) { setContentType(type); jeditorPane.setDocument(document); }
/* Method to set the current choice of the width scaling option. */ public void setScaleWidthToFit(boolean scaleWidth) { scaleWidthToFit = scaleWidth; } }
Herefter kan du benytte klassen ved f.eks.
DocumentRenderer DocumentRenderer = new DocumentRenderer(); DocumentRenderer.print(xxxxx);
hej mangoo20000 I forbindelse med min hovedopgave er jeg ved at lave en lille distribuerede system til dette skal jeg lave en funktion der printer en dagrapport ud for en asfaltbelægnings hold. En dagrapport er opdelt i objekter, dvs. at info om medarbejdernes og deres arbejdstid er en klasse, info om formanden og hans arbejdstid er en klasse. Men disse skal kunne printes ud som hel dagrapport for dagens arbejde.
ja men hvad gøre jeg hvis jeg har en sætning på 5 linier jeg vil hav skrevet ud på printeren. for x og y kordinaterne kan så vidt jeg har erfaret kun bruges til den ene og samme linie.
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.