当前位置: 首页>>代码示例>>Java>>正文


Java PDTextField类代码示例

本文整理汇总了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();
        }
    }
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:49,代码来源:AddFormField.java

示例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();
    }
}
 
开发者ID:mkl-public,项目名称:testarea-pdfbox2,代码行数:49,代码来源:FillInForm.java


注:本文中的org.apache.pdfbox.pdmodel.interactive.form.PDTextField类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。