Skip to content

Commit 069247d

Browse files
kevinwillems1993BlackEgoist
authored andcommitted
Added tests for DEVSIX-2069
Changed font for testing from ArialUni to SILEOT.ttf. Arial Unicode is 10+ MB and not freely available. Review remark, changing assertion to shorter syntax and define correct file name for "diff_".pdf. Fixed small issue with Assertion
1 parent dfbb6e9 commit 069247d

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTest.java

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.forms.fields.PdfFormField;
4848
import com.itextpdf.forms.fields.PdfTextFormField;
4949
import com.itextpdf.io.LogMessageConstant;
50+
import com.itextpdf.io.font.PdfEncodings;
5051
import com.itextpdf.io.font.constants.StandardFonts;
5152
import com.itextpdf.io.source.ByteArrayOutputStream;
5253
import com.itextpdf.kernel.colors.ColorConstants;
54+
import com.itextpdf.kernel.font.PdfFont;
5355
import com.itextpdf.kernel.font.PdfFontFactory;
5456
import com.itextpdf.kernel.geom.Rectangle;
5557
import com.itextpdf.kernel.pdf.PdfArray;
@@ -76,6 +78,7 @@ This file is part of the iText (R) project.
7678
import org.junit.Test;
7779
import org.junit.experimental.categories.Category;
7880

81+
import java.io.ByteArrayInputStream;
7982
import java.io.IOException;
8083
import java.util.Map;
8184

@@ -625,6 +628,158 @@ public void formRegenerateWithInvalidDefaultAppearance01() throws IOException, I
625628
}
626629
}
627630

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+
628783
@Test
629784
//DEVSIX-2393
630785
//TODO change cmp file after fix
Binary file not shown.

0 commit comments

Comments
 (0)