Skip to content

Commit 8614e72

Browse files
committed
Add all the attributes mentioned in xfdf spec for the supported xfdf annotations types
DEVSIX-4027
1 parent b006317 commit 8614e72

File tree

64 files changed

+914
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+914
-160
lines changed

forms/src/main/java/com/itextpdf/forms/xfdf/AnnotObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void addAttribute(String name, boolean value) {
337337
}
338338

339339
void addAttribute(String name, float value) {
340-
attributes.add(new AttributeObject(name, String.valueOf(value)));
340+
attributes.add(new AttributeObject(name, XfdfObjectUtils.convertFloatToString(value)));
341341
}
342342

343343
void addAttribute(String name, Rectangle value) {
@@ -364,7 +364,7 @@ void addAttribute(String name, PdfObject valueObject, boolean required) {
364364
} else if (valueObject.getType() == PdfObject.NAME) {
365365
valueString = ((PdfName)(valueObject)).getValue();
366366
} else if (valueObject.getType() == PdfObject.NUMBER) {
367-
valueString = String.valueOf(((PdfNumber)(valueObject)).getValue());
367+
valueString = XfdfObjectUtils.convertFloatToString((float)((PdfNumber)(valueObject)).getValue());
368368
} else if (valueObject.getType() == PdfObject.STRING) {
369369
valueString = ((PdfString)(valueObject)).getValue();
370370
}

forms/src/main/java/com/itextpdf/forms/xfdf/XfdfConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public final class XfdfConstants {
144144
public static final String NAME_CAPITAL = "Name";
145145
public static final String DEFAULT_APPEARANCE = "defaultappearance";
146146
public static final String DEFAULT_STYLE = "defaultstyle";
147+
public static final String INTENSITY = "intensity";
147148

148149
private XfdfConstants() {
149150
}

forms/src/main/java/com/itextpdf/forms/xfdf/XfdfObjectFactory.java

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@ This file is part of the iText (R) project.
5656
import org.xml.sax.SAXException;
5757

5858
import javax.xml.parsers.ParserConfigurationException;
59-
import java.io.IOException;
6059
import java.io.InputStream;
6160
import java.util.ArrayList;
6261
import java.util.List;
6362
import java.util.StringTokenizer;
6463

6564
public class XfdfObjectFactory {
6665

67-
private static Logger logger = LoggerFactory.getLogger(XfdfObjectFactory.class);
66+
private static final Logger logger = LoggerFactory.getLogger(XfdfObjectFactory.class);
6867

6968
/**
7069
* Extracts data from pdf document acroform and annotations into XfdfObject.
@@ -124,10 +123,9 @@ public XfdfObject createXfdfObject(PdfDocument document, String filename) {
124123
* @return XfdfObject containing original xfdf data.
125124
* @throws ParserConfigurationException if a XfdfObject cannot be created which satisfies the configuration
126125
* requested.
127-
* @throws IOException if any I/O issue occurs.
128126
* @throws SAXException if any parse errors occurs.
129127
*/
130-
public XfdfObject createXfdfObject(InputStream xfdfInputStream) throws ParserConfigurationException, IOException, SAXException {
128+
public XfdfObject createXfdfObject(InputStream xfdfInputStream) throws ParserConfigurationException, SAXException {
131129
XfdfObject xfdfObject = new XfdfObject();
132130

133131
Document document = XfdfFileUtils.createXfdfDocumentFromStream(xfdfInputStream);
@@ -209,6 +207,8 @@ private static boolean isAnnotSupported(String nodeName) {
209207
XfdfConstants.SQUARE.equalsIgnoreCase(nodeName) ||
210208
XfdfConstants.POLYLINE.equalsIgnoreCase(nodeName) ||
211209
XfdfConstants.POLYGON.equalsIgnoreCase(nodeName) ||
210+
XfdfConstants.STAMP.equalsIgnoreCase(nodeName) ||
211+
// XfdfConstants.FREETEXT.equalsIgnoreCase(nodeName) ||
212212
XfdfConstants.LINE.equalsIgnoreCase(nodeName);
213213
}
214214

@@ -233,12 +233,12 @@ private void visitAnnotationNode(Node currentNode, AnnotsObject annotsObject) {
233233
for (int i = 0; i < attributes.getLength(); i++) {
234234
addAnnotObjectAttribute(annotObject, attributes.item(i));
235235
}
236-
visitAnnotationInnerNodes(annotObject, currentNode);
236+
visitAnnotationInnerNodes(annotObject, currentNode, annotsObject);
237237
annotsObject.addAnnot(annotObject);
238238
}
239239
}
240240

241-
private void visitAnnotationInnerNodes(AnnotObject annotObject, Node annotNode) {
241+
private void visitAnnotationInnerNodes(AnnotObject annotObject, Node annotNode, AnnotsObject annotsObject) {
242242
NodeList children = annotNode.getChildNodes();
243243

244244
for (int temp = 0; temp < children.getLength(); temp++) {
@@ -256,6 +256,10 @@ private void visitAnnotationInnerNodes(AnnotObject annotObject, Node annotNode)
256256
if (XfdfConstants.VERTICES.equalsIgnoreCase(node.getNodeName())) {
257257
visitVerticesSubelement(node, annotObject);
258258
}
259+
if (isAnnotationSubtype(node.getNodeName()) &&
260+
isAnnotSupported(node.getNodeName())) {
261+
visitAnnotationNode(node, annotsObject);
262+
}
259263
}
260264
}
261265
}
@@ -330,7 +334,18 @@ private void addAnnotObjectAttribute(AnnotObject annotObject, Node attributeNode
330334
case XfdfConstants.REPLY_TYPE:
331335
case XfdfConstants.OPEN:
332336
case XfdfConstants.COORDS:
337+
case XfdfConstants.INTENT:
338+
case XfdfConstants.INTERIOR_COLOR:
339+
case XfdfConstants.HEAD:
340+
case XfdfConstants.TAIL:
333341
case XfdfConstants.FRINGE:
342+
case XfdfConstants.ROTATION:
343+
case XfdfConstants.JUSTIFICATION:
344+
345+
case XfdfConstants.WIDTH:
346+
case XfdfConstants.DASHES:
347+
case XfdfConstants.STYLE:
348+
case XfdfConstants.INTENSITY:
334349
annotObject.addAttribute(new AttributeObject(attributeName, attributeNode.getNodeValue()));
335350
break;
336351
default: logger.warn(IoLogMessageConstant.XFDF_UNSUPPORTED_ANNOTATION_ATTRIBUTE);
@@ -511,18 +526,25 @@ private static void addMarkupAnnotationAttributes(AnnotObject annot, PdfMarkupAn
511526
}
512527

513528
private static void addBorderStyleAttributes(AnnotObject annotObject, PdfNumber width,
514-
PdfString dashes, PdfString style) {
529+
PdfArray dashes, PdfName style) {
515530
annotObject.addAttribute(XfdfConstants.WIDTH, width);
516-
annotObject.addAttribute(XfdfConstants.DASHES, dashes);
517-
annotObject.addAttribute(XfdfConstants.STYLE, style);
531+
annotObject.addAttribute(XfdfConstants.DASHES, XfdfObjectUtils.convertDashesFromArray(dashes));
532+
annotObject.addAttribute(XfdfConstants.STYLE, XfdfObjectUtils.getStyleFullValue(style));
518533
}
519534

520535
private static void createTextMarkupAnnotation(PdfAnnotation pdfAnnotation, AnnotObject annot, int pageNumber) {
521536
PdfTextMarkupAnnotation pdfTextMarkupAnnotation = (PdfTextMarkupAnnotation) pdfAnnotation;
522537

523-
annot.addAttribute(new AttributeObject(XfdfConstants.COORDS,
524-
XfdfObjectUtils.convertQuadPointsToCoordsString(pdfTextMarkupAnnotation.getQuadPoints().toFloatArray())));
538+
if (pdfTextMarkupAnnotation.getQuadPoints() != null) {
539+
annot.addAttribute(new AttributeObject(XfdfConstants.COORDS, XfdfObjectUtils
540+
.convertQuadPointsToCoordsString(pdfTextMarkupAnnotation.getQuadPoints().toFloatArray())));
541+
}
525542

543+
if (PdfTextMarkupAnnotation.MarkupUnderline.equals(pdfTextMarkupAnnotation.getSubtype()) &&
544+
pdfTextMarkupAnnotation.getIntent() != null) {
545+
annot.addAttribute(new AttributeObject(XfdfConstants.INTENT,
546+
pdfTextMarkupAnnotation.getIntent().getValue()));
547+
}
526548

527549
if (pdfTextMarkupAnnotation.getContents() != null) {
528550
annot.setContents(pdfTextMarkupAnnotation.getContents());
@@ -559,22 +581,24 @@ private static void createCircleAnnotation(PdfAnnotation pdfAnnotation, AnnotObj
559581
PdfDictionary bs = pdfCircleAnnotation.getBorderStyle();
560582
if (bs != null) {
561583
addBorderStyleAttributes(annot, bs.getAsNumber(PdfName.W),
562-
bs.getAsString(PdfName.Dashed), bs.getAsString(PdfName.Style));
584+
bs.getAsArray(PdfName.D), bs.getAsName(PdfName.S));
563585
}
564586

565587
if (pdfCircleAnnotation.getBorderEffect() != null) {
566-
//TODO DEVSIX-4133 map intensity to border effect dictionary's I key
567-
//annot.addAttribute(new AttributeObject("intensity", pdfCircleAnnotation.getBorderEffect().getAsString()));
568-
annot.addAttribute(XfdfConstants.STYLE, pdfCircleAnnotation.getBorderEffect().getAsString(PdfName.Style));
569-
588+
annot.addAttribute(XfdfConstants.INTENSITY, pdfCircleAnnotation.getBorderEffect().getAsNumber(PdfName.I));
589+
if (annot.getAttribute(XfdfConstants.STYLE) == null) {
590+
annot.addAttribute(XfdfConstants.STYLE, XfdfObjectUtils.getStyleFullValue(
591+
pdfCircleAnnotation.getBorderEffect().getAsName(PdfName.S)));
592+
}
570593
}
571594

572595
if (pdfCircleAnnotation.getInteriorColor() != null && pdfCircleAnnotation.getInteriorColor().getColorValue() != null) {
573596
annot.addAttribute(new AttributeObject(XfdfConstants.INTERIOR_COLOR, XfdfObjectUtils.convertColorToString(pdfCircleAnnotation.getInteriorColor().getColorValue())));
574597
}
575598

576599
if(pdfCircleAnnotation.getRectangleDifferences() != null) {
577-
annot.addAttribute(new AttributeObject("fringe", XfdfObjectUtils.convertFringeToString(pdfCircleAnnotation.getRectangleDifferences().toFloatArray())));
600+
annot.addAttribute(new AttributeObject(XfdfConstants.FRINGE, XfdfObjectUtils.convertFringeToString(
601+
pdfCircleAnnotation.getRectangleDifferences().toFloatArray())));
578602
}
579603

580604
annot.setContents(pdfAnnotation.getContents());
@@ -589,20 +613,23 @@ private static void createSquareAnnotation(PdfAnnotation pdfAnnotation, AnnotObj
589613
PdfDictionary bs = pdfSquareAnnotation.getBorderStyle();
590614
if (bs != null) {
591615
addBorderStyleAttributes(annot, bs.getAsNumber(PdfName.W),
592-
bs.getAsString(PdfName.Dashed), bs.getAsString(PdfName.Style));
616+
bs.getAsArray(PdfName.D), bs.getAsName(PdfName.S));
593617
}
594618

595619
if (pdfSquareAnnotation.getBorderEffect() != null) {
596-
//TODO DEVSIX-4133 map intensity to border effect dictionary's I key
597-
//annot.addAttribute(new AttributeObject("intensity", pdfCircleAnnotation.getBorderEffect().getAsString()));
598-
annot.addAttribute(XfdfConstants.STYLE, pdfSquareAnnotation.getBorderEffect().getAsString(PdfName.Style));
620+
annot.addAttribute(XfdfConstants.INTENSITY, pdfSquareAnnotation.getBorderEffect().getAsNumber(PdfName.I));
621+
if (annot.getAttribute(XfdfConstants.STYLE) == null) {
622+
annot.addAttribute(XfdfConstants.STYLE, XfdfObjectUtils.getStyleFullValue(
623+
pdfSquareAnnotation.getBorderEffect().getAsName(PdfName.S)));
624+
}
599625
}
600626

601627
if (pdfSquareAnnotation.getInteriorColor() != null && pdfSquareAnnotation.getInteriorColor().getColorValue() != null) {
602628
annot.addAttribute(new AttributeObject(XfdfConstants.INTERIOR_COLOR, XfdfObjectUtils.convertColorToString(pdfSquareAnnotation.getInteriorColor().getColorValue())));
603629
}
604630
if(pdfSquareAnnotation.getRectangleDifferences() != null) {
605-
annot.addAttribute(new AttributeObject("fringe", XfdfObjectUtils.convertFringeToString(pdfSquareAnnotation.getRectangleDifferences().toFloatArray())));
631+
annot.addAttribute(new AttributeObject(XfdfConstants.FRINGE, XfdfObjectUtils.convertFringeToString(
632+
pdfSquareAnnotation.getRectangleDifferences().toFloatArray())));
606633
}
607634

608635
annot.setContents(pdfAnnotation.getContents());
@@ -615,7 +642,9 @@ private static void createStampAnnotation(PdfAnnotation pdfAnnotation, AnnotObje
615642
PdfStampAnnotation pdfStampAnnotation = (PdfStampAnnotation) pdfAnnotation;
616643

617644
annot.addAttribute(XfdfConstants.ICON, pdfStampAnnotation.getIconName());
618-
//How to add rotation? iText doesn't support ratotion attribute in PdfStampAnnotation
645+
if (pdfStampAnnotation.getRotation() != null) {
646+
annot.addAttribute(XfdfConstants.ROTATION, pdfStampAnnotation.getRotation().intValue());
647+
}
619648

620649
if (pdfStampAnnotation.getContents() != null) {
621650
annot.setContents(pdfStampAnnotation.getContents());
@@ -640,12 +669,14 @@ private static void createFreeTextAnnotation(PdfAnnotation pdfAnnotation, AnnotO
640669
PdfDictionary bs = pdfFreeTextAnnotation.getBorderStyle();
641670
if (bs != null) {
642671
addBorderStyleAttributes(annot, bs.getAsNumber(PdfName.W),
643-
bs.getAsString(PdfName.Dashed), bs.getAsString(PdfName.Style));
672+
bs.getAsArray(PdfName.D), bs.getAsName(PdfName.S));
644673
}
645674

646-
//TODO DEVSIX-4134 add rotation optional attribute
647-
//annot.addAttribute(new AttributeObject("rotation", pdfFreeTextAnnotation.));
648-
annot.addAttribute(new AttributeObject(XfdfConstants.JUSTIFICATION, String.valueOf(pdfFreeTextAnnotation.getJustification())));
675+
if (pdfFreeTextAnnotation.getRotation() != null) {
676+
annot.addAttribute(XfdfConstants.ROTATION, pdfFreeTextAnnotation.getRotation().intValue());
677+
}
678+
annot.addAttribute(new AttributeObject(XfdfConstants.JUSTIFICATION,
679+
XfdfObjectUtils.convertJustificationFromIntegerToString((pdfFreeTextAnnotation.getJustification()))));
649680
if (pdfFreeTextAnnotation.getIntent() != null) {
650681
annot.addAttribute(new AttributeObject(XfdfConstants.INTENT, pdfFreeTextAnnotation.getIntent().getValue()));
651682
}
@@ -703,7 +734,7 @@ private static void createLineAnnotation(PdfAnnotation pdfAnnotation, AnnotObjec
703734
PdfDictionary bs = pdfLineAnnotation.getBorderStyle();
704735
if (bs != null) {
705736
addBorderStyleAttributes(annot, bs.getAsNumber(PdfName.W),
706-
bs.getAsString(PdfName.Dashed), bs.getAsString(PdfName.Style));
737+
bs.getAsArray(PdfName.D), bs.getAsName(PdfName.S));
707738
}
708739

709740
annot.setContents(pdfAnnotation.getContents());
@@ -715,6 +746,18 @@ private static void createLineAnnotation(PdfAnnotation pdfAnnotation, AnnotObjec
715746
private static void createLinkAnnotation(PdfAnnotation pdfAnnotation, AnnotObject annot) {
716747
PdfLinkAnnotation pdfLinkAnnotation = (PdfLinkAnnotation) pdfAnnotation;
717748

749+
if (pdfLinkAnnotation.getBorderStyle() != null) {
750+
annot.addAttribute(XfdfConstants.STYLE, pdfLinkAnnotation.getBorderStyle().getAsString(PdfName.S));
751+
}
752+
if (pdfLinkAnnotation.getHighlightMode() != null) {
753+
annot.addAttribute(XfdfConstants.HIGHLIGHT,
754+
XfdfObjectUtils.getHighlightFullValue(pdfLinkAnnotation.getHighlightMode()));
755+
}
756+
if (pdfLinkAnnotation.getQuadPoints() != null) {
757+
annot.addAttribute(new AttributeObject(XfdfConstants.COORDS,
758+
XfdfObjectUtils.convertQuadPointsToCoordsString(pdfLinkAnnotation.getQuadPoints().toFloatArray())));
759+
}
760+
718761
if (pdfLinkAnnotation.getContents() != null) {
719762
annot.setContents(pdfLinkAnnotation.getContents());
720763
}
@@ -796,17 +839,20 @@ private static void createPolyGeomAnnotation(PdfAnnotation pdfAnnotation, AnnotO
796839
PdfDictionary bs = pdfPolyGeomAnnotation.getBorderStyle();
797840
if (bs != null) {
798841
addBorderStyleAttributes(annot, bs.getAsNumber(PdfName.W),
799-
bs.getAsString(PdfName.Dashed), bs.getAsString(PdfName.Style));
842+
bs.getAsArray(PdfName.D), bs.getAsName(PdfName.S));
800843
}
801844

802845
if (pdfPolyGeomAnnotation.getBorderEffect() != null) {
803-
//TODO DEVSIX-4133 map intensity to border effect dictionary's I key
804-
//annot.addAttribute(new AttributeObject("intensity", pdfCircleAnnotation.getBorderEffect().getAsString()));
805-
annot.addAttribute(XfdfConstants.STYLE, pdfPolyGeomAnnotation.getBorderEffect().getAsString(PdfName.Style));
846+
annot.addAttribute(XfdfConstants.INTENSITY, pdfPolyGeomAnnotation.getBorderEffect().getAsNumber(PdfName.I));
847+
if (annot.getAttribute(XfdfConstants.STYLE) == null) {
848+
annot.addAttribute(XfdfConstants.STYLE, XfdfObjectUtils.getStyleFullValue(
849+
pdfPolyGeomAnnotation.getBorderEffect().getAsName(PdfName.S)));
850+
}
806851
}
807852

808853
if (pdfPolyGeomAnnotation.getInteriorColor() != null) {
809-
annot.addAttribute(new AttributeObject(XfdfConstants.INTERIOR_COLOR, XfdfObjectUtils.convertColorToString(pdfPolyGeomAnnotation.getInteriorColor())));
854+
annot.addAttribute(new AttributeObject(XfdfConstants.INTERIOR_COLOR,
855+
XfdfObjectUtils.convertColorToString(pdfPolyGeomAnnotation.getInteriorColor())));
810856
}
811857
if (pdfPolyGeomAnnotation.getIntent() != null) {
812858
annot.addAttribute(new AttributeObject(XfdfConstants.INTENT, pdfPolyGeomAnnotation.getIntent().getValue()));

0 commit comments

Comments
 (0)