aspose file tools*
The moose likes Other Open Source Projects and the fly likes Inserting text into a textbox in a pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Inserting text into a textbox in a pdf" Watch "Inserting text into a textbox in a pdf" New topic
Author

Inserting text into a textbox in a pdf

swati basant
Greenhorn

Joined: Jan 30, 2011
Posts: 12
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
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
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.


Ping & DNS - my free Android networking tools app
swati basant
Greenhorn

Joined: Jan 30, 2011
Posts: 12

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
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
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?
 
Consider Paul's rocket mass heater.
 
subject: Inserting text into a textbox in a pdf