From: <doc...@us...> - 2007-09-26 00:02:33
|
Revision: 178 http://openpcl.svn.sourceforge.net/openpcl/?rev=178&view=rev Author: documentsystems Date: 2007-09-25 17:02:33 -0700 (Tue, 25 Sep 2007) Log Message: ----------- Howard Hoagland. Changed to use Jim Gabriel's new PCLPrintJob class, which meant to remove the PCL Direct printing and Windows Printing code in PosPrintPages, but keep the building of all the PCL bytes, so that the whole print operation is handled in PCLPrintJob with it's API being the initializePrintJob parameters. Implemented print duplex for Windows Print. Fixed bug when printing two pages, and doing Letter first then Legal, the two pages wouldn't reorder. Modified Paths: -------------- openpcl/src/com/openpcl/viewer/printing/PosPrintPages.java Modified: openpcl/src/com/openpcl/viewer/printing/PosPrintPages.java =================================================================== --- openpcl/src/com/openpcl/viewer/printing/PosPrintPages.java 2007-09-25 23:57:34 UTC (rev 177) +++ openpcl/src/com/openpcl/viewer/printing/PosPrintPages.java 2007-09-26 00:02:33 UTC (rev 178) @@ -7,17 +7,12 @@ import java.awt.Frame; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Locale; -import javax.print.DocFlavor; -import javax.print.DocPrintJob; -import javax.print.PrintException; import javax.print.PrintService; -import javax.print.SimpleDoc; import javax.print.attribute.DocAttributeSet; import javax.print.attribute.HashDocAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; @@ -50,9 +45,8 @@ private PosView mPosView = null; private Frame mParentFrame = null; - private PosPrintBufferedImage mPosPrintBufferedImage = null; - private PosPrintPageableInterface mPosPrintPageableInterface = null; - private PrinterJob mPrinterJob = null; + private PrinterJob mPrinterJob = null; // This is the SUN JRE java.awt.print.PrinterJob + private PCLPrintJob mPCLPrintJob = null; // This is our own com.openpcl.viewer.printing.PCLPrintJob in this project private PrintRequestAttributeSet mPrintRequestAttributeSet = null; private PrintService mPrintService = null; private PosPrintSetupDialog mPosPrintSetupDialog = null; @@ -69,6 +63,7 @@ public static final int sPageOrientationLandscape = 2; private int mProcessPrintType = 0; private int mNumberOfPagesThatWillPrint = 0; + private int mNumberBlankBackPages = 0; private static final String[] mTitleBarTextForPrintDialog = { "Print All", "Print Selected", "Print Changed", "Print Remaining"}; private PriModifyPclBytes mPriModifyPclBytes = null; @@ -88,13 +83,12 @@ public PosPrintPages(PosView pPosView) { super(); mPosView = pPosView; + mPCLPrintJob = mPosView.getOpenPCLViewer().getPCLPrintJob(); mParentFrame = mPosView.getOpenPCLViewer().getAppFrame(); mPrintRequestAttributeSet = mPosView.getOpenPCLViewer().getPrintRequestAttributeSet(); mPosPrintSetupDialogChoices = mPosView.getOpenPCLViewer().getPosPrintSetupDialogChoices(); mDocAttributeSet = new HashDocAttributeSet(); mDocAttributeSet.add(new DocumentName(mPosView.getOpenPCLViewer().getAppName() + " print", Locale.getDefault())); - mPosPrintBufferedImage = new PosPrintBufferedImage(mPosView); - mPosPrintPageableInterface = new PosPrintPageableInterface(mPosView); mPriModifyPclBytes = new PriModifyPclBytes(); mPrintingProgressBar = new JProgressBar(); mPrintingProgressBar.setStringPainted(true); @@ -112,7 +106,6 @@ mPrintingProgressJDialog.setLayout(new BorderLayout()); mPrintingProgressJDialog.add(tJPanel, BorderLayout.CENTER); mPrintingProgressJDialog.pack(); - mPosPrintBufferedImage.setPrintingProgressBar(mPrintingProgressBar); } /** @@ -319,14 +312,9 @@ // Call the print of the all/selected/remaining/changed pages "N" times depending on Additional Copies field for (int tNumCopies = 1; tNumCopies <= mNumberOfCopies; tNumCopies++) { - // Call "Use Windows Print" or "Print PCL Direct" - if (mPosPrintSetupDialogChoices.isPrintFormatWindowsPrint()) { - mPrintedOkReturn = imagePrint(mPagesToPrintArrayList, mPosPrintSetupDialogChoices, - mPrintingProgressBar, mPrintRequestAttributeSet); - } else { - mPrintedOkReturn = printPclDirect(mPagesToPrintArrayList, mPosPrintSetupDialogChoices, - mPrintingProgressBar, mPrintRequestAttributeSet); - } + // Call to "Print PCL Direct" or to "Windows/Image/GDI Print" + mPrintedOkReturn = printPclDirectOrWindowsPrint(mPagesToPrintArrayList, mPosPrintSetupDialogChoices, + mPrintingProgressBar); } // end "for (int tNumCopies = 1; tNumCopies <= mNumberOfCopies; tNumCopies++)" @@ -337,9 +325,11 @@ public void finished() { mPosView.getOpenPCLViewer().setIsDrawingForPrinting(false); mPosView.getOpenPCLViewer().renderImageCurrentPageAndZoom(); + int tTotalPagesPrinted = mPrintingProgressBar.getMaximum(); + JOptionPane.showMessageDialog(mPrintingProgressJDialog, mPosView.getOpenPCLViewer().getAppName() + "\nFinished printing " + - mPagesToPrintArrayList.size() + " page" + (mPagesToPrintArrayList.size() > 1 ? "s." : "."), + tTotalPagesPrinted + " page" + (tTotalPagesPrinted > 1 ? "s." : "."), "Finished Printing", JOptionPane.INFORMATION_MESSAGE); mPrintingProgressJDialog.setVisible(false); } @@ -362,7 +352,7 @@ } // If there were any Letter size pages that got moved to the "move to bottom" ArrayList - if (tMoveToBottomArrayList.size() > 1) { + if (tMoveToBottomArrayList.size() > 0) { for (PosTreeNode removeThisPosTreeNode : tMoveToBottomArrayList) { // Remove the Letter pages from the passed in ArrayList pPagesToPrintArrayList.remove(removeThisPosTreeNode); @@ -385,7 +375,7 @@ } // If there were any Legal size pages that got moved to the "move to bottom" ArrayList - if (tMoveToBottomArrayList.size() > 1) { + if (tMoveToBottomArrayList.size() > 0) { for (PosTreeNode removeThisPosTreeNode : tMoveToBottomArrayList) { // Remove the Legal pages from the passed in ArrayList pPagesToPrintArrayList.remove(removeThisPosTreeNode); @@ -513,26 +503,20 @@ * @param pPagesToPrintArrayList * @param pPosPrintSetupDialogChoices * @param pPrintingProgressBar - * @param pPrintRequestAttributeSet * @return boolean */ - private boolean printPclDirect(ArrayList<PosTreeNode> pPagesToPrintArrayList, - PosPrintSetupDialogChoices pPosPrintSetupDialogChoices, JProgressBar pPrintingProgressBar, - PrintRequestAttributeSet pPrintRequestAttributeSet) { + private boolean printPclDirectOrWindowsPrint(ArrayList<PosTreeNode> pPagesToPrintArrayList, + PosPrintSetupDialogChoices pPosPrintSetupDialogChoices, JProgressBar pPrintingProgressBar) { - DocFlavor tDocFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE; - SimpleDoc tSimpleDoc = null; - DocPrintJob tDocPrintJob = null; PosTreeNode tPrintThisTreeNode = null; - ByteArrayInputStream tByteArrayInputStream = null; ByteArrayOutputStream tByteArrayOutputStream =null; int tPageNumber = 0; - String tProgressBarBaseString = "Generating page: "; byte[] tPclBytes = null; - byte[] tLetterlSizePaperBytes = new byte[] {sbESC, '&', 'l', '2', 'A'}; - byte[] tLegalSizePaperBytes = new byte[] {sbESC, '&', 'l', '3', 'A'}; - byte[] tTopMargin3LinesBytes = new byte[] {sbESC, '&', 'l', '3', 'E'}; - + final byte[] tLetterSizePaperBytes = new byte[] {sbESC, '&', 'l', '2', 'A'}; + final byte[] tLegalSizePaperBytes = new byte[] {sbESC, '&', 'l', '3', 'A'}; + final byte[] tTopMargin3LinesBytes = new byte[] {sbESC, '&', 'l', '3', 'E'}; + mNumberBlankBackPages = 0; + // return if there's no current pages to print if (pPagesToPrintArrayList == null || pPagesToPrintArrayList.size() < 1) { return false; } @@ -596,25 +580,27 @@ try { if (pPosPrintSetupDialogChoices.isPaperSizePrintAllOnLegal()) { // The user has a check in the Print Setup dialog box for "Print all on Legal paper" - // so FORCE ALL PAGES TO LEGAL SIZE no matter if LTR in the Blockument DSP values + // so FORCE ALL PAGES TO LEGAL SIZE no matter if LTR is in the Blockument DSP values // or if this is a PCL file with a letter size PCL command if ( (mPreviousPagePaperSize == null) || (mPreviousPagePaperSize.equalsIgnoreCase("LTR")) ) { // Only if the previous page was null or Letter, write the Legal size page bytes tByteArrayOutputStream.write(tLegalSizePaperBytes); mCurrentPagePaperSize = "LGL"; } else { - // If the page size didn't change, don't write the page size, but the top margin needs to be set after a form feed + // The top margin needs to be set after the paper size change tByteArrayOutputStream.write(tTopMargin3LinesBytes); } - + } else { + // Here for both "Print all on Letter" and for "Print as in original PCL". Keep original paper size works for both. + // The PCLPrintJob print() needs to see the original page size. If Letter, keep letter. If Legal then scale down to Letter. if (mCurrentPagePaperSize.equalsIgnoreCase("LTR")) { // Letter size if ( (mPreviousPagePaperSize == null) || (mPreviousPagePaperSize.equalsIgnoreCase("LGL")) ) { // Only if the previous page was null or Legal, write the Letter size page bytes - tByteArrayOutputStream.write(tLetterlSizePaperBytes); + tByteArrayOutputStream.write(tLetterSizePaperBytes); } else { - // If the page size didn't change, don't write the page size, but the top margin needs to be set after a form feed + // The top margin needs to be set after the paper size change tByteArrayOutputStream.write(tTopMargin3LinesBytes); } } else { @@ -624,30 +610,33 @@ tByteArrayOutputStream.write(tLegalSizePaperBytes); mCurrentPagePaperSize = "LGL"; } else { - // If the page size didn't change, don't write the page size, but the top margin needs to be set after a form feed + // The top margin needs to be set after the paper size change tByteArrayOutputStream.write(tTopMargin3LinesBytes); } } } } catch (IOException e) { + PriDebug.releaseln("Error. Couldn't write page size PCL bytes to ByteArrayOutputStream.\n" + e); return false; } - + // If there were any PCL bytes to include only at print time (not to be viewed on the screen) then write the bytes here byte[] tPrintTimePclBytes = tPrintThisTreeNode.getPrintTimePclBytes(); if (tPrintTimePclBytes != null) { try { tByteArrayOutputStream.write(tPrintTimePclBytes); } catch (IOException e3) { - PriDebug.releaseln("Error. Couldn't write print time only PCL bytes to printer.\n" + e3); + PriDebug.releaseln("Error. Couldn't write print time only PCL bytes to ByteArrayOutputStream.\n" + e3); + return false; } } try { // Write the current page bytes tByteArrayOutputStream.write(tModifiedPclBytes); - } catch (IOException e) { - PriDebug.releaseln("Error. Couldn't write base page PCL bytes to printer.\n" + e); + } catch (IOException e4) { + PriDebug.releaseln("Error. Couldn't write the page PCL bytes to ByteArrayOutputStream.\n" + e4); + return false; } // Write form feed at the end of each page @@ -655,36 +644,66 @@ // Save the current page size to the previous page size to compare the next page with this page mPreviousPagePaperSize = mCurrentPagePaperSize; - - // Update the progress bar at each page - if (mPrintingProgressBar != null) { - mPrintingProgressBar.setValue(tPageNumber + 1); - mPrintingProgressBar.setString(tProgressBarBaseString + (tPageNumber + 1) + " of " + pPagesToPrintArrayList.size()); - } } // end for (tPageNumber = 0; tPageNumber < pPagesToPrintArrayList.size(); tPageNumber++) // Send a printer reset ESC E at the end of all the pages tByteArrayOutputStream.write(sbESC); tByteArrayOutputStream.write('E'); - // The input stream for the DocPrintJob - if (tByteArrayOutputStream == null || tByteArrayOutputStream.size() < 1) { return false; } - tByteArrayInputStream = new ByteArrayInputStream(tByteArrayOutputStream.toByteArray()); - tSimpleDoc = new SimpleDoc(tByteArrayInputStream, tDocFlavor, mDocAttributeSet); - if (tSimpleDoc == null) { return false; } - - if (mPrintService == null) { return false; } + // Set the PrintModeEnum + PCLPrintJob.PrintModeEnum tPrintModeEnum; + if (mPosPrintSetupDialogChoices.isPrintFormatWindowsPrint()) { + tPrintModeEnum = PCLPrintJob.PrintModeEnum.PrintModeGDI; + } else { + tPrintModeEnum = PCLPrintJob.PrintModeEnum.PrintModePCLDirect; + } + + // Set the OutputPageSizeEnum + PCLPrintJob.OutputPageSizeEnum tOutputPageSizeEnum; + if (mPosPrintSetupDialogChoices.isPaperSizePrintAllOnLegal()) { + tOutputPageSizeEnum = PCLPrintJob.OutputPageSizeEnum.OutputPageSizeAllOnLegal; + } else if (mPosPrintSetupDialogChoices.isPaperSizePrintAllOnLetterShrinksLegal()) { + tOutputPageSizeEnum = PCLPrintJob.OutputPageSizeEnum.OutputPageSizeAllOnLetter; + } else { + tOutputPageSizeEnum = PCLPrintJob.OutputPageSizeEnum.OutputPageSizeAsSpecifiedInPCL; + } - tDocPrintJob = mPrintService.createPrintJob(); - if (tDocPrintJob == null) { return false; } - + // Ajust the total number of pages in the Progress bar by the number of blank back pages when duplexing + int tLogicalPageCount = pPrintingProgressBar.getMaximum(); + int tPhysicalPageCount = tLogicalPageCount + mNumberBlankBackPages; + if (mNumberBlankBackPages > 0) { + pPrintingProgressBar.setMaximum(tPhysicalPageCount); + } + + // Set the print job name + String tPrintJobName = mPosView.getOpenPCLViewer().getAppName() + // Could be name of a subclass plugin app + " " + mPosView.getShortName() + ", " + tPhysicalPageCount + " page" + (tPhysicalPageCount == 1 ? "." : "s."); + + // Set the user selected printer from the custom print options dialog on the mPrintService which is + // passed in the below PCLPrintJob.initializePrintJob() try { - tDocPrintJob.print(tSimpleDoc, pPrintRequestAttributeSet); - } catch (PrintException e) { - PriDebug.error("In PosPrintPages, PrintException trying to print PCL Direct.", e); + mPrinterJob.setPrintService(mPrintService); + } catch (PrinterException ePrintService) { + PriDebug.releaseln("PrinterException PosPrintPages calling mPrinterJob.setPrintService()"); return false; } + // Initialize the PCLPrintJob + mPCLPrintJob.initializePrintJob(tPrintModeEnum, tOutputPageSizeEnum, tPrintJobName, + tByteArrayOutputStream.toByteArray(), mPrinterJob); + + // Set the JProgressBar in the PCLPrintJob + mPCLPrintJob.setPrintingProgressBar(pPrintingProgressBar); + + // Kick off the actual printing on paper + try { + mPrinterJob.print(mPCLPrintJob.getPrintRequestAttributeSet()); + } catch (PrinterException ex) { + // The job did not successfully complete + PriDebug.releaseln("PrinterException in PosPrintPages calling mPrinterJob.print()"); + return false; + } + // The pages printed out return true; } @@ -701,6 +720,7 @@ // Write an extra form feed here, and say the next page is going to print on the back pByteArrayOutputStream.write(sbFF); + mNumberBlankBackPages++; mDuplexModePaperSide = DUPLEX_MODE_NEXTPAGE_ONBACK; } else { @@ -713,59 +733,4 @@ } } - - /** - * Image print (instead of PCL Direct) - * @author Howard 6/1/06. Implemented mult page printing on 8/24/06 - * @param pPagesToPrintArrayList - * @param pPosPrintSetupDialogChoices - * @param pPrintingProgressBar - * @param pPrintRequestAttributeSet - * @return boolean - */ - private boolean imagePrint(ArrayList<PosTreeNode> pPagesToPrintArrayList, - PosPrintSetupDialogChoices pPosPrintSetupDialogChoices, JProgressBar pPrintingProgressBar, - PrintRequestAttributeSet pPrintRequestAttributeSet) { - // Set the pages to print so that when PrinterJob calls Printable.print(), it uses the PosTreeNode values - // that are the Objects in this ArrayList, to do Graphics draw commands from the PCL to render the image - mPosPrintBufferedImage.setPagesToPrint(pPagesToPrintArrayList); - - // Set the Print Setup choices object on the Printable Interface object - mPosPrintBufferedImage.setPosPrintSetupDialogChoices(pPosPrintSetupDialogChoices); - - // The below PosPrintBufferedImage implements the java.awt.print.Printable interface - // which has the interface required method: int print(Graphics g, PageFormat format, int pageNumber) - mPosPrintPageableInterface.setPrintable(mPosPrintBufferedImage); - - // Also pass the print ArrayList to the Pageable interface object for it to return the number of pages - // and more importantly to return the PageFormat for each page that has Letter or Legal for each page - mPosPrintPageableInterface.setPagesToPrint(pPagesToPrintArrayList); - - // Set the Print Setup choices object on the Pageable Interface object - mPosPrintPageableInterface.setPosPrintSetupDialogChoices(pPosPrintSetupDialogChoices); - - // Set the Pageable Interface object on the PrinterJob. - // Note that this Pageable Interface object has the Printable Interface object set in it from above - mPrinterJob.setPageable(mPosPrintPageableInterface); - - try { - // Set the user selected printer on the PrinterJob - mPrinterJob.setPrintService(mPrintService); - } catch (PrinterException e) { - PriDebug.releaseln("PrinterException in PosPrintPages image print, mPrinterJob.setPrintService()"); - return false; - } - - try { - // Print all the pages - mPrinterJob.print(pPrintRequestAttributeSet); - } catch (PrinterException e) { - PriDebug.releaseln("PrinterException in PosPrintPages image print, mPrinterJob.print()"); - return false; - } - - // The page printed out - return true; - } - -} +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |