Skip to content

Commit 133eabb

Browse files
committed
Support short font names in DA for freetext annotations
Acrobat resolves such names in DA as well. DEV-1896
1 parent b4a29ef commit 133eabb

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

itext/src/main/java/com/itextpdf/text/pdf/PdfStamperImp.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
import java.util.Iterator;
8383
import java.util.List;
8484
import java.util.Map;
85-
import java.util.RandomAccess;
8685

8786
class PdfStamperImp extends PdfWriter {
8887
HashMap<PdfReader, IntHashtable> readers2intrefs = new HashMap<PdfReader, IntHashtable>();
@@ -130,6 +129,24 @@ protected Counter getCounter() {
130129

131130
//Hash map of standard fonts used in flattening of annotations to prevent fonts duplication
132131
private HashMap<String, PdfIndirectReference> builtInAnnotationFonts = new HashMap<String, PdfIndirectReference>();
132+
private static HashMap<String, String> fromShortToFullAnnotationFontNames = new HashMap<String, String>();
133+
134+
static {
135+
fromShortToFullAnnotationFontNames.put("CoBO", BaseFont.COURIER_BOLDOBLIQUE);
136+
fromShortToFullAnnotationFontNames.put("CoBo", BaseFont.COURIER_BOLD);
137+
fromShortToFullAnnotationFontNames.put("CoOb", BaseFont.COURIER_OBLIQUE);
138+
fromShortToFullAnnotationFontNames.put("Cour", BaseFont.COURIER);
139+
fromShortToFullAnnotationFontNames.put("HeBO", BaseFont.HELVETICA_BOLDOBLIQUE);
140+
fromShortToFullAnnotationFontNames.put("HeBo", BaseFont.HELVETICA_BOLD);
141+
fromShortToFullAnnotationFontNames.put("HeOb", BaseFont.HELVETICA_OBLIQUE);
142+
fromShortToFullAnnotationFontNames.put("Helv", BaseFont.HELVETICA);
143+
fromShortToFullAnnotationFontNames.put("Symb", BaseFont.SYMBOL);
144+
fromShortToFullAnnotationFontNames.put("TiBI", BaseFont.TIMES_BOLDITALIC);
145+
fromShortToFullAnnotationFontNames.put("TiBo", BaseFont.TIMES_BOLD);
146+
fromShortToFullAnnotationFontNames.put("TiIt", BaseFont.TIMES_ITALIC);
147+
fromShortToFullAnnotationFontNames.put("TiRo", BaseFont.TIMES_ROMAN);
148+
fromShortToFullAnnotationFontNames.put("ZaDb", BaseFont.ZAPFDINGBATS);
149+
}
133150

134151
private double[] DEFAULT_MATRIX = {1, 0, 0, 1, 0, 0};
135152

@@ -1309,12 +1326,16 @@ private void flattenAnnotations(boolean flattenFreeTextAnnotations) {
13091326
if (operator.toString().equals("Tf")) {
13101327
pdfFontName = (PdfName) operands.get(0);
13111328
String fontName = pdfFontName.toString().substring(1);
1312-
fontReference = builtInAnnotationFonts.get(fontName);
1329+
String fullName = fromShortToFullAnnotationFontNames.get(fontName);
1330+
if (fullName == null) {
1331+
fullName = fontName;
1332+
}
1333+
fontReference = builtInAnnotationFonts.get(fullName);
13131334
if (fontReference == null) {
1314-
PdfDictionary dic = BaseFont.createBuiltInFontDictionary(fontName);
1335+
PdfDictionary dic = BaseFont.createBuiltInFontDictionary(fullName);
13151336
if (dic != null) {
13161337
fontReference = addToBody(dic).getIndirectReference();
1317-
builtInAnnotationFonts.put(fontName, fontReference);
1338+
builtInAnnotationFonts.put(fullName, fontReference);
13181339
}
13191340
}
13201341
}

itext/src/test/java/com/itextpdf/text/pdf/FreeTextFlatteningTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ public void flattenAndCheckCourier() throws IOException, DocumentException, Inte
106106
checkPageContent(outputFile);
107107
}
108108

109+
@Test
110+
public void flattenAndCheckShortFontName() throws IOException, DocumentException, InterruptedException {
111+
String inputFile = FOLDER + "freetext-times-short.pdf";
112+
String outputFile = TARGET + "freetext-times-short-flattened.pdf";
113+
114+
flattenFreeText(inputFile, outputFile);
115+
checkPageContent(outputFile);
116+
117+
String errorMessage = new CompareTool().compareByContent(outputFile, FOLDER + "cmp_freetext-times-short-flattened.pdf", TARGET, "diff_short");
118+
if ( errorMessage != null ) {
119+
Assert.fail(errorMessage);
120+
}
121+
}
122+
109123
private void checkAnnotationSize(String path, int expectedAnnotationsSize) throws IOException, DocumentException {
110124
FileInputStream fin = null;
111125
try {

0 commit comments

Comments
 (0)