public Book PreviewBattleforce() {
   if (battleforces.size() == 0) {
     return new Book();
   }
   pages = new Book();
   page.setPaper(paper);
   for (int i = 0; i < battleforces.size(); i++) {
     pages.append(((BattleforceCardPrinter) battleforces.get(i)), page);
   }
   return pages;
 }
  private void GeneratePrints() {
    // reset the book so we don't get extra pages!
    pages = new Book();

    // start building the print objects necessary
    page.setPaper(paper);

    for (int index = 0; index <= Mechs.size() - 1; index++) {
      PrintMech pm = (PrintMech) Mechs.get(index);
      pages.append(pm, page);
      if (Mechs.size() == 1) job.setJobName(pm.CurMech.GetFullName().trim());
    }
  }
  public void PrintBattleforce() {
    if (battleforces.size() == 0) {
      return;
    }
    pages = new Book();
    page.setPaper(paper);

    if (jobName.isEmpty()) {
      jobName = "BattleForce";
    }
    if (battleforces.size() == 1) {
      BattleForce bf = ((BattleforcePrinter) battleforces.get(0)).getBattleforce();
      if (!bf.ForceName.isEmpty()) {
        jobName = bf.ForceName.trim();
      }
    }

    job.setJobName(jobName);

    if (useDialog) {
      for (int i = 0; i < battleforces.size(); i++) {
        BattleforcePrinter bf = (BattleforcePrinter) battleforces.get(i);
        dlgPrintBattleforce pForce =
            new dlgPrintBattleforce((javax.swing.JFrame) Parent, true, bf.getBattleforce());
        pForce.setLocationRelativeTo((javax.swing.JFrame) Parent);
        pForce.setVisible(true);
        if (!pForce.Result) {
          return;
        }

        bf.setType(pForce.Sheet);
      }
    }

    for (int i = 0; i < battleforces.size(); i++) {
      pages.append((BattleforcePrinter) battleforces.get(i), page);
    }

    job.setPageable(pages);
    boolean DoPrint = job.printDialog();
    if (DoPrint) {
      try {
        job.print();
      } catch (PrinterException e) {
        System.err.println(e.getMessage());
        System.out.println(e.getStackTrace());
      }
    }
  }
  // 从预览中点击打印按钮时执行此函数打印
  public void printBinaryPicture() {

    int formatIndex = jComboBox_deviceName.getSelectedIndex();
    int number = Integer.parseInt(getBinaryPrintNum());

    int index = jComboBox_deviceName.getSelectedIndex();
    String deviceName = getDeviceName();
    String deviceType = getDeviceModel();
    String deviceID = getDeviceID(); // MAC
    String manageIP = getManageIP();
    String deviceSN = getDeviceSN();
    // String deviceVresion = getDeviceVersion();
    String power = getPower();
    String officialWebsite = getOfficialWebsite();

    Book book = new Book();
    PageFormat pf = new MyPaperFormat(mainUi);

    // 生成二维码
    productPowerLineQRCode(head, deviceID, deviceSN, manageIP);

    printInfo printinfo = mainUi.new printInfo();

    printinfo.setdeviceID(deviceID);
    printinfo.setManageIP(manageIP);
    printinfo.setDeviceSN(deviceSN);

    printinfo.setdeviceName(deviceName);
    printinfo.setdeviceType(deviceType);
    // printinfo.setDeviceVresion(deviceVresion);
    printinfo.setPower(power);
    printinfo.setOfficialWebsite(officialWebsite);
    printinfo.setCompany(companyInit);
    printinfo.setPowerLineIndex(index);

    myPrint = new MyPrint(printinfo);
    book.append(myPrint, pf);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(book);
    try {
      if (true) {
        job.print();
        mainUi.appendTextareaText(mainUi.jTextArea_status, "\n执行打印");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 5
0
  @Override
  public void print() {

    try {
      File input = new File(absoluteFilePath);
      FileInputStream fis = new FileInputStream(input);
      FileChannel fc = fis.getChannel();
      ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

      PDFFile curFile = null;
      PDFPrintPage pages = null;
      curFile = new PDFFile(bb); // Create PDF Print Page
      pages = new PDFPrintPage(curFile);
      PrinterJob pjob = PrinterJob.getPrinterJob();

      PrintService[] services = PrinterJob.lookupPrintServices();
      for (PrintService ps : services) {
        String pName = ps.getName();
        if (pName.equals(Profile.getInstance().getDefaultPrinterName())) {
          pjob.setPrintService(ps);
          logger.debug("Printing on : " + pName);
          break;
        }
      }

      pjob.setJobName(absoluteFilePath);
      Book book = new Book();
      PageFormat pformat = PrinterJob.getPrinterJob().defaultPage();
      book.append(pages, pformat, curFile.getNumPages());
      pjob.setPageable(book);

      // print
      PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

      // Print it
      pjob.print(aset);
      fis.close();
    } catch (Exception e) {
      logger.error("Cannot print pdf", e);
      Dialog.showThrowable("Erreur", "Impossible d'imprimer le fichier", e);
    }
  }
  private boolean printPages(int firstPageIndex, int lastPageIndex, boolean withPrintDialog)
      throws JRException {
    boolean isOK = true;

    if (firstPageIndex < 0
        || firstPageIndex > lastPageIndex
        || lastPageIndex >= jasperPrint.getPages().size()) {
      throw new JRException(
          "Invalid page index range : "
              + firstPageIndex
              + " - "
              + lastPageIndex
              + " of "
              + jasperPrint.getPages().size());
    }

    pageOffset = firstPageIndex;

    PrinterJob printJob = PrinterJob.getPrinterJob();

    // fix for bug ID 6255588 from Sun bug database
    initPrinterJobFields(printJob);

    PageFormat pageFormat = printJob.defaultPage();
    Paper paper = pageFormat.getPaper();

    printJob.setJobName("JasperReports - " + jasperPrint.getName());

    switch (jasperPrint.getOrientation()) {
      case JRReport.ORIENTATION_LANDSCAPE:
        {
          pageFormat.setOrientation(PageFormat.LANDSCAPE);
          paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
          paper.setImageableArea(0, 0, jasperPrint.getPageHeight(), jasperPrint.getPageWidth());
          break;
        }
      case JRReport.ORIENTATION_PORTRAIT:
      default:
        {
          pageFormat.setOrientation(PageFormat.PORTRAIT);
          paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
          paper.setImageableArea(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
        }
    }

    pageFormat.setPaper(paper);

    Book book = new Book();
    book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1);
    printJob.setPageable(book);
    try {
      if (withPrintDialog) {
        if (printJob.printDialog()) {
          printJob.print();
        } else {
          isOK = false;
        }
      } else {
        printJob.print();
      }
    } catch (Exception ex) {
      throw new JRException("Error printing report.", ex);
    }

    return isOK;
  }
    public void printPage(String size) {
      double width = 0.0;
      double height = 0.0;
      double margin = 0.0;
      PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
      try {
        PrinterJob pj = PrinterJob.getPrinterJob();
        if (pj.printDialog()) {
          PageFormat pf = pj.defaultPage();
          Paper paper = pf.getPaper();
          if (size.equals("3r")) {
            width = 5d * 72d;
            height = 3.5d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.JIS_B7);
          } else if (size.equals("4r")) {
            width = 6d * 72d;
            height = 4d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.JAPANESE_POSTCARD);
          } else if (size.equals("5r")) {
            width = 7d * 72d;
            height = 5d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.NA_5X7);
          } else if (size.equals("6r")) {
            width = 8d * 72d;
            height = 6d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.JAPANESE_DOUBLE_POSTCARD);
          } else if (size.equals("8r")) {
            width = 10d * 72d;
            height = 8d * 72d;
            margin = 0d * 72d;
            aset.add(MediaSizeName.NA_8X10);
          }
          paper.setSize(width, height);
          paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));
          System.out.println("Before- " + dump(paper));
          pf.setOrientation(PageFormat.LANDSCAPE);
          pf.setPaper(paper);
          System.out.println("After- " + dump(paper));
          System.out.println("After- " + dump(pf));
          dump(pf);
          PageFormat validatePage = pj.validatePage(pf);
          System.out.println("Valid- " + dump(validatePage));

          Book pBook = new Book();
          pBook.append(this, pf);
          pj.setPageable(pBook);

          aset.add(OrientationRequested.LANDSCAPE);

          try {
            pj.setPrintable(this);
            pj.print(aset);
          } catch (PrinterException ex) {
            ex.printStackTrace();
          }
        }

      } catch (Exception exp) {
        exp.printStackTrace();
      }
    }
  private void printDocument(InvoicePagePrint pageprintdocumnet) {
    PrintService[] service = PrinterJob.lookupPrintServices(); // list
    // of
    // printers
    int count = service.length;
    PrintService printSvc = null;
    for (int i = 0; i < count; i++) {
      System.out.println("Service name: " + service[i].getName());
      if (service[i].getName().indexOf("PDFcamp") != -1) {
        printSvc = service[i];
        i = count;
      }
    }

    // --- Create a new PrinterJob object and set predefined print service.
    try {

      PrinterJob printJob = PrinterJob.getPrinterJob();
      printJob.setPrintService(printSvc);

      PageFormat pf = printJob.defaultPage();
      // Paper paper = pf.getPaper();
      // paper.setSize(8.5 * 72, 11 * 72);
      // paper.setSize(79, 78);

      // paper.setImageableArea(0.5 * 72, 0.0 * 72, 7.5 * 72, 11.5 * 72);
      // paper.setImageableArea(0.5 , 0.0 , 79, 78);
      // pf.setPaper(paper);

      // --- Create a new book to add pages to
      Book book = new Book();

      // --- Add the cover page using the default page format for this
      // print
      // job
      // book.append(pageprintdocumnet, printJob.defaultPage());
      book.append(pageprintdocumnet, pf);

      // Paper pg = new Paper();
      // pg.setSize(1000, 900);
      // book.append(pageprintdocumnet, printJob..defaultPage();

      // --- Add the document page using a landscape page format
      /*
       * PageFormat documentPageFormat = new PageFormat();
       * documentPageFormat.setOrientation(PageFormat.LANDSCAPE); book.append(new Document(),
       * documentPageFormat);
       */
      // --- Add a third page using the same painter

      // --- Tell the printJob to use the book as the pageable object
      printJob.setPageable(book);

      // --- Show the print dialog box. If the user click the
      // --- print button we then proceed to print else we cancel
      // --- the process.
      // if (printJob.printDialog()) {

      printJob.print();
    } catch (Exception PrintException) {
      PrintException.printStackTrace();
    }
  }