A step-by-step guide for moving an iText 7 Java codebase to PDFluent in Rust. Covers opening documents, text extraction, form filling, and saving.
cargo add pdfluentRemove the iText Maven dependency and add PDFluent to your Rust project with cargo add.
<!-- pom.xml -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.5</version>
<type>pom</type>
</dependency># Cargo.toml
[dependencies]
pdfluent = "0.9"iText uses PdfReader and PdfDocument. PDFluent uses Document::open which returns a Result.
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
PdfDocument pdf = new PdfDocument(new PdfReader("report.pdf"));use pdfluent::Document;
let doc = Document::open("report.pdf")?;iText uses PdfTextExtractor with a LocationTextExtractionStrategy. PDFluent exposes a page-level extract_text method.
import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor;
import com.itextpdf.kernel.pdf.canvas.parser.listener.LocationTextExtractionStrategy;
String text = PdfTextExtractor.getTextFromPage(
pdf.getPage(1),
new LocationTextExtractionStrategy()
);let text = doc.page(0)?.extract_text()?;iText uses PdfAcroForm.getField().setValue(). PDFluent uses acroform().set_field().
import com.itextpdf.forms.PdfAcroForm;
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
form.getField("customer_name").setValue("Acme Corp");
form.getField("invoice_date").setValue("2024-04-14");let mut form = doc.acroform()?;
form.set_field("customer_name", "Acme Corp")?;
form.set_field("invoice_date", "2024-04-14")?;iText writes to a PdfWriter. PDFluent uses Document::save.
import com.itextpdf.kernel.pdf.PdfWriter;
PdfDocument out = new PdfDocument(
new PdfReader("input.pdf"),
new PdfWriter("output.pdf")
);
// ... modifications ...
out.close();doc.save("output.pdf")?;