本文整理汇总了Java中org.apache.pdfbox.pdmodel.interactive.form.PDField.setValue方法的典型用法代码示例。如果您正苦于以下问题:Java PDField.setValue方法的具体用法?Java PDField.setValue怎么用?Java PDField.setValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.pdfbox.pdmodel.interactive.form.PDField
的用法示例。
在下文中一共展示了PDField.setValue方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testFillLikeStDdt
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/39720305/ufffd-is-not-available-in-this-fonts-encoding-winansiencoding">
* U+FFFD is not available in this font's encoding: WinAnsiEncoding
* </a>
* <p>
* The issue cannot be reproduced.
* </p>
*/
@Test
public void testFillLikeStDdt() throws IOException
{
try ( InputStream originalStream = getClass().getResourceAsStream("FillFormField.pdf") )
{
PDDocument pdfDocument = PDDocument.load(originalStream);
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
if (acroForm != null)
{
List<PDField> fields = acroForm.getFields();
for (PDField field : fields) {
switch (field.getPartialName()) {
case "Title" /*"devices"*/:
field.setValue("Ger�t");
field.setReadOnly(true);
break;
}
}
acroForm.flatten(fields, true);
}
pdfDocument.save(new File(RESULT_FOLDER, "FillFormFieldStDdt.pdf"));
pdfDocument.close();
}
}
示例2: test
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
@Test
public void test() throws COSVisitorException, IOException
{
byte[] template = generateSimpleTemplate();
Files.write(new File(RESULT_FOLDER, "template.pdf").toPath(), template);
try ( PDDocument finalDoc = new PDDocument(); )
{
List<PDField> fields = new ArrayList<PDField>();
int i = 0;
for (String value : new String[]{"eins", "zwei"})
{
PDDocument doc = new PDDocument().load(new ByteArrayInputStream(template));
PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("SampleField");
field.setValue(value);
field.setPartialName("SampleField" + i++);
List<PDPage> pages = docCatalog.getAllPages();
finalDoc.addPage(pages.get(0));
fields.add(field);
}
PDAcroForm finalForm = new PDAcroForm(finalDoc);
finalDoc.getDocumentCatalog().setAcroForm(finalForm);
finalForm.setFields(fields);
finalDoc.save(new File(RESULT_FOLDER, "form-two-templates.pdf"));
}
}
示例3: setField
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
private PDAcroForm setField( PDAcroForm acroForm, String name, String value ) throws IOException
{
PDField field = acroForm.getField( name );
if ( field == null ) throw new RuntimeException( "No field found with name:" + name );
field.setValue( value );
return acroForm;
}
示例4: testFillAfterFillAndSign
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/33011970/pdfbox-1-8-10-fill-and-sign-document-filling-again-fails">
* PDFBox 1.8.10: Fill and Sign Document, Filling again fails
* </a>
* <br/>
* <a href="https://www.dropbox.com/s/xf5pb0ng8k9zd4i/doc_v2.pdf?dl=0">doc_v2.pdf</a>
* <br/>
* <a href="https://www.dropbox.com/s/s8295tfyjpe1l4l/doc_v2_fillsigned.pdf?dl=0">doc_v2_fillsigned.pdf</a>
* <p>
* The cause of the problem is that somehow during the original filling and signing the fonts in the default
* resources of the interactive form dictionary got lost.
* </p><p>
* In the original document doc_v2.pdf the interactive form dictionary contains entries for ZaDb and Helv
* in the Font dictionary in the default resources DR dictionary; so form filli-in works, cf.
* {@link #testFillWithoutFillAndSign()}.
* </p><p>
* In contrast in the interactive form dictionary of the filled and signed document doc_v2_fillsigned.pdf
* the Font dictionary in the default resources DR dictionary is missing; so form fill-in fails, cf.
* {@link #testFillAfterFillAndSign()}.
* </p>
*/
@Test
public void testFillAfterFillAndSign() throws COSVisitorException, IOException
{
File currentDocument = new File("src/test/resources/mkl/testarea/pdfbox1/form/doc_v2_fillsigned.pdf");
File newDocument = new File(RESULT_FOLDER, "doc_v2_fillsigned_filled.pdf");
String fieldName = "New Emergency Contact";
String value = "test";
PDDocument doc = null;
try (FileOutputStream fos = new FileOutputStream(newDocument))
{
Files.copy(currentDocument.toPath(), fos);
doc = PDDocument.load(currentDocument);
PDDocumentCatalog catalog = doc.getDocumentCatalog();
catalog.getCOSObject().setNeedToBeUpdate(true);
catalog.getPages().getCOSObject().setNeedToBeUpdate(true);
PDAcroForm form = catalog.getAcroForm();
form.getCOSObject().setNeedToBeUpdate(true);
form.getDefaultResources().getCOSObject().setNeedToBeUpdate(true);
PDField field = form.getField(fieldName);
field.setValue(value); // here the exception occurs.
// What should happen afterwards:
field.getCOSObject().setNeedToBeUpdate(true);
field.getAcroForm().getCOSObject().setNeedToBeUpdate(true);
((COSDictionary) field.getDictionary().getDictionaryObject("AP")).getDictionaryObject("N").setNeedToBeUpdate(true);
try (FileInputStream fis = new FileInputStream(newDocument))
{
doc.saveIncremental(fis, fos);
}
}
finally
{
if (null != doc)
{
doc.close();
doc = null;
}
}
}
示例5: testFillWithoutFillAndSign
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
/**
* @see #testFillAfterFillAndSign()
*/
@Test
public void testFillWithoutFillAndSign() throws COSVisitorException, IOException
{
File currentDocument = new File("src/test/resources/mkl/testarea/pdfbox1/form/doc_v2.pdf");
File newDocument = new File(RESULT_FOLDER, "doc_v2_filled.pdf");
String fieldName = "New Emergency Contact";
String value = "test";
PDDocument doc = null;
try (FileOutputStream fos = new FileOutputStream(newDocument))
{
Files.copy(currentDocument.toPath(), fos);
doc = PDDocument.load(currentDocument);
PDDocumentCatalog catalog = doc.getDocumentCatalog();
catalog.getCOSObject().setNeedToBeUpdate(true);
catalog.getPages().getCOSObject().setNeedToBeUpdate(true);
PDAcroForm form = catalog.getAcroForm();
form.getCOSObject().setNeedToBeUpdate(true);
form.getDefaultResources().getCOSObject().setNeedToBeUpdate(true);
PDField field = form.getField(fieldName);
field.setValue(value); // here the exception occurs.
// What should happen afterwards:
field.getCOSObject().setNeedToBeUpdate(true);
field.getAcroForm().getCOSObject().setNeedToBeUpdate(true);
((COSDictionary) field.getDictionary().getDictionaryObject("AP")).getDictionaryObject("N").setNeedToBeUpdate(true);
try (FileInputStream fis = new FileInputStream(newDocument))
{
doc.saveIncremental(fis, fos);
}
}
finally
{
if (null != doc)
{
doc.close();
doc = null;
}
}
}
示例6: loadAndPopulateTemplate
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private PDDocument loadAndPopulateTemplate(Tarjeta order) throws Exception {
PDDocument pdfDocument = PDDocument.load(new ByteArrayInputStream(pdfAsBytes));
PDAcroForm pdfForm = pdfDocument.getDocumentCatalog().getAcroForm();
List<PDField> fields = pdfForm.getFields();
for (PDField field : fields)
{
switch (field.getFullyQualifiedName())
{
case "txtNumTar": field.setValue(order.getNumTarjetaTesco());break;
case "txtFechaReporte": field.setValue(order.getFechaReporte().toString());break;
case "txtFechaCarga": field.setValue(order.getFechaCarga().toString());break;
case "txtLugarObs" : field.setValue(order.getLugarObs().getNombre());break;
case "txtClasiSuge" : field.setValue(order.getClasifSug().getNombre());break;
case "txtEquipo" : field.setValue(order.getEquipo().getNombre());break;
case "txtEvento" : field.setValue(order.getEvento().getNombre());break;
case "txtDecisionTomada" : field.setValue(order.getDecisionTomada());break;
case "txtEstado" : if (order.isEstado())
{
field.setValue("Abierto");break;
}else
{
field.setValue("Cerrado");break;
}
case "txtResuelto" : if (order.isResuelto())
{
field.setValue("Resuelto");break;
}else
{
field.setValue("No Resuelto");break;
}
case "txtReportado" : if (order.isReportado())
{
field.setValue("Reportado");break;
}else
{
field.setValue("No Reportado");break;
}
}
}
return pdfDocument;
}
示例7: loadAndPopulateTemplate
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
/**
* Loads the template pdf file and populates it with the order details
*
* @param order The order with the details for the pdf document
* @return The populated PDF document
* @throws Exception If the loading or the populating of the document fails for some reason
*/
private PDDocument loadAndPopulateTemplate(Order order) throws Exception {
PDDocument pdfDocument = PDDocument.load(new ByteArrayInputStream(pdfAsBytes));
PDAcroForm pdfForm = pdfDocument.getDocumentCatalog().getAcroForm();
List<PDField> fields = pdfForm.getFields();
SortedSet<OrderLine> orderLines = order.getOrderLines();
for (PDField field : fields) {
String fullyQualifiedName = field.getFullyQualifiedName();
if ("orderDate".equals(fullyQualifiedName)) {
field.setValue(order.getDate().toString());
} else if ("orderNumber".equals(fullyQualifiedName)) {
field.setValue(order.getNumber());
} else if ("customerName".equals(fullyQualifiedName)) {
field.setValue(order.getCustomerName());
} else if ("message".equals(fullyQualifiedName)) {
String message = "You have ordered '" + orderLines.size() +"' products";
field.setValue(message);
} else if ("preferences".equals(fullyQualifiedName)) {
field.setValue(order.getPreferences());
}
}
int i = 1;
Iterator<OrderLine> orderLineIterator = orderLines.iterator();
while (i < 7 && orderLineIterator.hasNext()) {
OrderLine orderLine = orderLineIterator.next();
String descriptionFieldName = "orderLine|"+i+"|desc";
pdfForm.getField(descriptionFieldName).setValue(orderLine.getDescription());
String costFieldName = "orderLine|"+i+"|cost";
pdfForm.getField(costFieldName).setValue(orderLine.getDescription());
String quantityFieldName = "orderLine|"+i+"|quantity";
pdfForm.getField(quantityFieldName).setValue(orderLine.getDescription());
i++;
}
return pdfDocument;
}
示例8: create
import org.apache.pdfbox.pdmodel.interactive.form.PDField; //导入方法依赖的package包/类
@Override
public PDDocument create(int start, T data) throws IOException {
PDDocument ret = resource.load(data);
if (appender != null) {
ret = appender.append(start, ret);
}
if ((converter != null) && (data != null)) {
Map<String, String> map = converter.convert(data);
if (((map != null) && !map.isEmpty()) || flatten) {
PDDocumentCatalog catalog = ret.getDocumentCatalog();
PDAcroForm acroForm = catalog.getAcroForm();
List<PDField> fields = acroForm.getFields();
for (PDField field : fields) {
if (map != null) {
boolean find = false;
String key = field.getFullyQualifiedName();
if (map.containsKey(key)) {
find = true;
} else {
key = field.getPartialName();
if (map.containsKey(key)) {
find = true;
} else {
key = field.getAlternateFieldName();
if (map.containsKey(key)) {
find = true;
}
}
}
if (find) {
field.setValue(map.get(key));
}
}
if (flatten) {
field.setReadonly(true);
}
}
}
}
return ret;
}