java link printer print file

Get the name of the printer:

// 获取打印机
public static List<String> findPrints() {
    List<String> list = new ArrayList<>();
    HashPrintRequestAttributeSet requestAttributeSet = new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    //查找所有的可用的打印服务
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, requestAttributeSet);
    if (printService == null || printService.length == 0) {
        log.info("打印获取失败,未找到可用打印机,请检查。");
    }
    if (printService != null) {
        for (PrintService print : printService) {
            list.add(print.getName());
        }
    }
    return list;
}


PFD file printing

  <!--打印-->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.6</version>
        </dependency>

code:

Need to get the name of the printer

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;

import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Sides;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;

public class DYController {
    public static void main(String[] args) throws Exception {
        
        String pdfFile = "E:\\a.pdf";//文件路径
        File file = new File(pdfFile);
        String printerName = "HP MFP M436 PCL6";//打印机名包含字串
        PDFprint(file,printerName);
    }

    public static void PDFprint(File file , String printerName) throws Exception {
        PDDocument document = null;
        try {
            document = PDDocument.load(file);
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setJobName(file.getName());
            if (printerName != null) {
                // 查找并设置打印机
                //获得本台电脑连接的所有打印机
                PrintService[] printServices = PrinterJob.lookupPrintServices();
                if(printServices == null || printServices.length == 0) {
                    System.out.print("打印失败,未找到可用打印机,请检查。");
                    return ;
                }
                PrintService printService = null;
                //匹配指定打印机
                for (int i = 0;i < printServices.length; i++) {
                    System.out.println(printServices[i].getName());
                    if (printServices[i].getName().contains(printerName)) {
                        printService = printServices[i];
                        break;
                    }
                }
                if(printService!=null){
                    printJob.setPrintService(printService);
                }else{
                    System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
                    return ;
                }
            }
            //设置纸张及缩放
            PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
            //设置多页打印
            Book book = new Book();
            PageFormat pageFormat = new PageFormat();
            //设置打印方向
            pageFormat.setOrientation(PageFormat.PORTRAIT);//纵向
            pageFormat.setPaper(getPaper());//设置纸张
            book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
            printJob.setPageable(book);
            printJob.setCopies(1);//设置打印份数
            //添加打印属性
            HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
            pars.add(Sides.DUPLEX); //设置单双页
            printJob.print(pars);
        }finally {
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Paper getPaper() {
        Paper paper = new Paper();
        // 默认为A4纸张,对应像素宽和高分别为 595, 842
        int width = 595;
        int height = 842;
        // 设置边距,单位是像素,10mm边距,对应 28px
        int marginLeft = 10;
        int marginRight = 0;
        int marginTop = 10;
        int marginBottom = 0;
        paper.setSize(width, height);
        // 下面一行代码,解决了打印内容为空的问题
        paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
        return paper;
    }

}

convert excel to pdf 

To generate excel, please see the link below:

java export Excel_Mr_Jin.'s blog-CSDN blog_java export excel method https://blog.csdn.net/Java_Mr_Jin/article/details/124076055?spm=1001.2014.3001.5501

Of course, if we want to print our excel, we can convert it to pdf, and then use the above to print:

For word conversion to pdf, jaboc is needed, so you need to download the jaboc.jar package first. The link and extraction code are as follows:

Link: Link: https://pan.baidu.com/s/17nLuDAYC33Yu8KvwS2J2gA 

Extraction code: qwer 

After decompressing the jar package

1. Copy the Jacob.jar package to the lib folder of the jdk of the program

2. Copy the dll file to the bin directory of jdk 

 Then introduce the jacob.jar package in the idea

 


		<dependency>
			<groupId>com.hynnet</groupId>
			<artifactId>jacob</artifactId>
			<version>1.18</version>
		</dependency>

 Convert excel to pdf in java:

package com.sangeng.controller;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;

public class DYPdfController {
    //word转化pdf,传入转换前的文件路径(例:"E:\\a.docx")和转换后的文件路径(例:"E:\\a.pdf")

    public static void main(String[] args) {
        String sFilePath = "D:\\java-demo\\zzzzz\\qqq.doc";//文件路径
        String toFilePath = "D:\\java-demo\\zzzzz\\qqq.pdf";
        wordToPDF(sFilePath,toFilePath);
    }

    public static void wordToPDF(String sFilePath,String toFilePath) {

        System.out.println("启动 Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", sFilePath).toDispatch();

            System.out.println("打开文档:" + sFilePath);
            System.out.println("转换文档到 PDF:" + toFilePath);

            File tofile = new File(toFilePath);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", toFilePath,17);//17是pdf格式 // FileName
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时:" + (end - start) + "ms.");
        } catch (Exception e) {
            System.out.println("========Error:文档转换失败:" + e.getMessage());
        } finally {
            Dispatch.call(doc, "Close", false);
            System.out.println("关闭文档");
            if (app != null)
                app.invoke("Quit", new Variant[]{});
        }
        // 如果没有这句话,winword.exe进程将不会关闭
        ComThread.Release();
    }
}

 Of course the conversion is done after printing. Remember to delete the generated pdf file:

                File file=new File(toFilePath);
                if(file.exists()&&file.isFile())
                    file.delete();

Print directly to excel

Disadvantage: It is impossible to change the printing parameters

package com.sangeng.controller;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordController {
    public static void main(String[] args) {

        String filePath = "E:\\a.docx";//文件路径
        String printerName = "HP MFP M436 PCL6";//打印机名包含字串
        printWord(filePath,printerName);

    }

    public static void printWord(String filePath, String printerName){
//        初始化线程
        ComThread.InitSTA();
        ActiveXComponent word = new ActiveXComponent("Word.Application");
        //设置打印机名称
        word.setProperty("ActivePrinter", new Variant(printerName));
        // 这里Visible是控制文档打开后是可见还是不可见,若是静默打印,那么第三个参数就设为false就好了
        Dispatch.put(word, "Visible", new Variant(false));
        // 获取文档属性
        Dispatch document = word.getProperty("Documents").toDispatch();
        // 打开激活文挡
        Dispatch doc=Dispatch.call(document, "Open", filePath).toDispatch();
        //Dispatch doc = Dispatch.invoke(document, "Open", Dispatch.Method,
        //  new Object[] { filePath }, new int[1]).toDispatch();
        try{
            Dispatch.callN(doc, "PrintOut");
            System.out.println("打印成功!");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("打印失败");
        }finally {
            try {
                if (doc != null) {
                    Dispatch.call(doc, "Close", new Variant(0));//word文档关闭
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
            //退出
            word.invoke("Quit", new Variant[0]);
            //释放资源
            ComThread.Release();
            ComThread.quitMainSTA();
        }
    }
}

Importing the C drive fails 

Files cannot be directly imported into the c drive because of permission issues. So first create a folder and then import it

 File dir=new File("C:\\nmgbzj");
        File file2=new File(dir.getPath()+"/");
        if(!dir.exists()){
            dir.mkdir();
            file2.createNewFile();
        }else{
            if(file2.exists()){
                file2.delete();
                file2.createNewFile();
            }else{
                file2.createNewFile();
            }

        }

Guess you like

Origin blog.csdn.net/Java_Mr_Jin/article/details/125761082