Java (481)
This article demonstrates how to detect the required form fields in an existing PDF document using Spire.PDF for Java.
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;
public class DetectRequiredFields {
public static void main(String[] args) {
//load a PDF file
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Fields.pdf");
//get form widget from the PDF document.
PdfFormWidget formWidget = (PdfFormWidget)doc.getForm();
//loop through the fields widget
for (int i = 0; i < formWidget.getFieldsWidget().getList().getCapacity(); i++) {
//get the specific field
PdfField field = (PdfField) formWidget.getFieldsWidget().getList().get_Item(i);
//get the field name
String fieldName = field.getName();
//determine if the field is required
boolean isRequired = field.getRequired();
if (isRequired){
//print the required field
System.out.println(fieldName + " is required");
}
}
}
}

Published in
Form Fields
Tagged under
This article demonstrates how to add combo box, check box, text, picture, date picker and drop-down list content controls to a Word document using Spire.Doc for Java.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.Date;
public class ContentControls {
public static void main(String[] args){
//create a new Word document
Document document = new Document();
Section section = document.addSection();
Paragraph paragraph = section.addParagraph();
TextRange txtRange = paragraph.appendText("The following example shows how to add content controls in a Word document.");
section.addParagraph();
//add combo box content control
paragraph = section.addParagraph();
txtRange = paragraph.appendText("Add Combo Box Content Control: ");
txtRange.getCharacterFormat().setItalic(true);
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
paragraph.getChildObjects().add(sd);
sd.getSDTProperties().setSDTType(SdtType.Combo_Box);
sd.getSDTProperties().setAlias("ComboBox");
sd.getSDTProperties().setTag("ComboBox");
SdtComboBox cb = new SdtComboBox();
cb.getListItems().add(new SdtListItem("Spire.Doc"));
cb.getListItems().add(new SdtListItem("Spire.XLS"));
cb.getListItems().add(new SdtListItem("Spire.PDF"));
sd.getSDTProperties().setControlProperties(cb);
TextRange rt = new TextRange(document);
rt.setText(cb.getListItems().get(0).getDisplayText());
sd.getSDTContent().getChildObjects().add(rt);
section.addParagraph();
//Add checkbox content control
paragraph = section.addParagraph();
txtRange = paragraph.appendText("Add Check Box Content Control: ");
txtRange.getCharacterFormat().setItalic(true);
sd = new StructureDocumentTagInline(document);
paragraph.getChildObjects().add(sd);
sd.getSDTProperties().setSDTType(SdtType.Check_Box);
sd.getSDTProperties().setAlias("CheckBox");
sd.getSDTProperties().setTag("CheckBox");
SdtCheckBox scb = new SdtCheckBox();
sd.getSDTProperties().setControlProperties(scb);
rt = new TextRange(document);
sd.getChildObjects().add(rt);
scb.setChecked(true);
section.addParagraph();
//add text content control
paragraph = section.addParagraph();
txtRange = paragraph.appendText("Add Text Content Control: ");
txtRange.getCharacterFormat().setItalic(true);
sd = new StructureDocumentTagInline(document);
paragraph.getChildObjects().add(sd);
sd.getSDTProperties().setSDTType(SdtType.Text);
sd.getSDTProperties().setAlias("Text");
sd.getSDTProperties().setTag("Text");
SdtText text = new SdtText(true);
text.isMultiline(true);
sd.getSDTProperties().setControlProperties(text);
rt = new TextRange(document);
rt.setText("Text");
sd.getSDTContent().getChildObjects().add(rt);
section.addParagraph();
//add picture content control
paragraph = section.addParagraph();
txtRange = paragraph.appendText("Add Picture Content Control: ");
txtRange.getCharacterFormat().setItalic(true);
sd = new StructureDocumentTagInline(document);
paragraph.getChildObjects().add(sd);
sd.getSDTProperties().setControlProperties(new SdtPicture());
sd.getSDTProperties().setAlias("Picture");
sd.getSDTProperties().setTag("Picture");
DocPicture pic = new DocPicture(document);
pic.setWidth(10f);
pic.setHeight(10f);
pic.loadImage("logo.png");
sd.getSDTContent().getChildObjects().add(pic);
section.addParagraph();
//add date picker content control
paragraph = section.addParagraph();
txtRange = paragraph.appendText("Add Date Picker Content Control: ");
txtRange.getCharacterFormat().setItalic(true);
sd = new StructureDocumentTagInline(document);
paragraph.getChildObjects().add(sd);
sd.getSDTProperties().setSDTType(SdtType.Date_Picker);
sd.getSDTProperties().setAlias("Date");
sd.getSDTProperties().setTag("Date");
SdtDate date = new SdtDate();
date.setCalendarType(CalendarType.Default);
date.setDateFormat("yyyy.MM.dd");
date.setFullDate(new Date());
sd.getSDTProperties().setControlProperties(date);
rt = new TextRange(document);
rt.setText("2018.12.25");
sd.getSDTContent().getChildObjects().add(rt);
section.addParagraph();
//add drop-down list content control
paragraph = section.addParagraph();
txtRange = paragraph.appendText("Add Drop-Down List Content Control: ");
txtRange.getCharacterFormat().setItalic(true);
sd = new StructureDocumentTagInline(document);
paragraph.getChildObjects().add(sd);
sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
sd.getSDTProperties().setAlias("DropDownList");
sd.getSDTProperties().setTag("DropDownList");
SdtDropDownList sddl = new SdtDropDownList();
sddl.getListItems().add(new SdtListItem("Harry"));
sddl.getListItems().add(new SdtListItem("Jerry"));
sd.getSDTProperties().setControlProperties(sddl);
rt = new TextRange(document);
rt.setText(sddl.getListItems().get(0).getDisplayText());
sd.getSDTContent().getChildObjects().add(rt);
//save the resultant document
document.saveToFile("addContentControls.docx", FileFormat.Docx_2013);
}
}
Output:

Published in
Document Operation
Tagged under
This article will show you how to set the wrapping style to adjust the position of the image in Java applications with the help of Spire.Doc for Java.
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.TextWrappingType;
import com.spire.doc.fields.DocPicture;
public class ImageWrappingStyle {
public static void main(String[] args) throws Exception {
Document doc = new Document();
doc.loadFromFile("Sample.docx");
Section sec = doc.getSections().get(0);
Paragraph para = sec.getParagraphs().get(0);
DocPicture picture = para.appendPicture("logo.png");
//Set image width and height
picture.setWidth(150f);
picture.setHeight(125f);
//Set text wrapping style to Behind
picture.setTextWrappingStyle(TextWrappingStyle.Behind);
picture.setTextWrappingType(TextWrappingType.Both);
//Save the document to file
doc.saveToFile("Output/WrapStyle.docx");
doc.close();
}
}
Effective screenshot after setting the wrapping style for image:

Published in
Image and Shape
Tagged under