diff options
author | Teddy Wing | 2023-04-22 19:27:58 +0200 |
---|---|---|
committer | Teddy Wing | 2023-04-22 19:27:58 +0200 |
commit | d48f72fd266fb9b6b48122a4b192140774475416 (patch) | |
tree | e9271dd94edf5f0548f3d09204bea078e4abfce5 | |
download | pdf-form-replace-font2-d48f72fd266fb9b6b48122a4b192140774475416.tar.bz2 |
Change PDF form field font
Use iText to change the font of interactive form fields in a PDF.
Changes the font in 'f1040.pdf' to Courier.
This code is based on the following example from iText:
https://kb.itextsupport.com/home/it7kb/ebooks/itext-7-jump-start-tutorial-for-net/chapter-5-manipulating-an-existing-pdf-document-net#Chapter5:ManipulatinganexistingPDFdocument|.NET-Changingthepropertiesofformfields
For now I just downloaded the dependencies' JAR files and put them in a
'lib/' directory rather than have to learn how to use a dependency
management tool like Maven. Here is the contents of the 'lib/'
directory:
barcodes-7.2.2.jar
commons-7.2.2.jar
font-asian-7.2.2.jar
forms-7.2.2.jar
hyph-7.2.2.jar
io-7.2.2.jar
kernel-7.2.2.jar
layout-7.2.2.jar
pdfa-7.2.2.jar
pdftest-7.2.2.jar
sign-7.2.2.jar
slf4j-api-1.7.9.jar
styled-xml-parser-7.2.2.jar
svg-7.2.2.jar
To run this:
$ make
$ make run
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | Main.java | 29 | ||||
-rw-r--r-- | Makefile | 8 |
3 files changed, 40 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ff6cbba --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.class + +*.pdf diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..7ae3719 --- /dev/null +++ b/Main.java @@ -0,0 +1,29 @@ +import com.itextpdf.kernel.pdf.*; +import com.itextpdf.kernel.font.*; +import com.itextpdf.io.font.constants.StandardFonts; +import com.itextpdf.forms.*; +import com.itextpdf.forms.fields.*; + +import java.io.*; +import java.util.Map; + +public class Main { + public static void main(String args[]) throws IOException { + PdfDocument pdf = new PdfDocument( + new PdfReader("f1040.pdf"), + new PdfWriter("f1040-courier.pdf") + ); + + PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, false); + + PdfFont courier = PdfFontFactory.createFont(StandardFonts.COURIER); + Map<String, PdfFormField> fields = form.getFormFields(); + + for (var entry : fields.entrySet()) { + PdfFormField field = entry.getValue(); + field.setFont(courier); + } + + pdf.close(); + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0125451 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CLASSPATH := -classpath '.:./lib/*' + +all: + javac $(CLASSPATH) Main.java + +.PHONY: run +run: + java $(CLASSPATH) Main |