@@ -47,9 +47,11 @@ This file is part of the iText (R) project.
47
47
import com .itextpdf .forms .fields .PdfFormField ;
48
48
import com .itextpdf .forms .fields .PdfTextFormField ;
49
49
import com .itextpdf .io .LogMessageConstant ;
50
+ import com .itextpdf .io .font .PdfEncodings ;
50
51
import com .itextpdf .io .font .constants .StandardFonts ;
51
52
import com .itextpdf .io .source .ByteArrayOutputStream ;
52
53
import com .itextpdf .kernel .colors .ColorConstants ;
54
+ import com .itextpdf .kernel .font .PdfFont ;
53
55
import com .itextpdf .kernel .font .PdfFontFactory ;
54
56
import com .itextpdf .kernel .geom .Rectangle ;
55
57
import com .itextpdf .kernel .pdf .PdfArray ;
@@ -76,6 +78,7 @@ This file is part of the iText (R) project.
76
78
import org .junit .Test ;
77
79
import org .junit .experimental .categories .Category ;
78
80
81
+ import java .io .ByteArrayInputStream ;
79
82
import java .io .IOException ;
80
83
import java .util .Map ;
81
84
@@ -625,6 +628,158 @@ public void formRegenerateWithInvalidDefaultAppearance01() throws IOException, I
625
628
}
626
629
}
627
630
631
+ @ Test
632
+ //DEVSIX-2069
633
+ //Create a document with formfields and paragraphs in both fonts, and fill them before closing the document
634
+ public void fillFieldWithHebrewCase1 () throws IOException , InterruptedException {
635
+ String testName = "fillFieldWithHebrewCase1" ;
636
+ String outPdf = destinationFolder + testName + ".pdf" ;
637
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
638
+
639
+ PdfWriter writer = new PdfWriter (outPdf );
640
+ PdfDocument pdfDoc = new PdfDocument (writer );
641
+ Document document = new Document (pdfDoc );
642
+
643
+ PdfFont hebrew = PdfFontFactory .createFont (sourceFolder + "OpenSansHebrew-Regular.ttf" , PdfEncodings .IDENTITY_H , true );
644
+ hebrew .setSubset (false );
645
+ PdfFont sileot = PdfFontFactory .createFont (sourceFolder + "SILEOT.ttf" , PdfEncodings .IDENTITY_H , true );
646
+ sileot .setSubset (false );
647
+
648
+ PdfAcroForm form = PdfAcroForm .getAcroForm (pdfDoc , true );
649
+
650
+
651
+ String text = "שלום וברכה" ;
652
+ createAcroForm (pdfDoc , form , hebrew , text , 0 );
653
+ createAcroForm (pdfDoc , form , sileot , text , 3 );
654
+
655
+ addParagraph (document , text , hebrew );
656
+ addParagraph (document , text , sileot );
657
+
658
+ pdfDoc .close ();
659
+
660
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
661
+ }
662
+
663
+ @ Test
664
+ //DEVSIX-2069, cmp_file will have to be changed after fix
665
+ //Create a document with formfields and paragraphs in both fonts, and fill them after closing and reopening the document
666
+ public void fillFieldWithHebrewCase2 () throws IOException , InterruptedException {
667
+ String testName = "fillFieldWithHebrewCase2" ;
668
+ String outPdf = destinationFolder + testName + ".pdf" ;
669
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
670
+
671
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
672
+
673
+ PdfWriter writer = new PdfWriter (baos );
674
+ PdfDocument pdfDoc = new PdfDocument (writer );
675
+ Document document = new Document (pdfDoc );
676
+
677
+ PdfFont hebrew = PdfFontFactory .createFont (sourceFolder + "OpenSansHebrew-Regular.ttf" , PdfEncodings .IDENTITY_H , true );
678
+ hebrew .setSubset (false );
679
+ PdfFont sileot = PdfFontFactory .createFont (sourceFolder + "SILEOT.ttf" , PdfEncodings .IDENTITY_H , true );
680
+ sileot .setSubset (false );
681
+
682
+ PdfAcroForm form = PdfAcroForm .getAcroForm (pdfDoc , true );
683
+
684
+ createAcroForm (pdfDoc , form , hebrew , null , 0 );
685
+ createAcroForm (pdfDoc , form , sileot , null , 3 );
686
+
687
+ String text = "שלום וברכה" ;
688
+ addParagraph (document , text , hebrew );
689
+ addParagraph (document , text , sileot );
690
+
691
+ pdfDoc .close ();
692
+
693
+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (new ByteArrayInputStream (baos .toByteArray ())), new PdfWriter (outPdf ));
694
+ fillAcroForm (pdfDocument , text );
695
+ pdfDocument .close ();
696
+
697
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
698
+ }
699
+
700
+ @ Test
701
+ //DEVSIX-2069
702
+ //Create a document with formfields in both fonts, and fill them before closing the document
703
+ public void fillFieldWithHebrewCase3 () throws IOException , InterruptedException {
704
+ String testName = "fillFieldWithHebrewCase3" ;
705
+ String outPdf = destinationFolder + testName + ".pdf" ;
706
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
707
+
708
+ PdfWriter writer = new PdfWriter (outPdf );
709
+ PdfDocument pdfDoc = new PdfDocument (writer );
710
+
711
+ PdfFont hebrew = PdfFontFactory .createFont (sourceFolder + "OpenSansHebrew-Regular.ttf" , PdfEncodings .IDENTITY_H , true );
712
+ hebrew .setSubset (false );
713
+ PdfFont sileot = PdfFontFactory .createFont (sourceFolder + "SILEOT.ttf" , PdfEncodings .IDENTITY_H , true );
714
+ sileot .setSubset (false );
715
+
716
+ PdfAcroForm form = PdfAcroForm .getAcroForm (pdfDoc , true );
717
+
718
+ String text = "שלום וברכה" ;
719
+ createAcroForm (pdfDoc , form , hebrew , text , 0 );
720
+ createAcroForm (pdfDoc , form , sileot , text , 3 );
721
+
722
+ pdfDoc .close ();
723
+
724
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
725
+ }
726
+
727
+ @ Test
728
+ //DEVSIX-2069, cmp_file will have to be changed after fix
729
+ //Create a document with formfields in both fonts, and fill them after closing and reopening the document
730
+ public void fillFieldWithHebrewCase4 () throws IOException , InterruptedException {
731
+ String testName = "fillFieldWithHebrewCase4" ;
732
+ String outPdf = destinationFolder + testName + ".pdf" ;
733
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
734
+
735
+ ByteArrayOutputStream baos = new ByteArrayOutputStream ();
736
+
737
+ PdfWriter writer = new PdfWriter (baos );
738
+ PdfDocument pdfDoc = new PdfDocument (writer );
739
+
740
+ PdfFont hebrew = PdfFontFactory .createFont (sourceFolder + "OpenSansHebrew-Regular.ttf" , PdfEncodings .IDENTITY_H , true );
741
+ hebrew .setSubset (false );
742
+ PdfFont sileot = PdfFontFactory .createFont (sourceFolder + "SILEOT.ttf" , PdfEncodings .IDENTITY_H , true );
743
+ sileot .setSubset (false );
744
+
745
+ PdfAcroForm form = PdfAcroForm .getAcroForm (pdfDoc , true );
746
+
747
+ createAcroForm (pdfDoc , form , hebrew , null , 0 );
748
+ createAcroForm (pdfDoc , form , sileot , null , 3 );
749
+
750
+ pdfDoc .close ();
751
+
752
+ String text = "שלום וברכה" ;
753
+ PdfDocument pdfDocument = new PdfDocument (new PdfReader (new ByteArrayInputStream (baos .toByteArray ())), new PdfWriter (outPdf ));
754
+ fillAcroForm (pdfDocument , text );
755
+ pdfDocument .close ();
756
+
757
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
758
+ }
759
+
760
+ private void createAcroForm (PdfDocument pdfDoc , PdfAcroForm form , PdfFont font , String text , int offSet ){
761
+ for (int x = offSet ; x < (offSet + 3 ); x ++) {
762
+ Rectangle rect = new Rectangle (100 + (30 * x ), 100 + (100 * x ), 55 , 30 );
763
+ PdfFormField field = PdfFormField .createText (pdfDoc , rect , "f-" + x , "" , font , 12.0f );
764
+ field .setJustification (PdfFormField .ALIGN_RIGHT );
765
+ if (text != null ) {
766
+ field .setValue (text );
767
+ }
768
+ form .addField (field );
769
+ }
770
+ }
771
+
772
+ private void addParagraph (Document document , String text , PdfFont font ){
773
+ document .add (new Paragraph ("Hello world " ).add (text ).setFont (font ));
774
+ }
775
+
776
+ private void fillAcroForm (PdfDocument pdfDocument , String text ){
777
+ PdfAcroForm acroForm = PdfAcroForm .getAcroForm (pdfDocument , false );
778
+ for (PdfFormField field : acroForm .getFormFields ().values ()) {
779
+ field .setValue (text );
780
+ }
781
+ }
782
+
628
783
@ Test
629
784
//DEVSIX-2393
630
785
//TODO change cmp file after fix
0 commit comments