首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Java应用程序与Posiflex打印机的jPOS连接

通过Java应用程序与Posiflex打印机的jPOS连接
EN

Stack Overflow用户
提问于 2017-11-11 07:00:27
回答 1查看 199关注 0票数 0

我想将Posiflex打印机集成到我的应用程序中,其中逻辑名为"PP Demo“,设备模型为PP-8900。

打印机连接到我的系统上,可以用Posiflex软件打印。如何将它与我的应用程序集成?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-14 05:43:05

我只需要将所需的dll文件粘贴到我的java库中--就是这样,代码中提到了jpos.xml文件.

代码语言:javascript
复制
package practice;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;


public class PrintableComponent {

    public static void main(String[] args) {

        PrintableComponent example3 = new PrintableComponent();
        System.exit(0);
    }

    // --- Private instances declarations
    private final static int POINTS_PER_INCH = 72;

    /**
     * Constructor: Example3
     * <p>
     * 
     */
    public PrintableComponent() {

        // --- Create a new PrinterJob object
        PrinterJob printJob = PrinterJob.getPrinterJob();

        // --- 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(new IntroPage(), 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
        book.append(new Document(), documentPageFormat);

        // --- 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()) {
            try {
                printJob.print();
            } catch (Exception PrintException) {
                PrintException.printStackTrace();
            }
        }

    }

    /**
     * Class: IntroPage
     * <p>
     * 
     * This class defines the painter for the cover page by implementing the
     * Printable interface.
     * <p>
     * 
     * @author Jean-Pierre Dube <jpdube@videotron.ca>
     * @version 1.0
     * @since 1.0
     * @see Printable
     */
    private class IntroPage implements Printable {

        /**
         * Method: print
         * <p>
         * 
         * @param g
         *            a value of type Graphics
         * @param pageFormat
         *            a value of type PageFormat
         * @param page
         *            a value of type int
         * @return a value of type int
         */
        public int print(Graphics g, PageFormat pageFormat, int page) {

            // --- Create the Graphics2D object
            Graphics2D g2d = (Graphics2D) g;

            // --- Translate the origin to 0,0 for the top left corner
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            // --- Set the default drawing color to black
            g2d.setPaint(Color.black);

            // --- Draw a border arround the page
            Rectangle2D.Double border = new Rectangle2D.Double(0, 0, pageFormat.getImageableWidth(),
                    pageFormat.getImageableHeight());
            g2d.draw(border);

            // --- Print the title
            String titleText = "Printing in Java Part 2";
            Font titleFont = new Font("helvetica", Font.BOLD, 36);
            g2d.setFont(titleFont);

            // --- Compute the horizontal center of the page
            FontMetrics fontMetrics = g2d.getFontMetrics();
            double titleX = (pageFormat.getImageableWidth() / 2) - (fontMetrics.stringWidth(titleText) / 2);
            double titleY = 3 * POINTS_PER_INCH;
            g2d.drawString(titleText, (int) titleX, (int) titleY);

            return (PAGE_EXISTS);
        }
    }

    /**
     * Class: Document
     * <p>
     * 
     * This class is the painter for the document content.
     * <p>
     * 
     * 
     * @author Jean-Pierre Dube <jpdube@videotron.ca>
     * @version 1.0
     * @since 1.0
     * @see Printable
     */
    private class Document implements Printable {

        /**
         * Method: print
         * <p>
         * 
         * @param g
         *            a value of type Graphics
         * @param pageFormat
         *            a value of type PageFormat
         * @param page
         *            a value of type int
         * @return a value of type int
         */
        public int print(Graphics g, PageFormat pageFormat, int page) {

            // --- Create the Graphics2D object
            Graphics2D g2d = (Graphics2D) g;

            // --- Translate the origin to 0,0 for the top left corner
            g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

            // --- Set the drawing color to black
            g2d.setPaint(Color.black);

            // --- Draw a border arround the page using a 12 point border
            g2d.setStroke(new BasicStroke(12));
            Rectangle2D.Double border = new Rectangle2D.Double(0, 0, pageFormat.getImageableWidth(),
                    pageFormat.getImageableHeight());

            g2d.draw(border);

            // --- Print page 1
            if (page == 1) {
                // --- Print the text one inch from the top and laft margins
                g2d.drawString("This the content page of page: " + page, POINTS_PER_INCH, POINTS_PER_INCH);
                return (PAGE_EXISTS);
            }

            // --- Print page 2
            else if (page == 2) {
                // --- Print the text one inch from the top and laft margins
                g2d.drawString("This the content of the second page: " + page, POINTS_PER_INCH, POINTS_PER_INCH);
                return (PAGE_EXISTS);
            }

            // --- Validate the page
            return (NO_SUCH_PAGE);

        }
    }

} // Example3
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47235295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档