I found the answer... I needed to use NoView instead of ToggleNoView.
So for those interested, setting setNoView(false) and setPrint(true) does
the trick.

On Fri, Feb 24, 2017 at 9:19 PM, Gilad Denneboom <[email protected]>
wrote:

> Maybe you can help me with one additional thing... I want to set the field
> as "Hidden, but printable" but none of the built-in methods seem to do that.
> I've trying various combinations of setInvisible, setHidden, setPrinted
> and setToggleNoView, but they didn't work. Any ideas how to achieve that?
>
> On Fri, Feb 24, 2017 at 7:30 PM, Gilad Denneboom <
> [email protected]> wrote:
>
>> Thanks for the tip!
>>
>> I got it to work, but not by using the COSDictionary object, which didn't
>> see to "take". My final code is this:
>>
>>         PDTextField bottledOnField = new PDTextField(acroForm);
>>         String defaultAppearanceString = "/Helv 12 Tf 0 0 1 rg";
>>         bottledOnField.setDefaultAppearance(defaultAppearanceString);
>>         bottledOnField.setPartialName("BottledOn");
>>         acroForm.getFields().add(bottledOnField);
>>
>>         PDAnnotationWidget bottledOnWidget =
>> bottledOnField.getWidgets().get(0);
>>         bottledOnWidget.setRectangle(new PDRectangle(bottledOnX,
>> bottledOnY, bottledOnW, bottledOnH));
>>         bottledOnWidget.setHidden(false);
>>         bottledOnWidget.setPrinted(true);
>>         bottledOnField.setQ(1); // set alignment to "center"
>>         bottledOnField.setReadOnly(true);
>>         bottledOnWidget.setPage(p);
>>         p.getAnnotations().add(bottledOnField.getWidgets().get(0));
>>
>> On Fri, Feb 24, 2017 at 7:05 PM, Maruan Sahyoun <[email protected]>
>> wrote:
>>
>>> Hi,
>>>
>>> you are missing to add the field to the AcroForm. Please take a look at
>>>
>>> https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main
>>> /java/org/apache/pdfbox/examples/interactive/form/CreateSimp
>>> leForm.java?view=markup
>>>
>>> If that doesn't work for you let me know and I'll enhance the sample.
>>>
>>> BR
>>> Maruan
>>>
>>> > Am 24.02.2017 um 17:26 schrieb Gilad Denneboom <
>>> [email protected]>:
>>> >
>>> > Hi all,
>>> >
>>> > I'm in the process of moving from PDFBox 1.x to 2.x, but have
>>> encountered
>>> > an issue that I hope you could help me with.
>>> > One of my requirements in various projects is to add new form fields
>>> to a
>>> > file.
>>> > In PDFBox 1.x I would do this by generating a COSDictionary object
>>> which
>>> > contained all the basic field properties (type, value, rect, font size,
>>> > text color, etc.), and would then use it to generate a new PDTextbox
>>> object
>>> > (for example), which I would then add the page's annotation list. And
>>> here
>>> > is my problem. In PDFBox 2.x the constructor for PDTextField no longer
>>> > accepts the COSDictionary as its second parameter, only a PDAcroForm
>>> as its
>>> > first. So how am I supposed to generate a new field?
>>> > Then I noticed that the PDAnnotationWidget constructor can now take a
>>> > COSDictionary object as its parameter, so I tried that, but it doesn't
>>> > work... No field is added.
>>> > Here's my my code (the coordinates might be off, but it should still
>>> add
>>> > something, I think...):
>>> >
>>> >        originalDoc = PDDocument.load(new File(outputFilePath));
>>> >        PDAcroForm acroForm = new PDAcroForm(originalDoc);
>>> >        p = originalDoc.getPage(0);
>>> >        float bottledOnX = inch * 2.375f;
>>> >        float bottledOnY = inch * 0.032f;
>>> >        float bottledOnW = inch * 0.257f;
>>> >        float bottledOnH = inch * 0.231f;
>>> >        COSDictionary bottledOnFieldDic = createTextField("BottledOn",
>>> > "TEST", new float[] {bottledOnX, bottledOnY, bottledOnX+bottledOnW,
>>> > bottledOnY+bottledOnH}, "50", 1);
>>> >        PDTextField bottledOnField = new PDTextField(acroForm);
>>> >        PDAnnotationWidget bottledOnWidget = new
>>> > PDAnnotationWidget(bottledOnFieldDic);
>>> >        bottledOnWidget.setHidden(false);
>>> >        bottledOnWidget.setPrinted(true);
>>> >        bottledOnField.getWidgets().set(0, bottledOnWidget);
>>> >        bottledOnField.setQ(1); // set alignment to "center"
>>> >        bottledOnField.setReadOnly(true);
>>> >        p.getAnnotations().add(bottledOnField.getWidgets().get(0));
>>> >        originalDoc.save(outputFilePath);
>>> >        originalDoc.close();
>>> >
>>> >    private static COSDictionary createTextField(String fieldName,
>>> String
>>> > value, float[] rect, String fontSize, int textColor) {
>>> >        COSDictionary cosDict = new COSDictionary();
>>> >        COSArray rectCosArray = new COSArray();
>>> >        rectCosArray.add(new COSFloat(rect[0])); // lower x boundary
>>> >        rectCosArray.add(new COSFloat(rect[1])); // lower y boundary
>>> >        rectCosArray.add(new COSFloat(rect[2])); // upper x boundary
>>> >        rectCosArray.add(new COSFloat(rect[3])); // upper y boundary
>>> >
>>> >        cosDict.setItem(COSName.RECT, rectCosArray);
>>> >        cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field
>>> Type
>>> >        cosDict.setItem(COSName.TYPE, COSName.ANNOT);
>>> >        cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
>>> >        cosDict.setItem(COSName.T, new COSString(fieldName));
>>> >
>>> >        cosDict.setString(COSName.DA, "/Helv "+fontSize+" Tf
>>> "+textColor+"
>>> > g");
>>> >
>>> >        COSString fieldValue = new COSString(value);
>>> >        cosDict.setItem(COSName.V, fieldValue);
>>> >
>>> >        return cosDict;
>>> >    }
>>> >
>>> > Any ideas what I'm doing wrong here?
>>> >
>>> > Thanks in advance!
>>> >
>>> > Gilad
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>>
>>
>

Reply via email to