Hi All,
I have the following requirements and have implemented the most part of it,
but can't get the final part working, which is the printing on a physical
printer:
1. Create PDF template - Livecycle
2. Populate template with Receipt information - iText
3. Using Java, print receipt pdf at a particular printer - java.print
For part 3, when I actually send the file to the printer, it print's out the
pdf, but none of the AcroForm fields are populated with data. If I open the
PDF and then print from there, it has the fields.
I'm guessing this is something to do with the output pdf after using iText
to populate the form. It seems like the template and data elements aren't
merged.
Here is my code - using itextpdf-5.1.0 and pdf-renderer-1.0.5:
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Map;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.AcroFields.Item;
import com.sun.pdfview.PDFFile;
public class PDFCreate {
public static File createPdfData(String pdfTemplate, String fileName) throws
Exception {
PdfReader reader = new PdfReader(pdfTemplate);
File outputFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
PdfStamper stamper = new PdfStamper(reader,fos);
AcroFields form = stamper.getAcroFields();
Map<String,Item> fieldMap = form.getFields();
for(String s:fieldMap.keySet()) {
if(s.contains("ReceiptNumber")) {
form.setField(s, "10000000");
}
}
stamper.setFormFlattening(true);
stamper.close();
return outputFile;
}
public static void main(String[] args) {
try {
File file = null;
// created by Adobe Livecycle as a static pdf form
String pdfTemplate = "E:\\tmp\\pdfTemplate.pdf";
String outputFileName = "E:\\tmp\\receipt.pdf";
try {
file = createPdfData(pdfTemplate, outputFileName);
} catch (Exception ex) {
ex.printStackTrace();
}
if (file == null) {
return;
}
FileChannel fc = new FileInputStream(file).getChannel();
ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY,0,fc.size());
PDFFile pdfFile = new PDFFile (buf);
PDFPrintPage pages = new PDFPrintPage(pdfFile);
PrinterJob pjob = PrinterJob.getPrinterJob();
//pjob.setPrintService(printSvc);
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pf.setOrientation(PageFormat.PORTRAIT);
Paper paper = new Paper();
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
pf.setPaper(paper);
pjob.setJobName(file.getName());
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
pjob.print();
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I run this code and set the file to be printed as a standard pdf, all
elements are printed out successfully.
I think it's definitely something wrong with my createPdfData method.
Regards,
Aditya Gollakota
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php