#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
Keep form fields fillable when embedding fonts for PDF/UA (#8716)
Anthony Stirlingf67c1ccaa692
diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/service/ua/FontEmbeddingService.java b/app/proprietary/src/main/java/stirling/software/proprietary/service/ua/FontEmbeddingService.javaindex 9a3ad8e934..fa45664669 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/service/ua/FontEmbeddingService.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/service/ua/FontEmbeddingService.java@@ -17,6 +17,8 @@ import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;import org.apache.pdfbox.pdmodel.PDResources;import org.apache.pdfbox.pdmodel.font.PDFont;+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;import org.apache.pdfbox.text.PDFTextStripper;import org.springframework.stereotype.Service;@@ -69,8 +71,10 @@ public class FontEmbeddingService {*/public Result embedFonts(byte[] pdfBytes) {Set<String> missing;+ boolean hasFormFields;try (PDDocument document = Loader.loadPDF(pdfBytes)) {missing = findUnembeddedFonts(document);+ hasFormFields = hasFormFields(document);} catch (IOException e) {return new Result(pdfBytes, false, Set.of(), "Could not inspect fonts: " + e.getMessage());@@ -78,6 +82,15 @@ public class FontEmbeddingService {if (missing.isEmpty()) {return new Result(pdfBytes, false, Set.of(), null);}+ if (hasFormFields) {+ return new Result(+ pdfBytes,+ false,+ missing,+ "Font embedding was skipped because it would flatten the form fields. "+ + missing.size()+ + " font(s) remain unembedded.");+ }if (!isGhostscriptAvailable()) {return new Result(pdfBytes,@@ -149,6 +162,17 @@ public class FontEmbeddingService {}}+ private static boolean hasFormFields(PDDocument document) throws IOException {+ for (PDPage page : document.getPages()) {+ for (PDAnnotation annotation : page.getAnnotations()) {+ if (annotation instanceof PDAnnotationWidget) {+ return true;+ }+ }+ }+ return false;+ }+/*** True when the rewritten document still holds the original's content. A collapse in page count* or content-stream size is the only signature of a failed rewrite the exit code hides.@@ -189,8 +213,8 @@ public class FontEmbeddingService {* came back with each page truncated to its first half - 422070 characters down to 211230 -* while its content streams stayed well inside the byte ratio below.*- * <p>Growth is not loss: flattening a widget annotation into the page legitimately adds text.- * Only a shortfall fails.+ * <p>Growth is not loss: flattening an annotation into the page legitimately adds text. Only a+ * shortfall fails.*/private static boolean lostText(PDDocument before, PDDocument after) {String textBefore = extractText(before);diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaRealCorpusTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaRealCorpusTest.javaindex 4d11551aff..eb4744785c 100644--- a/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaRealCorpusTest.java+++ b/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaRealCorpusTest.java@@ -36,12 +36,10 @@ class PdfUaRealCorpusTest {/** Files the converter is expected to refuse rather than process. */private static final List<String> EXPECTED_REJECTS = List.of("encrypted.pdf", "corrupted.pdf");- // Files the font-embedding pass still alters, measured 2026-08-28. Both are- // ADDITIONS, not loss: the embedder flattens a widget annotation into the- // page, and injects spaces into rotated text. Loss is caught by- // FontEmbeddingService, which keeps the original instead.- private static final List<String> KNOWN_EMBED_TEXT_DIFFS =- List.of("rotated-text-sample.pdf", "annotation-text-sample.pdf");+ // Files the font-embedding pass still alters, measured 2026-08-28. This is an+ // ADDITION, not loss: the embedder injects spaces into rotated text. Loss is+ // caught by FontEmbeddingService, which keeps the original instead.+ private static final List<String> KNOWN_EMBED_TEXT_DIFFS = List.of("rotated-text-sample.pdf");@BeforeAllstatic void setUp() {diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaServicesTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaServicesTest.javaindex 9d2e14ee0d..19ff7ba589 100644--- a/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaServicesTest.java+++ b/app/proprietary/src/test/java/stirling/software/proprietary/service/ua/PdfUaServicesTest.java@@ -1,5 +1,6 @@package stirling.software.proprietary.service.ua;+import static org.junit.jupiter.api.Assertions.assertArrayEquals;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertNotNull;@@ -18,6 +19,9 @@ import org.apache.pdfbox.pdmodel.encryption.AccessPermission;import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;import org.apache.pdfbox.pdmodel.font.PDType1Font;import org.apache.pdfbox.pdmodel.font.Standard14Fonts;+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;+import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Nested;@@ -229,6 +233,47 @@ class PdfUaServicesTest {assertFalse(result.changed());assertEquals(embedded.length, result.pdfBytes().length);}++ @Test+ @DisplayName("keeps a fillable form rather than flattening it to embed fonts")+ void keepsFormFieldsFillable() throws Exception {+ assertFormKept(unembeddedFontForm(true));+ }++ @Test+ @DisplayName("keeps widgets the AcroForm does not list rather than flattening them")+ void keepsOrphanWidgetsFillable() throws Exception {+ assertFormKept(unembeddedFontForm(false));+ }++ private void assertFormKept(byte[] form) {+ FontEmbeddingService.Result result = fonts.embedFonts(form);+ assertFalse(result.changed());+ assertArrayEquals(form, result.pdfBytes());+ assertTrue(result.fonts().contains("Helvetica"));+ assertNotNull(result.warning());+ assertTrue(result.warning().contains("form fields"));+ }+ }++ private static byte[] unembeddedFontForm(boolean listedInAcroForm) throws IOException {+ try (PDDocument document = Loader.loadPDF(unembeddedFontPdf())) {+ PDAcroForm acroForm = new PDAcroForm(document);+ document.getDocumentCatalog().setAcroForm(acroForm);+ PDTextField field = new PDTextField(acroForm);+ field.setPartialName("name");+ if (listedInAcroForm) {+ acroForm.getFields().add(field);+ }+ PDAnnotationWidget widget = field.getWidgets().get(0);+ widget.setRectangle(new PDRectangle(50, 600, 200, 20));+ PDPage page = document.getPage(0);+ widget.setPage(page);+ page.getAnnotations().add(widget);+ ByteArrayOutputStream out = new ByteArrayOutputStream();+ document.save(out);+ return out.toByteArray();+ }}@Nested