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 usingOfdConverter. - Call both
close()anddispose()after usingPdfDocument. - 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.
Top comments (0)