一、前言
下面通过pdfbox的org.apache.pdfbox.pdmodel.PDDocument的pdf文件实现了分别org.apache.pdfbox.tools.ExportFDF、org.apache.pdfbox.tools.ImportFDF进行pdf导出、导入内容代码示例。
二、代码示例
1.ExportFDF导出pdf代码示例
package org.apache.pdfbox.tools;@b@@b@import java.io.File;@b@import java.io.IOException;@b@import org.apache.pdfbox.pdmodel.PDDocument;@b@import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;@b@import org.apache.pdfbox.pdmodel.fdf.FDFDocument;@b@ @b@public class ExportFDF@b@{@b@ /**@b@ * Creates a new instance of ImportFDF.@b@ */@b@ public ExportFDF()@b@ {@b@ }@b@@b@ /**@b@ * This will import an fdf document and write out another pdf.@b@ * <br>@b@ * see usage() for commandline@b@ *@b@ * @param args command line arguments@b@ *@b@ * @throws IOException in case the file can not be read or the data can not be exported.@b@ */@b@ public static void main(String[] args) throws IOException@b@ {@b@ // suppress the Dock icon on OS X@b@ System.setProperty("apple.awt.UIElement", "true");@b@@b@ ExportFDF exporter = new ExportFDF();@b@ exporter.exportFDF( args );@b@ }@b@@b@ private void exportFDF( String[] args ) throws IOException@b@ {@b@ PDDocument pdf = null;@b@ FDFDocument fdf = null;@b@@b@ try@b@ {@b@ if( args.length != 1 && args.length != 2 )@b@ {@b@ usage();@b@ }@b@ else@b@ {@b@ pdf = PDDocument.load( new File(args[0]) );@b@ PDAcroForm form = pdf.getDocumentCatalog().getAcroForm();@b@ if( form == null )@b@ {@b@ System.err.println( "Error: This PDF does not contain a form." );@b@ }@b@ else@b@ {@b@ String fdfName = null;@b@ if( args.length == 2 )@b@ {@b@ fdfName = args[1];@b@ }@b@ else@b@ {@b@ if( args[0].length() > 4 )@b@ {@b@ fdfName = args[0].substring( 0, args[0].length() -4 ) + ".fdf";@b@ }@b@ }@b@ fdf = form.exportFDF();@b@ fdf.save( fdfName );@b@ }@b@ }@b@ }@b@ finally@b@ {@b@ close( fdf );@b@ close( pdf );@b@ }@b@ }@b@@b@ /**@b@ * This will print out a message telling how to use this example.@b@ */@b@ private static void usage()@b@ {@b@ String message = "Usage: org.apache.pdfbox.ExportFDF <inputfile> [output-fdf-file]\n"@b@ + "\nOptions:\n"@b@ + " [output-fdf-file] : Default is pdf name, test.pdf->test.fdf";@b@ @b@ System.err.println(message);@b@ System.exit(1);@b@ }@b@@b@ /**@b@ * Close the document.@b@ *@b@ * @param doc The doc to close.@b@ *@b@ * @throws IOException If there is an error closing the document.@b@ */@b@ public void close( FDFDocument doc ) throws IOException@b@ {@b@ if( doc != null )@b@ {@b@ doc.close();@b@ }@b@ }@b@@b@ /**@b@ * Close the document.@b@ *@b@ * @param doc The doc to close.@b@ *@b@ * @throws IOException If there is an error closing the document.@b@ */@b@ public void close( PDDocument doc ) throws IOException@b@ {@b@ if( doc != null )@b@ {@b@ doc.close();@b@ }@b@ }@b@}
2. ImportFDF导入内容至pdf代码示例
package org.apache.pdfbox.tools;@b@@b@import java.io.File;@b@import java.io.IOException; @b@import org.apache.pdfbox.pdmodel.PDDocument;@b@import org.apache.pdfbox.pdmodel.PDDocumentCatalog;@b@import org.apache.pdfbox.pdmodel.fdf.FDFDocument;@b@import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;@b@@b@/**@b@ * This example will take a PDF document and fill the fields with data from the@b@ * FDF fields.@b@ *@b@ * @author Ben Litchfield@b@ */@b@public class ImportFDF@b@{@b@ /**@b@ * Creates a new instance of ImportFDF.@b@ */@b@ public ImportFDF()@b@ {@b@ }@b@@b@ /**@b@ * This will takes the values from the fdf document and import them into the@b@ * PDF document.@b@ *@b@ * @param pdfDocument The document to put the fdf data into.@b@ * @param fdfDocument The FDF document to get the data from.@b@ *@b@ * @throws IOException If there is an error setting the data in the field.@b@ */@b@ public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException@b@ {@b@ PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();@b@ PDAcroForm acroForm = docCatalog.getAcroForm();@b@ if (acroForm == null)@b@ {@b@ return;@b@ }@b@ acroForm.setCacheFields( true );@b@ acroForm.importFDF( fdfDocument );@b@ @b@ //TODO this can be removed when we create appearance streams@b@ acroForm.setNeedAppearances(true);@b@ }@b@@b@ /**@b@ * This will import an fdf document and write out another pdf.@b@ * <br>@b@ * see usage() for commandline@b@ *@b@ * @param args command line arguments@b@ *@b@ * @throws IOException If there is an error importing the FDF document.@b@ */@b@ public static void main(String[] args) throws IOException@b@ {@b@ // suppress the Dock icon on OS X@b@ System.setProperty("apple.awt.UIElement", "true");@b@@b@ ImportFDF importer = new ImportFDF();@b@ importer.importFDF( args );@b@ }@b@@b@ private void importFDF( String[] args ) throws IOException@b@ {@b@ PDDocument pdf = null;@b@ FDFDocument fdf = null;@b@@b@ try@b@ {@b@ if( args.length != 3 )@b@ {@b@ usage();@b@ }@b@ else@b@ {@b@ ImportFDF importer = new ImportFDF();@b@@b@ pdf = PDDocument.load( new File(args[0]) );@b@ fdf = FDFDocument.load( args[1] );@b@ importer.importFDF( pdf, fdf );@b@@b@ pdf.save( args[2] );@b@ }@b@ }@b@ finally@b@ {@b@ close( fdf );@b@ close( pdf );@b@ }@b@ }@b@@b@ /**@b@ * This will print out a message telling how to use this example.@b@ */@b@ private static void usage()@b@ {@b@ System.err.println( "usage: org.apache.pdfbox.tools.ImportFDF <pdf-file> <fdf-file> <output-file>" );@b@ System.exit(1);@b@ }@b@@b@ /**@b@ * Close the document.@b@ *@b@ * @param doc The doc to close.@b@ *@b@ * @throws IOException If there is an error closing the document.@b@ */@b@ public void close( FDFDocument doc ) throws IOException@b@ {@b@ if( doc != null )@b@ {@b@ doc.close();@b@ }@b@ }@b@@b@ /**@b@ * Close the document.@b@ *@b@ * @param doc The doc to close.@b@ *@b@ * @throws IOException If there is an error closing the document.@b@ */@b@ public void close( PDDocument doc ) throws IOException@b@ {@b@ if( doc != null )@b@ {@b@ doc.close();@b@ }@b@ }@b@}