I have some one off pdf java code in which I'm using Itext-1.4.1. I used
a number of examples and code found on this list and elsewhere - so if it
looks familiar it probably is.
The code performs the following:
for each file it finds in a directory it reads the file and fields and
replaces any fields named *.1 with *.2 (2..5) then saves the new file
naming the new file oldFileName.2.pdf - it then concats itself to the end
of the oldFileName.pdf. I do this 5 times up through oldFileName.5.pdf
(which is 5 copies of oldFileName.pdf with renamed fields on each.
It also contains fields that it doesn't rename and I want these to be the
same on each page in the new file.
The problem is any fields on file1 that aren't renamed will not copy over
correcty to file2 - they appear correct in the form tool in acrobat until
I open up the field - it doesn't have a name and will blow up when I try
to merge an fdf with it.
I'm attempting to include the code here... Thanks in advance for any
suggestions.
//begin code
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.SimpleBookmark;
public class RenameFormFieldsv5
{
public static void usage()
{
System.out.println("Rename Fields");
// System.out.println("usage: rename_fields in_path out_path
// [[name1=newname1]...[namex=newnamex]]");
}
// Make sure path exists, and is a directory
public static boolean isValidPath(String path)
{
File dir = new File(path);
if(!dir.exists())
return (false);
if(!dir.isDirectory())
return (false);
return (true);
}
// Rename fields in in_pdf using args[] array, output to out_pdf
public static void process(String in_pdf)
{
System.out.println("Processing: " + in_pdf);
int fileIndexMax = 5;
try
{
for(int fileIndex = 2; fileIndex <= fileIndexMax; fileIndex++)
{
// modify the fields
String out_tmp = in_pdf.replace(".pdf", "." + fileIndex +
".pdf");
PdfStamper stamp = new PdfStamper(
new PdfReader(in_pdf), new FileOutputStream(out_tmp));
AcroFields form = stamp.getAcroFields();
HashMap fields = new HashMap(form.getFields());
Set keys = fields.keySet();
for(Iterator i = keys.iterator(); i.hasNext();)
{
String value = (String)i.next();
if(value.startsWith("replform"))
{
String newValue = value.replace(".1", "." + fileIndex);
System.out.println("\tReplacing " + value + " with " +
newValue);
form.renameField(value, newValue);
}
}
stamp.close();
// concatenate files
System.out.println("\tConcatenating files...");
if(fileIndex == 2)
concatPDF(
new String[]{
in_pdf,
out_tmp,
out_tmp.replace(".tmp", ".pdf")});
else
concatPDF(
new String[]{
out_tmp.replace("." + fileIndex + ".tmp", "." +
(fileIndex - 1) + ".pdf"),
out_tmp,
out_tmp.replace(".tmp", ".pdf")});
// clean up --
System.out.println("\tDeleting " + out_tmp + "...");
new File(out_tmp).delete();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
// Main entry point
public static void main(String[] args)
{
// if (args.length < 2) {
// usage();
// return;
// }
int idx = 0;
// if (args.length - idx < 2) {
// usage();
// return;
// }
// Get and validate input path
String in_path = args[idx];
if(!in_path.endsWith("\\"))
{
in_path = in_path + "\\";
}
if(!isValidPath(in_path))
{
System.out.println("invalid path: " + in_path);
return;
}
// // Get and validate output path
// String out_path = args[idx + 1];
// if (!out_path.endsWith("\\") )
// {
// out_path = out_path + "\\";
// }
// if (! isValidPath(out_path) )
// {
// System.out.println("invalid path: " + out_path);
// return;
// }
//
// // Make sure we don't overwrite files in same directory
// if (in_path.equals(out_path))
// {
// System.out.println("input path cannot be same as output path");
// return;
// }
// Get list of all pdf files in input path
File dir = new File(in_path);
FilenameFilter filter = new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".pdf");
}
};
String[] children = dir.list(filter);
// If no pdf files found, end with error message
if(children == null)
{
// Either dir does not exist or is not a directory
System.out.println("No PDF files found in directory " + in_path);
}
else
{
for(int i = 0; i < children.length; i++)
{
// Get filename of PDF file
String filename = children[i];
// Process the PDF file
process(in_path + filename); // , out_path + filename, args);
}
}
}
public static void concatPDF(String args[])
{
if(args.length < 2)
{
System.err.println("arguments: file1 [file2 ...] destfile");
}
else
{
try
{
int pageOffset = 0;
ArrayList master = new ArrayList();
int f = 0;
String outFile = args[args.length - 1];
Document document = null;
PdfCopy writer = null;
while(f < args.length - 1)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[f]);
reader.consolidateNamedDestinations();
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
List bookmarks = SimpleBookmark.getBookmark(reader);
if(bookmarks != null)
{
if(pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks,
pageOffset, null);
master.addAll(bookmarks);
}
pageOffset += n;
System.out.println("\tThere are " + n + " pages in " +
args[f]);
if(f == 0)
{
// step 1: creation of a document-object
document = new Document(reader.getPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
writer = new PdfCopy(document, new
FileOutputStream(outFile));
// step 3: we open the document
document.open();
}
// step 4: we add content
PdfImportedPage page;
for(int i = 0; i < n;)
{
++i;
page = writer.getImportedPage(reader, i);
writer.addPage(page);
System.out.println("\tProcessed page " + i);
}
writer.freeReader(reader);
reader.close();
f++;
}
if(master.size() > 0)
writer.setOutlines(master);
// step 5: we close the document
document.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
//end code
Thanks!
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions