如何使用pdfbox 2.0将PDF转换为postscript文件

时间:2016-07-14 17:14:46

标签: pdf pdfbox postscript

我能够使用PDFBox(版本1.8.9)创建PDF,然后使用以下代码将其转换为PostScript文件:

    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    StreamPrintServiceFactory[] factories =
            StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
                    DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());
    if (factories.length == 0) {
        throw new PrinterException("No PostScript factories available");
    }
    PDDocument document = pdfGenerator.getDocument();

    // Attributes are specified by https://docs.oracle.com/javase/7/docs/api/
    // see package javax.print.attribute.standard
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.NA_LETTER);
    aset.add(new PageRanges(1, document.getNumberOfPages()));

    FileOutputStream fos = new FileOutputStream(filePathAndName);
    factories[0].getPrintService(fos).createPrintJob().print(
            new SimpleDoc(new PDPageable(document), flavor, null), aset);
    fos.close();
    document.close();

PDPageable对象似乎不在PDFBox 2.0代码中,我没有在迁移文档中看到它。如何使用PDFBox 2.0将PDF文件转换为PostScript文件?

谢谢

1 个答案:

答案 0 :(得分:0)

你是对的,在PDFBox version 1.18.12 Package org.apache.pdfbox.pdmodel有一个PDPageable类,但相应的version 2.0.3 Package org.apache.pdfbox.pdmodel没有。

但您想要做的是转换为PostScript语言文档。我认为PDFPrintable会为你做到这一点。

请参阅此另一个问题Printing to PostScript with PDFBox produces a massive file, why?,查看显示正在使用的PDFPrintable的代码段。我已将其简化了一下并将其包含在下面。你看起来很熟悉吗? : - )

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.NA_LETTER);

FileOutputStream fos = new FileOutputStream(filePathAndName);
StreamPrintService sps = factories[0].getPrintService(fos);
        DocPrintJob dpj = sps.createPrintJob();
        SimpleDoc sd = new SimpleDoc(new PDFPrintable(document, Scaling.ACTUAL_SIZE, false), flavor, null);
        factories[0].getPrintService(fos).createPrintJob().print(
                new SimpleDoc(new PDFPrintable(document, Scaling.ACTUAL_SIZE, false), flavor, daset), aset);
fos.close();
document.close();