Converting file streams to PDF/UA
Applications don’t always receive documents as file paths. A document might already be open as a file stream when it reaches a conversion service.
This sample converts a PDF supplied through a FileInputStream into a PDF/UA document written through a FileOutputStream. Keeping the conversion stream-based avoids reopening the input and output through SDK file-path overloads.
Preparing the project
Specify a package name and import the SDK and Java I/O classes used by the sample:
package io.nutrient.Sample;
import io.nutrient.sdk.Document;import io.nutrient.sdk.enums.PdfConformance;import io.nutrient.sdk.settings.PdfSettings;
import java.io.FileInputStream;import java.io.FileOutputStream;
public class ConvertFromFileStreamToPdfUa {Create the entry point and open the input file, output file, and document in a try-with-resources statement. Keep the input stream open for the lifetime of the document. Java closes the document before it closes either stream.
public static void main(String[] args) { try (FileInputStream inputStream = new FileInputStream("input.pdf"); FileOutputStream outputStream = new FileOutputStream("output.pdf"); Document document = Document.open(inputStream)) {Converting file streams to PDF/UA
Set PDF/UA-1 conformance and pass the output stream to exportAsPdf():
PdfSettings pdfSettings = document.getSettings().getPdfSettings(); pdfSettings.setConformance(PdfConformance.PDF_UA_1);
document.exportAsPdf(outputStream); System.out.println("Successfully converted the file stream to PDF/UA."); } catch (Exception e) { System.err.println("Failed to convert the file stream to PDF/UA: " + e.getMessage()); System.exit(1); } }}Document.open(FileInputStream) reads the source through the supplied stream. exportAsPdf(FileOutputStream) writes the converted document through the supplied output stream. Both streams remain owned by the application and are closed by the try-with-resources statement.
Error handling
The sample catches exceptions from Java file operations and SDK document processing. It writes an error message and returns a nonzero exit code when either operation fails.
Conclusion
The sample opens a PDF from a FileInputStream, applies PDF/UA-1 conformance settings, and writes the converted document to a FileOutputStream.
Download this ready-to-use sample package.