This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting text into a textbox in a pdf

 
swati basant
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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...

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic