打开Office 1.1.4使用java导出为带有背景图像的PDF

时间:2014-07-23 13:29:51

标签: java pdf openoffice.org

我有一个问题需要解决我们使用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");
            }
        }
    }       
}

提前致谢。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

大多数最佳解决方案是升级到较新版本的Open Office(您应该使用的API调用仍然有效)。另一个选择是尝试调用API来要求文档自行刷新。