You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Richard Martin (JIRA)" <ji...@apache.org> on 2017/03/19 14:43:42 UTC
[jira] [Commented] (PDFBOX-3723) FDFField.getValue() doesn't return
the values in line with the Javadoc.
[ https://issues.apache.org/jira/browse/PDFBOX-3723?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15931719#comment-15931719 ]
Richard Martin commented on PDFBOX-3723:
----------------------------------------
Maruan asked that I post a unit test for this. I'm a newbie so this might not be the right way to post it but I don't see any way to post an attachment. I've pasted the java source and the XFDF source below separated by a line of equal signs. Let me know if there's a "better way". The loading of the XFDF file is configured to a path unique to my environment. That would need to be changed for testing.
===============================================================
Java Source
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
import org.apache.pdfbox.pdmodel.fdf.FDFField;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;
import org.apache.pdfbox.pdmodel.interactive.form.PDVariableText;
import java.io.File;
import java.io.IOException;
import java.util.List;
//import org.apache.pdfbox.pdmodel.PDDocument;
//import org.apache.pdfbox.tools.*;
/**
* This example will take a PDF document and fill the fields with data from the
* XFDF fields.
*
* @author <a href="ben@benlitchfield.com">Ben Litchfield</a>
* @version $Revision: 1.2 $
*/
public class ALS3122FormFillV2UnitTest
{
/**
* Creates a new instance of ImportFDF.
*/
public ALS3122FormFillV2UnitTest()
{
}
/**
* This will takes the values from the fdf document and import them into the
* PDF document.
*
* @param pdfDocument The document to put the fdf data into.
* @param fdfDocument The FDF document to get the data from.
*
* @throws IOException If there is an error setting the data in the field.
*/
public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException
{
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
acroForm.setNeedAppearances(true);
// acroForm.setNeedAppearances(false);
// see need for line above at: http://stackoverflow.com/questions/12198742/pdf-form-text-hidden-unless-clicked
acroForm.setCacheFields( true );
acroForm.importFDF( fdfDocument );
// acroForm.setNeedAppearances(true);
}
/**
* This will import an fdf document and write out another pdf.
* <br />
* see usage() for commandline
*
* @param args command line arguments
*
* @throws Exception If there is an error importing the FDF document.
*/
public static void main(String[] args) throws Exception
{
ALS3122FormFillV2UnitTest importer = new ALS3122FormFillV2UnitTest();
importer.importXFDF( args );
}
private void importXFDF( String[] args ) throws Exception
{
PDDocument pdf = null;
FDFDocument fdf = null;
try
{
// if( args.length != 3 )
// {
// usage();
// }
// else
{
ALS3122FormFillV2UnitTest importer = new ALS3122FormFillV2UnitTest();
fdf = FDFDocument.loadXFDF( "/Users/richardmartin/Downloads/Tester-doh-31222012UnitTest.xfdf" );
// now let's look for fields to adjust font size if needed
List<?> fields = fdf.getCatalog().getFDF().getFields();
if( fields != null )
{
String defaultAppearanceString = "/Helv 0 Tf 0 g";
for (Object field : fields)
{
FDFField fdfField = (FDFField) field;
String fdfName, fdfValue;
Integer fdfNameLen;
Integer fdfValueLen;
fdfName = fdfField.getPartialFieldName() ;
// fdfValue = (String) fdfField.getValue() ;
fdfValue = String.valueOf(fdfField.getValue());
fdfNameLen = fdfName.length();
fdfValueLen = fdfValue.length();
boolean fontAutoSize = false;
String pdfName = null;
String pdfType = null;
// PDField pdfField = form.getField(fdfName) ;
// if (pdfField != null) {
// pdfName = pdfField.getPartialName();
// pdfType = pdfField.getFieldType();
// }
if (fdfName != null & fdfValue != null ) { //& pdfName != null) {
System.out.println("fdfField " + fdfName + " " + " " + fdfValue);
}
}
}
}
}
finally
{
close( fdf );
}
}
/**
* This will print out a message telling how to use this example.
*/
private static void usage()
{
System.err.println( "usage: org.apache.pdfbox.ImportXFDF <pdf-file> <fdf-file> <output-file>" );
}
/**
* Close the document.
*
* @param doc The doc to close.
*
* @throws IOException If there is an error closing the document.
*/
public void close( FDFDocument doc ) throws IOException
{
if( doc != null )
{
doc.close();
}
}
/**
* Close the document.
*
* @param doc The doc to close.
*
* @throws IOException If there is an error closing the document.
*/
public void close( PDDocument doc ) throws IOException
{
if( doc != null )
{
doc.close();
}
}
}
===============================================================
XFDF source
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<f href="http://arlinprd3.adaptiveresults.com:5050/ALS/faces/pdf/doh-31222012.pdf"/>
<fields>
<field name="MedEvalID">
<value>103</value>
</field>
<field name="ResidentID">
<value>13</value>
</field>
<field name="Residents_Name">
<value>Tester, Richard (Dick)</value>
</field>
<field name="Medication15">
<value>Ranitidine</value>
</field>
<field name="Medic_Dosage15">
<value>150 mg</value>
</field>
<field name="Medic_Type15">
<value>1/2 tab</value>
</field>
<field name="Medic_Freq15">
<value>BID</value>
</field>
<field name="Medic_Route15">
<value>PO</value>
</field>
<field name="Medic_Diag15">
<value>GERD</value>
</field>
<field name="Medic_Provider15">
<value>Evans, Robert</value>
</field>
</fields>
</xfdf>
> FDFField.getValue() doesn't return the values in line with the Javadoc.
> -----------------------------------------------------------------------
>
> Key: PDFBOX-3723
> URL: https://issues.apache.org/jira/browse/PDFBOX-3723
> Project: PDFBox
> Issue Type: Bug
> Components: PDModel
> Affects Versions: 2.0.0, 2.0.4, 2.0.5
> Reporter: Maruan Sahyoun
> Assignee: Maruan Sahyoun
> Fix For: 2.0.6
>
>
> For the 2.0.x releases {{FDFField.getValue()}} doesn't neither return the documented {{PDTextStream}} return type nor {{String}} for values of type {{COSString}} or {{COSStream}}. This has somewhat be masked by another issue fixed in PDFBOX-3596.
> As {{PDTextStream}} is no longer available we should return as {{String}} (which is inline with {{PDField}}) and adjust the javadoc accordingly.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org