package org.ijsolutions.pdf; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.pdfbox.cos.COSDocument; import org.apache.pdfbox.io.RandomAccessFile; import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.dom4j.Element; import org.openedit.OpenEditException; import org.openedit.page.Page; import org.openedit.page.manage.PageManager; import org.openedit.repository.ContentItem; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.AcroFields; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.RandomAccessFileOrArray; public class PdfUtil { private static final Log log = LogFactory.getLog(PdfUtil.class); protected PageManager fieldPageManager; protected File fieldRoot; public File getRoot() { return fieldRoot; } public void setRoot(File inRoot) { fieldRoot = inRoot; } public PageManager getPageManager() { return fieldPageManager; } public void setPageManager(PageManager inPageManager) { fieldPageManager = inPageManager; } public void generateMultiPagePDF(InputStream inTemplate, OutputStream inOut, Element inRoot, PDFInstructions instructions) throws DocumentException, IOException { Document document = null; PdfCopy writer = null; RandomAccessFileOrArray target; PdfReader reader; target = new RandomAccessFileOrArray(inTemplate); reader = new PdfReader(target, null); for (Iterator iterator = inRoot.elementIterator("document"); iterator.hasNext();) { Element element = (Element) iterator.next(); // This will get the file names from the document element and use it // for the pdf creation... but it doesn't work. creating a second // String input_file = element.attributeValue("file"); // Page page = getPageManager().getPage(input_file); // target = new RandomAccessFileOrArray(page.getInputStream()); // reader = new PdfReader(target, null); // Create the target writer targeting the browser output stream // (hopefully this isn't too slow!) // open the document if (document == null) { document = new Document(reader.getPageSizeWithRotation(1)); writer = new PdfCopy(document, inOut); if (instructions.isLock()) { writer.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.STRENGTH128BITS); } writer.setFullCompression(); document.open(); } target.seek(0); reader = new PdfReader(target, null); addData(reader, writer, document, element, instructions); // target.close(); } document.close(); writer.flush(); writer.close(); } public void generatePDF(InputStream inTemplate, OutputStream inOut, Element inRoot, PDFInstructions instructions) throws DocumentException, IOException { RandomAccessFileOrArray target = new RandomAccessFileOrArray(inTemplate); PdfReader reader = new PdfReader(target, null); Document document = null; // Create the target writer targeting the browser output stream // (hopefully this isn't too slow!) PdfCopy writer = null; // open the document if (document == null) { document = new Document(reader.getPageSizeWithRotation(1)); writer = new PdfCopy(document, inOut); if (instructions.isLock()) { writer.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.STRENGTH128BITS); } writer.setFullCompression(); document.open(); } for (Iterator iterator = inRoot.elementIterator("document"); iterator.hasNext();) { Element doc = (Element) iterator.next(); target.seek(0); reader = new PdfReader(target, null); addData(reader, writer, document, doc, instructions); } document.close(); writer.flush(); writer.close(); } private void addData(PdfReader target, PdfCopy inWriter, Document inDocument, Element inDoc, PDFInstructions inStructions) throws DocumentException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(target, baos); stamper.setFullCompression(); // stamper.setEncryption(null, null,PdfWriter.AllowPrinting // ,PdfWriter.STRENGTH40BITS); stamper.setFormFlattening(inStructions.isFlatten()); AcroFields form = stamper.getAcroFields(); log.info("form has the following fields: " + form.getFields().keySet()); for (Iterator iterator = inDoc.elementIterator("property"); iterator.hasNext();) { Element prop = (Element) iterator.next(); String val = prop.getText(); String field = prop.attributeValue("name"); String type = prop.attributeValue("type"); if (type == null || "text".equals(type)) { boolean success = form.setField(field, val); log.info("field" + field + "val" + val + success); } else if ("image".equals(type)) { // form.setField(field, val); String page = prop.attributeValue("path"); // Page inputImage = getPageManager().getPage(page); String path = getPageManager().getPage(page).getContentItem().getActualPath(); File input = new File(getRoot(), path); Image image = Image.getInstance(input.getAbsolutePath()); boolean centered = false; float photo[] = new float[5]; String llx = prop.attributeValue("llx"); String lly = prop.attributeValue("lly"); photo[0] = 1.0f; photo[1] = Float.parseFloat(llx); photo[2] = Float.parseFloat(lly); String rotation = prop.attributeValue("rotate"); if (rotation != null) { float degrees = Float.parseFloat(rotation); image.setRotationDegrees(degrees); } String width = prop.attributeValue("width"); String height = prop.attributeValue("height"); if (width != null) { image.scaleToFit(Float.parseFloat(width), Float.parseFloat(height)); } else { Rectangle rect = new Rectangle(photo[1], photo[2], photo[3], photo[4]); image.scaleToFit(rect.getWidth(), rect.getHeight()); } if (centered) { Rectangle rect = new Rectangle(photo[1], photo[2], photo[3], photo[4]); image.setAbsolutePosition(photo[1] + (rect.getWidth() - image.getScaledWidth()) / 2, photo[2] + (rect.getHeight() - image.getScaledHeight()) / 2); } else { // bottom corner image.setAbsolutePosition(photo[1], photo[2]); } PdfContentByte cb = stamper.getOverContent((int) photo[0]); cb.addImage(image); } else if ("qrcode".equals(type)) { // String page = prop.attributeValue("path"); // // Page inputImage = getPageManager().getPage(page); // String path = getPageManager().getPage(page).getContentItem() // .getActualPath(); // // File input = new File(getRoot(), path); // // String barcode = val; // BarcodeQRCode qrcode = new BarcodeQRCode( // "Moby Dick by Herman Melville", 1, 1, null); // Image image = qrcode.getImage(); // // boolean centered = false; // float photo[] = new float[5]; // String llx = prop.attributeValue("llx"); // String lly = prop.attributeValue("lly"); // // photo[0] = 1.0f; // photo[1] = Float.parseFloat(llx); // photo[2] = Float.parseFloat(lly); // // String rotation = prop.attributeValue("rotate"); // if (rotation != null) { // float degrees = Float.parseFloat(rotation); // image.setRotationDegrees(degrees); // } // // String width = prop.attributeValue("width"); // String height = prop.attributeValue("height"); // // if (width != null) { // image.scaleToFit(Float.parseFloat(width), // Float.parseFloat(height)); // // } else { // Rectangle rect = new Rectangle(photo[1], photo[2], // photo[3], photo[4]); // image.scaleToFit(rect.getWidth(), rect.getHeight()); // } // // if (centered) { // Rectangle rect = new Rectangle(photo[1], photo[2], // photo[3], photo[4]); // image.setAbsolutePosition( // photo[1] // + (rect.getWidth() - image.getScaledWidth()) // / 2, // photo[2] // + (rect.getHeight() - image // .getScaledHeight()) / 2); // } else { // // bottom corner // image.setAbsolutePosition(photo[1], photo[2]); // } // PdfContentByte cb = stamper.getOverContent((int) photo[0]); // // Rectangle rect = new Rectangle(photo[1], photo[2], photo[3], // photo[4]); // String code = form.getField(field); // form.removeField(code); // // image.scaleToFit(rect.getWidth(), rect.getHeight()); // image.setAbsolutePosition(photo[1], photo[2]); // cb.addImage(image); } } stamper.close(); PdfReader fromstream = new PdfReader(baos.toByteArray()); for (int i = 0; i < fromstream.getNumberOfPages(); i++) { inWriter.addPage(inWriter.getImportedPage(fromstream, i + 1)); } } public Map loadFormData(Page inPage) throws Exception { Document document = null; PdfCopy writer = null; RandomAccessFileOrArray target; PdfReader reader; target = new RandomAccessFileOrArray(inPage.getInputStream()); reader = new PdfReader(target, null); AcroFields form = reader.getAcroFields(); log.info("form has the following fields: " + form.getFields().keySet()); HashMap map = new HashMap(); for (Iterator iterator = form.getFields().keySet().iterator(); iterator.hasNext();) { String key = (String) iterator.next(); String val = form.getField(key); map.put(key, val); } return map; } public String getTextForPage(ContentItem inItem, int pagenumber) { try { PDFTextStripper pdfStripper = null; PDDocument pdDoc = null; PDFParser parser = new PDFParser(new RandomAccessFile(new File(inItem.getAbsolutePath()), "r")); parser.parse(); pdfStripper = new PDFTextStripper(); COSDocument cosDoc = parser.getDocument(); pdDoc = new PDDocument(cosDoc); pdfStripper.setStartPage(pagenumber); pdfStripper.setEndPage(pagenumber); String parsedText = pdfStripper.getText(pdDoc); System.out.println(parsedText); return parsedText; } catch (Exception e) { throw new OpenEditException(e); } } }