@@ -56,15 +56,14 @@ This file is part of the iText (R) project.
56
56
import org .xml .sax .SAXException ;
57
57
58
58
import javax .xml .parsers .ParserConfigurationException ;
59
- import java .io .IOException ;
60
59
import java .io .InputStream ;
61
60
import java .util .ArrayList ;
62
61
import java .util .List ;
63
62
import java .util .StringTokenizer ;
64
63
65
64
public class XfdfObjectFactory {
66
65
67
- private static Logger logger = LoggerFactory .getLogger (XfdfObjectFactory .class );
66
+ private static final Logger logger = LoggerFactory .getLogger (XfdfObjectFactory .class );
68
67
69
68
/**
70
69
* Extracts data from pdf document acroform and annotations into XfdfObject.
@@ -124,10 +123,9 @@ public XfdfObject createXfdfObject(PdfDocument document, String filename) {
124
123
* @return XfdfObject containing original xfdf data.
125
124
* @throws ParserConfigurationException if a XfdfObject cannot be created which satisfies the configuration
126
125
* requested.
127
- * @throws IOException if any I/O issue occurs.
128
126
* @throws SAXException if any parse errors occurs.
129
127
*/
130
- public XfdfObject createXfdfObject (InputStream xfdfInputStream ) throws ParserConfigurationException , IOException , SAXException {
128
+ public XfdfObject createXfdfObject (InputStream xfdfInputStream ) throws ParserConfigurationException , SAXException {
131
129
XfdfObject xfdfObject = new XfdfObject ();
132
130
133
131
Document document = XfdfFileUtils .createXfdfDocumentFromStream (xfdfInputStream );
@@ -209,6 +207,8 @@ private static boolean isAnnotSupported(String nodeName) {
209
207
XfdfConstants .SQUARE .equalsIgnoreCase (nodeName ) ||
210
208
XfdfConstants .POLYLINE .equalsIgnoreCase (nodeName ) ||
211
209
XfdfConstants .POLYGON .equalsIgnoreCase (nodeName ) ||
210
+ XfdfConstants .STAMP .equalsIgnoreCase (nodeName ) ||
211
+ // XfdfConstants.FREETEXT.equalsIgnoreCase(nodeName) ||
212
212
XfdfConstants .LINE .equalsIgnoreCase (nodeName );
213
213
}
214
214
@@ -233,12 +233,12 @@ private void visitAnnotationNode(Node currentNode, AnnotsObject annotsObject) {
233
233
for (int i = 0 ; i < attributes .getLength (); i ++) {
234
234
addAnnotObjectAttribute (annotObject , attributes .item (i ));
235
235
}
236
- visitAnnotationInnerNodes (annotObject , currentNode );
236
+ visitAnnotationInnerNodes (annotObject , currentNode , annotsObject );
237
237
annotsObject .addAnnot (annotObject );
238
238
}
239
239
}
240
240
241
- private void visitAnnotationInnerNodes (AnnotObject annotObject , Node annotNode ) {
241
+ private void visitAnnotationInnerNodes (AnnotObject annotObject , Node annotNode , AnnotsObject annotsObject ) {
242
242
NodeList children = annotNode .getChildNodes ();
243
243
244
244
for (int temp = 0 ; temp < children .getLength (); temp ++) {
@@ -256,6 +256,10 @@ private void visitAnnotationInnerNodes(AnnotObject annotObject, Node annotNode)
256
256
if (XfdfConstants .VERTICES .equalsIgnoreCase (node .getNodeName ())) {
257
257
visitVerticesSubelement (node , annotObject );
258
258
}
259
+ if (isAnnotationSubtype (node .getNodeName ()) &&
260
+ isAnnotSupported (node .getNodeName ())) {
261
+ visitAnnotationNode (node , annotsObject );
262
+ }
259
263
}
260
264
}
261
265
}
@@ -330,7 +334,18 @@ private void addAnnotObjectAttribute(AnnotObject annotObject, Node attributeNode
330
334
case XfdfConstants .REPLY_TYPE :
331
335
case XfdfConstants .OPEN :
332
336
case XfdfConstants .COORDS :
337
+ case XfdfConstants .INTENT :
338
+ case XfdfConstants .INTERIOR_COLOR :
339
+ case XfdfConstants .HEAD :
340
+ case XfdfConstants .TAIL :
333
341
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 :
334
349
annotObject .addAttribute (new AttributeObject (attributeName , attributeNode .getNodeValue ()));
335
350
break ;
336
351
default : logger .warn (IoLogMessageConstant .XFDF_UNSUPPORTED_ANNOTATION_ATTRIBUTE );
@@ -511,18 +526,25 @@ private static void addMarkupAnnotationAttributes(AnnotObject annot, PdfMarkupAn
511
526
}
512
527
513
528
private static void addBorderStyleAttributes (AnnotObject annotObject , PdfNumber width ,
514
- PdfString dashes , PdfString style ) {
529
+ PdfArray dashes , PdfName style ) {
515
530
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 ) );
518
533
}
519
534
520
535
private static void createTextMarkupAnnotation (PdfAnnotation pdfAnnotation , AnnotObject annot , int pageNumber ) {
521
536
PdfTextMarkupAnnotation pdfTextMarkupAnnotation = (PdfTextMarkupAnnotation ) pdfAnnotation ;
522
537
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
+ }
525
542
543
+ if (PdfTextMarkupAnnotation .MarkupUnderline .equals (pdfTextMarkupAnnotation .getSubtype ()) &&
544
+ pdfTextMarkupAnnotation .getIntent () != null ) {
545
+ annot .addAttribute (new AttributeObject (XfdfConstants .INTENT ,
546
+ pdfTextMarkupAnnotation .getIntent ().getValue ()));
547
+ }
526
548
527
549
if (pdfTextMarkupAnnotation .getContents () != null ) {
528
550
annot .setContents (pdfTextMarkupAnnotation .getContents ());
@@ -559,22 +581,24 @@ private static void createCircleAnnotation(PdfAnnotation pdfAnnotation, AnnotObj
559
581
PdfDictionary bs = pdfCircleAnnotation .getBorderStyle ();
560
582
if (bs != null ) {
561
583
addBorderStyleAttributes (annot , bs .getAsNumber (PdfName .W ),
562
- bs .getAsString (PdfName .Dashed ), bs .getAsString (PdfName .Style ));
584
+ bs .getAsArray (PdfName .D ), bs .getAsName (PdfName .S ));
563
585
}
564
586
565
587
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
+ }
570
593
}
571
594
572
595
if (pdfCircleAnnotation .getInteriorColor () != null && pdfCircleAnnotation .getInteriorColor ().getColorValue () != null ) {
573
596
annot .addAttribute (new AttributeObject (XfdfConstants .INTERIOR_COLOR , XfdfObjectUtils .convertColorToString (pdfCircleAnnotation .getInteriorColor ().getColorValue ())));
574
597
}
575
598
576
599
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 ())));
578
602
}
579
603
580
604
annot .setContents (pdfAnnotation .getContents ());
@@ -589,20 +613,23 @@ private static void createSquareAnnotation(PdfAnnotation pdfAnnotation, AnnotObj
589
613
PdfDictionary bs = pdfSquareAnnotation .getBorderStyle ();
590
614
if (bs != null ) {
591
615
addBorderStyleAttributes (annot , bs .getAsNumber (PdfName .W ),
592
- bs .getAsString (PdfName .Dashed ), bs .getAsString (PdfName .Style ));
616
+ bs .getAsArray (PdfName .D ), bs .getAsName (PdfName .S ));
593
617
}
594
618
595
619
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
+ }
599
625
}
600
626
601
627
if (pdfSquareAnnotation .getInteriorColor () != null && pdfSquareAnnotation .getInteriorColor ().getColorValue () != null ) {
602
628
annot .addAttribute (new AttributeObject (XfdfConstants .INTERIOR_COLOR , XfdfObjectUtils .convertColorToString (pdfSquareAnnotation .getInteriorColor ().getColorValue ())));
603
629
}
604
630
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 ())));
606
633
}
607
634
608
635
annot .setContents (pdfAnnotation .getContents ());
@@ -615,7 +642,9 @@ private static void createStampAnnotation(PdfAnnotation pdfAnnotation, AnnotObje
615
642
PdfStampAnnotation pdfStampAnnotation = (PdfStampAnnotation ) pdfAnnotation ;
616
643
617
644
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
+ }
619
648
620
649
if (pdfStampAnnotation .getContents () != null ) {
621
650
annot .setContents (pdfStampAnnotation .getContents ());
@@ -640,12 +669,14 @@ private static void createFreeTextAnnotation(PdfAnnotation pdfAnnotation, AnnotO
640
669
PdfDictionary bs = pdfFreeTextAnnotation .getBorderStyle ();
641
670
if (bs != null ) {
642
671
addBorderStyleAttributes (annot , bs .getAsNumber (PdfName .W ),
643
- bs .getAsString (PdfName .Dashed ), bs .getAsString (PdfName .Style ));
672
+ bs .getAsArray (PdfName .D ), bs .getAsName (PdfName .S ));
644
673
}
645
674
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 ()))));
649
680
if (pdfFreeTextAnnotation .getIntent () != null ) {
650
681
annot .addAttribute (new AttributeObject (XfdfConstants .INTENT , pdfFreeTextAnnotation .getIntent ().getValue ()));
651
682
}
@@ -703,7 +734,7 @@ private static void createLineAnnotation(PdfAnnotation pdfAnnotation, AnnotObjec
703
734
PdfDictionary bs = pdfLineAnnotation .getBorderStyle ();
704
735
if (bs != null ) {
705
736
addBorderStyleAttributes (annot , bs .getAsNumber (PdfName .W ),
706
- bs .getAsString (PdfName .Dashed ), bs .getAsString (PdfName .Style ));
737
+ bs .getAsArray (PdfName .D ), bs .getAsName (PdfName .S ));
707
738
}
708
739
709
740
annot .setContents (pdfAnnotation .getContents ());
@@ -715,6 +746,18 @@ private static void createLineAnnotation(PdfAnnotation pdfAnnotation, AnnotObjec
715
746
private static void createLinkAnnotation (PdfAnnotation pdfAnnotation , AnnotObject annot ) {
716
747
PdfLinkAnnotation pdfLinkAnnotation = (PdfLinkAnnotation ) pdfAnnotation ;
717
748
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
+
718
761
if (pdfLinkAnnotation .getContents () != null ) {
719
762
annot .setContents (pdfLinkAnnotation .getContents ());
720
763
}
@@ -796,17 +839,20 @@ private static void createPolyGeomAnnotation(PdfAnnotation pdfAnnotation, AnnotO
796
839
PdfDictionary bs = pdfPolyGeomAnnotation .getBorderStyle ();
797
840
if (bs != null ) {
798
841
addBorderStyleAttributes (annot , bs .getAsNumber (PdfName .W ),
799
- bs .getAsString (PdfName .Dashed ), bs .getAsString (PdfName .Style ));
842
+ bs .getAsArray (PdfName .D ), bs .getAsName (PdfName .S ));
800
843
}
801
844
802
845
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
+ }
806
851
}
807
852
808
853
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 ())));
810
856
}
811
857
if (pdfPolyGeomAnnotation .getIntent () != null ) {
812
858
annot .addAttribute (new AttributeObject (XfdfConstants .INTENT , pdfPolyGeomAnnotation .getIntent ().getValue ()));
0 commit comments