A step-by-step guide for replacing Apache PDFBox with PDFluent. Covers dependency setup, document loading, text extraction, form filling, and saving.
cargo add pdfluent@1.0.0-beta.5Remove PDFBox from pom.xml or build.gradle and add pdfluent to Cargo.toml.
<!-- pom.xml -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.1</version>
</dependency># Cargo.toml
[dependencies]
pdfluent = "0.9"PDFBox uses PDDocument.load() with a File or byte array. PDFluent uses Document::open which returns a Result.
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
PDDocument doc = PDDocument.load(new File("contract.pdf"));use pdfluent::PdfDocument;
let doc = PdfDocument::open("contract.pdf")?;PDFBox requires a PDFTextStripper instance and produces a single string for the whole document. PDFluent extracts per-page.
import org.apache.pdfbox.text.PDFTextStripper;
PDFTextStripper stripper = new PDFTextStripper();
stripper.setStartPage(1);
stripper.setEndPage(1);
String text = stripper.getText(doc);let text = doc.page(1)?.text()?;
// All pages
for i in 0..doc.page_count() {
let text = doc.page(i)?.text()?;
println!("{}", text);
}PDFBox accesses fields through PDDocumentCatalog and PDAcroForm. PDFluent uses a direct acroform() handle.
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
acroForm.getField("first_name").setValue("Jane");
acroForm.getField("last_name").setValue("Smith");
acroForm.flatten();let mut form = doc.acroform()?;
form.set_field("first_name", "Jane")?;
form.set_field("last_name", "Smith")?;
form.flatten()?;PDFBox requires explicit close(). PDFluent drops the document when it goes out of scope; call save() to write.
doc.save("output.pdf");
doc.close(); // must call close() to release file handlesdoc.save("output.pdf")?;
// doc drops automatically at end of scope