【问题标题】:How to convert a PDF to a postscript file using pdfbox 2.0如何使用 pdfbox 2.0 将 PDF 转换为 postscript 文件
【发布时间】:2016-07-14 17:14:46
【问题描述】:

我能够使用 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 文件?

谢谢

【问题讨论】:

  • 有一个PDFPageable。
  • 如果您找到问题的答案,将其作为答案发布会很有帮助。这样,其他人就可以从您的发现中学习。
  • 谢谢。我最终做的是使用 PDFBox 读回 PDF 并使用 PDFBox 从 PDF 读取的 x/y 坐标自己写出 post 脚本命令。我不想自学发布脚本命令,但这是我发现可以创建大小合理的发布脚本文件的唯一方法。 PDPageable 类将每个 PDF 页面写为图像,因此后脚本文件比我需要的大很多倍。

标签: pdf pdfbox postscript


【解决方案1】:

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

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

请参阅另一个 SO 问题Printing to PostScript with PDFBox produces a massive file, why?,以获取显示 PDFPrintable 正在使用中的代码 sn-p。我对其进行了一些简化,并将其包含在下面。你是不是觉得很眼熟? :-)

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();

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 2018-03-22
    • 1970-01-01
    • 2011-03-30
    • 2016-11-07
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多