Skip to content

Commit

Permalink
Support short font names in DA for freetext annotations
Browse files Browse the repository at this point in the history
Acrobat resolves such names in DA as well.

DEV-1896
  • Loading branch information
IdamkinI committed Aug 11, 2017
1 parent b4a29ef commit 133eabb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
29 changes: 25 additions & 4 deletions itext/src/main/java/com/itextpdf/text/pdf/PdfStamperImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;

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

//Hash map of standard fonts used in flattening of annotations to prevent fonts duplication
private HashMap<String, PdfIndirectReference> builtInAnnotationFonts = new HashMap<String, PdfIndirectReference>();
private static HashMap<String, String> fromShortToFullAnnotationFontNames = new HashMap<String, String>();

static {
fromShortToFullAnnotationFontNames.put("CoBO", BaseFont.COURIER_BOLDOBLIQUE);
fromShortToFullAnnotationFontNames.put("CoBo", BaseFont.COURIER_BOLD);
fromShortToFullAnnotationFontNames.put("CoOb", BaseFont.COURIER_OBLIQUE);
fromShortToFullAnnotationFontNames.put("Cour", BaseFont.COURIER);
fromShortToFullAnnotationFontNames.put("HeBO", BaseFont.HELVETICA_BOLDOBLIQUE);
fromShortToFullAnnotationFontNames.put("HeBo", BaseFont.HELVETICA_BOLD);
fromShortToFullAnnotationFontNames.put("HeOb", BaseFont.HELVETICA_OBLIQUE);
fromShortToFullAnnotationFontNames.put("Helv", BaseFont.HELVETICA);
fromShortToFullAnnotationFontNames.put("Symb", BaseFont.SYMBOL);
fromShortToFullAnnotationFontNames.put("TiBI", BaseFont.TIMES_BOLDITALIC);
fromShortToFullAnnotationFontNames.put("TiBo", BaseFont.TIMES_BOLD);
fromShortToFullAnnotationFontNames.put("TiIt", BaseFont.TIMES_ITALIC);
fromShortToFullAnnotationFontNames.put("TiRo", BaseFont.TIMES_ROMAN);
fromShortToFullAnnotationFontNames.put("ZaDb", BaseFont.ZAPFDINGBATS);
}

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

Expand Down Expand Up @@ -1309,12 +1326,16 @@ private void flattenAnnotations(boolean flattenFreeTextAnnotations) {
if (operator.toString().equals("Tf")) {
pdfFontName = (PdfName) operands.get(0);
String fontName = pdfFontName.toString().substring(1);
fontReference = builtInAnnotationFonts.get(fontName);
String fullName = fromShortToFullAnnotationFontNames.get(fontName);
if (fullName == null) {
fullName = fontName;
}
fontReference = builtInAnnotationFonts.get(fullName);
if (fontReference == null) {
PdfDictionary dic = BaseFont.createBuiltInFontDictionary(fontName);
PdfDictionary dic = BaseFont.createBuiltInFontDictionary(fullName);
if (dic != null) {
fontReference = addToBody(dic).getIndirectReference();
builtInAnnotationFonts.put(fontName, fontReference);
builtInAnnotationFonts.put(fullName, fontReference);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ public void flattenAndCheckCourier() throws IOException, DocumentException, Inte
checkPageContent(outputFile);
}

@Test
public void flattenAndCheckShortFontName() throws IOException, DocumentException, InterruptedException {
String inputFile = FOLDER + "freetext-times-short.pdf";
String outputFile = TARGET + "freetext-times-short-flattened.pdf";

flattenFreeText(inputFile, outputFile);
checkPageContent(outputFile);

String errorMessage = new CompareTool().compareByContent(outputFile, FOLDER + "cmp_freetext-times-short-flattened.pdf", TARGET, "diff_short");
if ( errorMessage != null ) {
Assert.fail(errorMessage);
}
}

private void checkAnnotationSize(String path, int expectedAnnotationsSize) throws IOException, DocumentException {
FileInputStream fin = null;
try {
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 133eabb

Please sign in to comment.