【问题标题】:Weird symbols where printing PDF打印 PDF 时出现奇怪的符号
【发布时间】:2019-03-14 18:08:46
【问题描述】:

我想在网络打印机上打印 PDF 文件。打印txt文件或String没有问题,但是当我尝试打印PDF/JEPG/DOC文件时出现问题。

打印机:三星 SCX-6545 系列 PCL

public static void printFile(String filename) throws IOException {

    FileInputStream psStream = null;
    try {
        psStream = new FileInputStream(filename);
    } catch (FileNotFoundException ffne) {
    }

    if (psStream == null) {
        return;
    }
    int count = 0;

    DocFlavor psInFormat = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(1));
    // aset.add(MediaSize.ISO_A4);
    // aset.add(Sides.DUPLEX);
    PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, null);
    for (int i = 0; i < services.length; i++) {
        if (services[i].getName().contains("SCX")) {
            count = i;
        }
    }
    DocFlavor[] docFalvor = services[count].getSupportedDocFlavors();
    for (int i = 0; i < docFalvor.length; i++) {
        System.out.println(docFalvor[i].getMimeType());
    }
    if (services.length > 0) {
        System.out.println(Files.probeContentType(Paths.get(filename)));
        DocPrintJob job = services[count].createPrintJob();
        try {
            job.print(myDoc, aset);
        } catch (PrintException pe) {
            System.out.print(pe);

        }
    }
    psStream.close();
}

public static void main(String[] args) throws IOException {
    printFile("C:/Users/artur.pakula/Documents/aaa.pdf");
}

结果:

【问题讨论】:

  • 你在哪里声明了变量“count”?
  • 使用DocFlavor.URL.AUTOSENSE 可能会更好。代替 FileInputStream,使用Paths.get(filename).toUri().toURL() 获取一个对象,作为第一个参数传递给 SimpleDoc 构造函数。

标签: java printing java-print


【解决方案1】:

我解决了这个问题。

代码:

 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdmodel.PDDocument;
 public static void printFile(String filename) throws IOException {

    FileInputStream psStream = null;
    try {
        psStream = new FileInputStream(filename);
    } catch (FileNotFoundException ffne) {
    }

    if (psStream == null) {
        return;
    }
    int count = 0;
    DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;

    Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(1));
    // aset.add(new Copies(5));
    // aset.add(MediaSize.ISO_A4);
    // aset.add(Sides.DUPLEX);
    PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, null);
    for (int i = 0; i < services.length; i++) {
        if (services[i].getName().contains("ITE_LUB_MONO")) {
            count = i;
        }
    }
    DocFlavor[] docFalvor = services[count].getSupportedDocFlavors();
    for (int i = 0; i < docFalvor.length; i++) {
        System.out.println(docFalvor[i].getMimeType());
    }
    if (services.length > 0) {
        System.out.println(Files.probeContentType(Paths.get(filename)));
        System.out.println(services[count].getName());
        DocPrintJob job = services[count].createPrintJob();
        try {
            // właściwy kod
            PDFParser parser = new PDFParser(psStream);
            PrinterJob job1 = PrinterJob.getPrinterJob();
            job1.setPrintService(services[count]);
            PDDocument doc = PDDocument.load(filename);
            doc.silentPrint(job1);
            // właściwy kod koniec

        } catch (PrinterException pe) {
            System.out.print(pe);

        }
    }
    psStream.close();
}

public static void main(String[] args) throws IOException {
    printFile("C:/Users/jakub.wojtczak/Desktop/aaa.pdf");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    相关资源
    最近更新 更多