将 XML 转换为 PDF

XML 格式用于存储结构化数据。有几种方法可以转换 XML 在 Aspose.PDF 中转换为 PDF。

考虑使用基于 XSL-FO 标准的 XML 文档选项。

将 XSL-FO 转换为 PDF

可以使用以下方式实现 XSL-FO 文件到 PDF 的转换 文档 对象为 XslFoLoadOptions, 但有时你可能会遇到文件结构不正确的情况。

// Convert XML to PDF
    public void convertXMLtoPDF() {
        // Initialize document object
        String pdfDocumentFileName = new File(fileStorage,"XML-to-PDF.pdf").toString();
        String xmlDocumentFileName = new File(fileStorage,"Conversion/employees.xml").toString();
        String xsltDocumentFileName = new File(fileStorage, "Conversion/employees.xslt").toString();

        try {
            XslFoLoadOptions options = new XslFoLoadOptions(xsltDocumentFileName);
            document = new Document(xmlDocumentFileName,options);
            // Save resultant PDF file
            document.save(pdfDocumentFileName.toString());
        } catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        resultMessage.setText(R.string.success_message);
    }    
    ```