本文整理匯總了Java中org.apache.pdfbox.pdmodel.interactive.form.PDTextField類的典型用法代碼示例。如果您正苦於以下問題:Java PDTextField類的具體用法?Java PDTextField怎麽用?Java PDTextField使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PDTextField類屬於org.apache.pdfbox.pdmodel.interactive.form包,在下文中一共展示了PDTextField類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testAddFieldLikeEugenePodoliako
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField; //導入依賴的package包/類
/**
* <a href="https://stackoverflow.com/questions/46433388/pdfbox-could-not-find-font-helv">
* PDFbox Could not find font: /Helv
* </a>
* <br/>
* <a href="https://drive.google.com/file/d/0B2--NSDOiujoR3hOZFYteUl2UE0/view?usp=sharing">
* 4.pdf
* </a>
* <p>
* The cause is a combination of the OP and the source PDF not providing
* a default appearance for the text field and PDFBox providing defaults
* inconsequentially.
* </p>
* <p>
* This is fixed here by setting the default appearance explicitly.
* </p>
*/
@Test
public void testAddFieldLikeEugenePodoliako() throws IOException {
try ( InputStream originalStream = getClass().getResourceAsStream("4.pdf") )
{
PDDocument pdf = PDDocument.load(originalStream);
PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDPage page = pdf.getPage(0);
PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("SampleField");
acroForm.getFields().add(textBox);
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(0, 0, 0, 0);
widget.setRectangle(rect);
widget.setPage(page);
// Unnecessary code from OP
// widget.setAppearance(acroForm.getFields().get(0).getWidgets().get(0).getAppearance());
// Fix added to set default appearance accordingly
textBox.setDefaultAppearance(acroForm.getFields().get(0).getCOSObject().getString("DA"));
widget.setPrinted(false);
page.getAnnotations().add(widget);
acroForm.refreshAppearances();
acroForm.flatten();
pdf.save(new File(RESULT_FOLDER, "4-add-field.pdf"));
pdf.close();
}
}
示例2: testFillLikeRichardBrown
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField; //導入依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/36926060/apache-pdfbox-form-fill-truetype-text-spacing-issue">
* Apache PDFBox Form Fill TrueType text spacing issue
* </a>
* <br>
* <a href="https://www.dropbox.com/sh/b7ft1k0wfesob8s/AABnrTOgX26JlWCxl85jXns0a/FillFormField.pdf?dl=0">
* FillFormField.pdf
* </a>
* <p>
* Indeed, the issue can be reproduced, it is due to a combination of two factors:
* </p>
* <p>
* <b>A quirk of PDFBox when writing text</b> - When writing text into a content stream,
* PDFBox translates each Unicode codepoint into a name and looks up that name in a map
* generating from the inverted font encoding. For some encodings, though, there are two
* codes mapping to the name space, and the inverted map maps back to only one of them,
* in the case at hand the non-breaking variant. As both are expected to be typographically
* identical, this should not be a problem. But:
* </p>
* <p>
* <b>Non-conformant font in the PDF</b> - The font Impact in the PDF is defined with
* width 176 for the normal space glyph and 750 for the nonbreaking space glyph. Thus,
* they typographically differ vehemently.
* </p>
*/
@Test
public void testFillLikeRichardBrown() throws IOException
{
try ( InputStream originalStream = getClass().getResourceAsStream("FillFormField.pdf") )
{
// load the documents
PDDocument pdfDocument = PDDocument.load(originalStream);
// get the document catalog
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
// as there might not be an AcroForm entry a null check is necessary
if (acroForm != null)
{
PDTextField field = (PDTextField) acroForm.getField( "Title" );
field.setValue("Low Mileage Beauty Kill");
}
// Save and close the filled out form.
pdfDocument.save(new File(RESULT_FOLDER, "FillFormFieldRichardBrown.pdf"));
pdfDocument.close();
}
}