Migrate from iText 7 to PDFluent

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.

Migrating from iText 7 to PDFluent. Install with cargo add pdfluent

Migration steps

1

Replace the dependency

Remove the iText Maven dependency and add PDFluent to your Rust project with cargo add.

iText 7 (before)
<!-- pom.xml -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext7-core</artifactId>
    <version>7.2.5</version>
    <type>pom</type>
</dependency>
PDFluent (after)
# Cargo.toml
[dependencies]
pdfluent = "0.9"
2

Open a document

iText uses PdfReader and PdfDocument. PDFluent uses Document::open which returns a Result.

iText 7 (before)
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;

PdfDocument pdf = new PdfDocument(new PdfReader("report.pdf"));
PDFluent (after)
use pdfluent::Document;

let doc = Document::open("report.pdf")?;
3

Extract text

iText uses PdfTextExtractor with a LocationTextExtractionStrategy. PDFluent exposes a page-level extract_text method.

iText 7 (before)
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()
);
PDFluent (after)
let text = doc.page(0)?.extract_text()?;
4

Fill form fields

iText uses PdfAcroForm.getField().setValue(). PDFluent uses acroform().set_field().

iText 7 (before)
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");
PDFluent (after)
let mut form = doc.acroform()?;
form.set_field("customer_name", "Acme Corp")?;
form.set_field("invoice_date", "2024-04-14")?;
5

Save the output

iText writes to a PdfWriter. PDFluent uses Document::save.

iText 7 (before)
import com.itextpdf.kernel.pdf.PdfWriter;

PdfDocument out = new PdfDocument(
    new PdfReader("input.pdf"),
    new PdfWriter("output.pdf")
);
// ... modifications ...
out.close();
PDFluent (after)
doc.save("output.pdf")?;

Things to watch out for

  • !iText page numbers are 1-indexed. PDFluent page numbers are 0-indexed.
  • !iText AGPL requires source disclosure for commercial use. Check your license before migration.
  • !iText's pdfHTML add-on (HTML to PDF) has no direct PDFluent equivalent.

Frequently asked questions