【问题标题】:Open Office 1.1.4 Export to PDF with background images using java使用java打开Office 1.1.4导出为带有背景图像的PDF
【发布时间】:2014-07-23 13:29:51
【问题描述】:

我有一个问题需要解决,我们使用 OpenOffice 1.1.4 模板化报表并以编程方式将它们导出为 PDF。 创建模板的团队最近将表格中的标题图像和一些图像更改为背景图像(在它们刚刚插入之前),因为此更改当前程序不会使用图像创建 PDF。我们可以手动从 OpenOffice 导出并包含图像。任何人都可以帮助我进行更改以包含这些背景图像吗?

当前代码:

private void print(XInterface xComponent,
        PrintRequestDTO printReq, File sourceFile,
        Vector<String> pages) throws java.lang.Exception {

    String pageRange;

    // XXX create the PDF via OOo export facility
    com.sun.star.frame.XStorable pdfCreator = (com.sun.star.frame.XStorable) UnoRuntime
            .queryInterface(
                    com.sun.star.frame.XStorable.class,
                    xComponent);

    PropertyValue[] outputOpts = new PropertyValue[2];

    outputOpts[0] = new PropertyValue();
    outputOpts[0].Name = "CompressionMode";
    outputOpts[0].Value = "1"; // XXX Change this perhaps?

    outputOpts[1] = new PropertyValue();
    outputOpts[1].Name = "PageRange";

    if (printReq.getPageRange() == null) {

        pageRange = "1-";

    }
    else {

        if (printReq.getPageRange().length() > 0) {

            pageRange = printReq.getPageRange();

        }
        else {

            pageRange = "1-";

        }

    }

    log.debug("Print Instruction - page range = "
            + pageRange);

    PropertyValue[] filterOpts = new PropertyValue[3];

    filterOpts[0] = new PropertyValue();
    filterOpts[0].Name = "FilterName";
    filterOpts[0].Value = "writer_pdf_Export"; // MS Word 97

    filterOpts[1] = new PropertyValue();
    filterOpts[1].Name = "Overwrite";
    filterOpts[1].Value = new Boolean(true);

    filterOpts[2] = new PropertyValue();
    filterOpts[2].Name = "FilterData";
    filterOpts[2].Value = outputOpts;

    if (pages.size() == 0) { // ie no forced page breaks
        // set page range
        outputOpts[1].Value = pageRange;
        filterOpts[2] = new PropertyValue();
        filterOpts[2].Name = "FilterData";
        filterOpts[2].Value = outputOpts;

        File outputFile = new File(
                sourceFile.getParent(),
                printReq.getOutputFileName()
                        + ".pdf");

        StringBuffer sPDFUrl = new StringBuffer(
                "file:///");
        sPDFUrl.append(outputFile.getCanonicalPath()
                .replace('\\', '/'));

        log.debug("PDF file = " + sPDFUrl.toString());

        if (pdfCreator != null) {

            sleep();
            pdfCreator.storeToURL(sPDFUrl.toString(),
                    filterOpts);

        }
    }
    else if (pages.size() > 1) {
        throw new PrintDocumentException(
                "Only one forced split catered for currently");
    }
    else { // a forced split exists.
        log.debug("Page break found in "
                + (String) pages.firstElement());
        String[] newPageRanges = calculatePageRanges(
                (String) pages.firstElement(), pageRange);

        int rangeCount = newPageRanges.length;
        for (int i = 0; i < rangeCount; i++) {
            outputOpts[1].Value = newPageRanges[i];
            log.debug("page range = " + newPageRanges[i]);
            filterOpts[2] = new PropertyValue();
            filterOpts[2].Name = "FilterData";
            filterOpts[2].Value = outputOpts;
            String fileExtension = (i == 0 && rangeCount > 1) ? "__Summary.pdf"
                    : ".pdf";
            File outputFile = new File(
                    sourceFile.getParent(),
                    printReq.getOutputFileName()
                            + fileExtension);

            StringBuffer sPDFUrl = new StringBuffer(
                    "file:///");
            sPDFUrl.append(outputFile.getCanonicalPath()
                    .replace('\\', '/'));
            log.debug("PDF file = " + sPDFUrl.toString());

            if (pdfCreator != null) {
                log.debug("about to create the PDF file");
                sleep();
                pdfCreator.storeToURL(
                        sPDFUrl.toString(), filterOpts);
                log.debug("done");
            }
        }
    }       
}

提前致谢。

【问题讨论】:

  • 当您的应用程序运行时,您能否查看在保存为 PDF 之前在 Open Office 中加载的文件?您可能需要在加载文档时设置可见性。如果是,图像是否可见?
  • 没有隐藏文件。我确实这么想,并试图让它可见,但我无法让它这样做。如果您能帮助我使其打开可见,我将不胜感激?

标签: java pdf openoffice.org


【解决方案1】:

很高兴使文档可见的建议有所帮助。由于它还解决了问题,因此您遇到了计时/线程问题。我怀疑您会发现在执行保存到 PDF 之前进行睡眠的另一个狡猾的选项也会允许图像出现。这些解决方案都不好。

大多数可能最好的解决方法是升级到更新版本的 Open Office(您拥有的 API 调用应该仍然有效)。另一种选择是尝试调用 API 以要求文档自行刷新。

【讨论】:

    【解决方案2】:

    找到正确的属性后,我能够打开隐藏属性设置为 false 的文件,这意味着当文件导出为 PDF 时,它包含背景图像。很遗憾,我找不到另一个可以隐藏文件但至少可以正常工作的解决方案。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2014-06-29
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多