• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inserting text into a textbox in a pdf

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI..please help me out

M totally new to the concept of pdfbox..n i have no idea where to start from...

My problem's this...I have a Pdf doc which has a text field...i need to write a java prog using pdfbox to retrieve or write values into the field..
please help me...

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're basically out of luck. While PDFBox has tools to extract text from a PDF (and its web site explains how to use those), those get you ALL text; there's no way to extract specific bits of it. And PDFBox has no facilities for changing specific bits of a PDF file. Maybe if the field is part of an AcroForm; the iText library could also help in that case.
 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

package myPack;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;

import org.apache.pdfbox.exceptions.COSVisitorException;

import org.apache.pdfbox.examples.AbstractExample;


public class SetField extends AbstractExample
{



public void setField( PDDocument pdfDocument, String name, String value ) throws IOException
{
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField( name );

if( field != null )
{
field.setValue( value );
}
else
{
System.err.println( "No field found with name:" + name );
}



public static void main(String[] args) throws IOException, COSVisitorException
{
SetField setter = new SetField();
setter.setField( args );
}

private void setField( String[] args ) throws IOException , COSVisitorException
{
PDDocument pdf = null;
try
{
if( args.length != 3 )
{
usage();
}
else
{
SetField ex = new SetField();

pdf = PDDocument.load( args[0] );

-> ex.setField( pdf, args[1], args[2] );

pdf.save( args[0] );
}
}
finally
{
if( pdf != null )
{
pdf.close();
}
}
}

private static void usage()
{
System.err.println( "usage: org.apache.pdfbox.examples.fdf.SetField <pdf-file> <field-name> <field-value>" );
}
}

Yea...the textbox is a part of acroform...
This is the code i thot wud help me...bt then again theres a problem... the line where i have given an arrow...i dont knw wat to pass as the 2nd argument..
as in the field_name

is there any api using wich i cud list out all the field_names in an acroform..
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.

is there any api using wich i cud list out all the field_names in an acroform..


Doesn't the org.apache.pdfbox.examples.fdf.PrintFields example do that?
reply
    Bookmark Topic Watch Topic
  • New Topic