Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 51213200C25 for ; Fri, 24 Feb 2017 19:31:06 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 4C994160B69; Fri, 24 Feb 2017 18:31:06 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 70B99160B62 for ; Fri, 24 Feb 2017 19:31:05 +0100 (CET) Received: (qmail 96950 invoked by uid 500); 24 Feb 2017 18:31:04 -0000 Mailing-List: contact users-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@pdfbox.apache.org Delivered-To: mailing list users@pdfbox.apache.org Received: (qmail 96938 invoked by uid 99); 24 Feb 2017 18:31:04 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 24 Feb 2017 18:31:04 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id B10A818E83B for ; Fri, 24 Feb 2017 18:31:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.899 X-Spam-Level: * X-Spam-Status: No, score=1.899 tagged_above=-999 required=6.31 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HTML_MESSAGE=2, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=disabled Authentication-Results: spamd3-us-west.apache.org (amavisd-new); dkim=pass (2048-bit key) header.d=gmail.com Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id soJiTuUJYyam for ; Fri, 24 Feb 2017 18:31:01 +0000 (UTC) Received: from mail-yb0-f172.google.com (mail-yb0-f172.google.com [209.85.213.172]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id CA2105F30C for ; Fri, 24 Feb 2017 18:31:00 +0000 (UTC) Received: by mail-yb0-f172.google.com with SMTP id a5so7302669ybb.2 for ; Fri, 24 Feb 2017 10:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=/Ca/OmmzfdQ08ep30LXUE/Ud+6rfFuOr3ibRg8Ow8OQ=; b=Twv/N6hbGGrV8vjbAXmArsWIA++dcD/+N9zKFXqmLhD69zP1vx6HdbMtgSlQc3+w8I k9F5/yJDK/c9zdQvvZLst9X+hWtYBTuUaWwjdcXBGJzJe73nzAAgoCmvlj6DWotaeDUx uLUrB76/heoP2TeUzAcxiyOMFR5RAVnGJ44b86my9nGwiRl8XY0Adws6w9AaOEtixUFO JwUeX5YcW4oXV7RiElu5iZIVRFy9vyeMnqNLalNMtCeH4NJF7RSPkhgPi1cAlvchNFx/ VeeRK2XGCvXstUPeTF66L9+2KnW4+8yBoF9kWgiXd+UbPV7Q9jP3yHVkdSX7+DYJDcHB TKew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=/Ca/OmmzfdQ08ep30LXUE/Ud+6rfFuOr3ibRg8Ow8OQ=; b=coW+aTSSBRTGQcwLmbxr1wKPHfLYWNNLjnapLvTShHXOW7wF06HUzryva8L4p568w4 9oBaXzWNR2HlI/CwU7KV2p7fQvDPwA1SnEAuhRbGUksdXtj3djjePZe2mQ00YVYSZ5QZ AZ9+39T01Vm7SzAijsLQSqiKn7j82HFCON50h+Eq+105JMU8hmrlp4Af9QzwiR7vjQ/h nN5SVNIl79KXBS2eISROSEZlTata65DkNvh6OAZXYi2DkcEdR/KKfQIvSo+r7BstydTt IHS0ds3yl6QIhVcH2mSMzHrX74O4YBLEjQyov15vNCwjVI96MGxuHd5ruxQtecsXsKCn 0GdA== X-Gm-Message-State: AMke39kpstj63wVfMYyGoaCJ9f/fcVi8oBAgByezmKZ8TurIyV1UcSq7kR/m58gK20MLU+nyniO+wnT2lo7RAg== X-Received: by 10.37.231.12 with SMTP id e12mr2683198ybh.190.1487961057160; Fri, 24 Feb 2017 10:30:57 -0800 (PST) MIME-Version: 1.0 Received: by 10.129.172.66 with HTTP; Fri, 24 Feb 2017 10:30:36 -0800 (PST) In-Reply-To: <0EB0FCF7-50A6-4AD1-8F8D-988F72E0A610@fileaffairs.de> References: <0EB0FCF7-50A6-4AD1-8F8D-988F72E0A610@fileaffairs.de> From: Gilad Denneboom Date: Fri, 24 Feb 2017 19:30:36 +0100 Message-ID: Subject: Re: Creating new text fields using PDFBox 2.x To: "users@pdfbox.apache.org" Content-Type: multipart/alternative; boundary=94eb2c0a73104078b905494aeea2 archived-at: Fri, 24 Feb 2017 18:31:06 -0000 --94eb2c0a73104078b905494aeea2 Content-Type: text/plain; charset=UTF-8 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 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/ > CreateSimpleForm.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 < > gilad.denneboom@gmail.com>: > > > > 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: users-unsubscribe@pdfbox.apache.org > For additional commands, e-mail: users-help@pdfbox.apache.org > > --94eb2c0a73104078b905494aeea2--