Skip to main content

How to Convert Between OFD and PDF in Java

 OFD, short for Open Fixed-layout Document, is a fixed-layout document format commonly used in China for electronic documents, invoices, and official files. PDF is another fixed-layout format widely used for document sharing, printing, and archiving.

In Java document processing projects, you may need to convert OFD files to PDF for easier viewing or distribution. You may also need to convert PDF files to OFD to meet specific document exchange or archiving requirements.

This article shows how to convert OFD to PDF and PDF to OFD in Java using Spire.PDF for Java. The examples are kept simple and focus on common conversion tasks.


Install Spire.PDF for Java

If you use Maven, add the following repository and dependency to your pom.xml file:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.6.1</version>
    </dependency>
</dependencies>

For Gradle projects, use:

repositories {
    maven {
        url 'https://repo.e-iceblue.cn/repository/maven-public/'
    }
}

dependencies {
    implementation 'e-iceblue:spire.pdf:12.6.1'
}

You can replace the version number with the version used in your project.

Convert OFD to PDF in Java

To convert an OFD file to PDF, use the OfdConverter class. You only need to specify the input OFD file, call the toPdf() method, and then release the converter.

import com.spire.pdf.conversion.OfdConverter;

public class ConvertOfdToPdf {
    public static void main(String[] args) {
        // Specify the input OFD file path
        String inputFile = "data/sample.ofd";

        // Specify the output PDF file path
        String outputFile = "output/sample.pdf";

        // Create an OfdConverter instance
        OfdConverter converter = new OfdConverter(inputFile);

        // Convert OFD to PDF
        converter.toPdf(outputFile);

        // Release resources
        converter.dispose();
    }
}

The OfdConverter class is used for OFD-related conversion. In this example, the source OFD file is loaded from the data folder and saved as a PDF file in the output folder.

Convert PDF to OFD in Java

To convert a PDF document to OFD, load the PDF with PdfDocument, then save it using FileFormat.OFD.

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToOfd {
    public static void main(String[] args) {
        // Create a PdfDocument object
        PdfDocument document = new PdfDocument();

        // Load the PDF file
        document.loadFromFile("data/sample.pdf");

        // Save the PDF file as OFD
        document.saveToFile("output/sample.ofd", FileFormat.OFD);

        // Release resources
        document.close();
        document.dispose();
    }
}

The important part is the saveToFile() method. By passing FileFormat.OFD, the loaded PDF document is saved as an OFD file.

Batch Convert OFD Files to PDF in Java

If you need to convert multiple OFD files, you can scan a folder and process all .ofd files one by one.

import com.spire.pdf.conversion.OfdConverter;

import java.io.File;

public class BatchConvertOfdToPdf {
    public static void main(String[] args) {
        String inputDirectory = "input/ofd";
        String outputDirectory = "output/pdf";

        File inputDir = new File(inputDirectory);
        File outputDir = new File(outputDirectory);

        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }

        File[] ofdFiles = inputDir.listFiles((dir, name) -> name.toLowerCase().endsWith(".ofd"));

        if (ofdFiles == null || ofdFiles.length == 0) {
            System.out.println("No OFD files found.");
            return;
        }

        for (File ofdFile : ofdFiles) {
            String fileName = ofdFile.getName();
            String outputFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";
            String outputPath = new File(outputDir, outputFileName).getAbsolutePath();

            OfdConverter converter = new OfdConverter(ofdFile.getAbsolutePath());
            converter.toPdf(outputPath);
            converter.dispose();

            System.out.println("Converted: " + fileName);
        }
    }
}

This example is useful for simple batch conversion tasks, such as converting a folder of OFD invoices, reports, or archived files to PDF.

Practical Notes

Before using the conversion code in a project, there are a few details worth checking:

  • Make sure the input file path and output folder path are correct.
  • Create the output directory before saving converted files.
  • Call dispose() after using OfdConverter.
  • Call both close() and dispose() after using PdfDocument.
  • If the document contains Chinese characters or special fonts, test the output on the target server environment.
  • For important documents, compare the converted file with the source file before using it in production.

FAQs

Can Java convert OFD to PDF?

Yes. You can use OfdConverter in Spire.PDF for Java to load an OFD file and convert it to PDF with the toPdf() method.

Can Java convert PDF to OFD?

Yes. You can load a PDF file with PdfDocument and save it as an OFD file by using saveToFile() with FileFormat.OFD.

Does the conversion require Microsoft Office or Adobe Acrobat?

No. The conversion is handled directly in Java and does not require Microsoft Office or Adobe Acrobat to be installed.

Will the converted file keep the same layout?

OFD and PDF are both fixed-layout document formats, so they are suitable for preserving page layout during conversion. However, the final result may still depend on the source document, fonts, images, and runtime environment. For business or official documents, it is recommended to review the converted output.

Conclusion

Converting between OFD and PDF in Java can be done with a small amount of code. For OFD to PDF conversion, use the OfdConverter class and call toPdf(). For PDF to OFD conversion, load the PDF with PdfDocument and save it with FileFormat.OFD.

For single-file conversion, the basic examples are usually enough. For repeated tasks, you can use a simple batch conversion loop to process multiple OFD files in a folder.

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>    ...

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();   ...