List of usage examples for com.itextpdf.text.pdf PdfStamper PdfStamper
public PdfStamper(final PdfReader reader, final OutputStream os) throws DocumentException, IOException
From source file:learn.PdfRendererBasicFragment.java
License:Apache License
public void merge(int pageNum) { try {//w w w. ja va 2 s. c om Image image = Image.getInstance(signatureByte); pdfReader = new PdfReader(path); //fix y Matrix matrix = mSignatureImage.getImageMatrix(); // Get the values of the matrix float[] values = new float[9]; matrix.getValues(values); float relativeX = (mSignatureImage.getX() - values[2]) / values[0]; float relativeY = (mSignatureImage.getY() - values[5]) / values[4]; x = relativeX; y = relativeY; if (pageNum != -1) { y = pdfReader.getCropBox(pageNum).getHeight() - y; y -= mSign.getHeight(); } PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(newP)); if (pageNum == -1) { y = pdfReader.getCropBox(1).getHeight() - y; y -= mSign.getHeight(); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { //put content under PdfContentByte content;// = pdfStamper.getUnderContent(i); // image.setAbsolutePosition(x, y); // content.addImage(image); //put content over content = pdfStamper.getOverContent(i); image.setAbsolutePosition(x, y); content.addImage(image); //Text over the existing page /*BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); content.beginText(); content.setFontAndSize(bf, 18); content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Page No: " + i, 430, 15, 0); content.endText();*/ } } else { PdfContentByte content = pdfStamper.getOverContent(pageNum); image.setAbsolutePosition(x, y); content.addImage(image); } pdfStamper.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Logica.LogicaReserva.java
private void GenerarDocumento(Reserva nuevaReserva) throws IOException, DocumentException { //String dirPath = "C:\\"; String fileName = "Base reserva.pdf"; HashMap fieldsWithValues = new HashMap(); ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(fileName); PdfStamper stamper = new PdfStamper(reader, baosPDF); AcroFields form = stamper.getAcroFields(); HashMap fields = (HashMap) form.getFields(); Set keys = fields.keySet();/*from w ww. ja v a2s.c om*/ //Metodo que retorna map de datos que queremos obtener de objeto para agregar a PDF fieldsWithValues = crearHashMapReserva(nuevaReserva, keys); //Iteracion sobre campos de pdf Iterator itr = keys.iterator(); while (itr.hasNext()) { String fieldName = (String) itr.next(); String fieldValue = fieldsWithValues.get(fieldName) != null ? (String) (fieldsWithValues.get(fieldName)) : ""; form.setField(fieldName, fieldValue); form.setFieldProperty(fieldName, "setfflags", PdfFormField.FF_READ_ONLY, null); } stamper.setFormFlattening(true); stamper.close(); reader.close(); //Guardando cambios String nombre; DateFormat fecha = new SimpleDateFormat("yyyy_MM_dd HH_mm_ss"); nombre = fecha.format(nuevaReserva.getFechaHasta()); String nombreydir = "Documentos\\Documento Reserva -" + nombre + "-.pdf"; OutputStream pdf = new FileOutputStream(nombreydir); baosPDF.writeTo(pdf); pdf.close(); Hilo h1 = new Hilo("email", nombreydir, nuevaReserva.getCliente().getCorreo()); h1.start(); try { File archivo = new File(nombreydir); Desktop.getDesktop().open(archivo); } catch (IOException ex) { } //EnvioEmail(nombreydir, nuevaReserva.getCliente().getCorreo()); }
From source file:managedbeans.descargas.PDFConversionDemo.java
public static void main(String[] args) { try {//from ww w . j a v a 2 s . com //Read file using PdfReader PdfReader pdfReader = new PdfReader("HelloWorld.pdf"); //Modify file using PdfReader PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("HelloWorld-modified.pdf")); Image image = Image.getInstance("temp.png"); image.scaleAbsolute(100, 50); image.setAbsolutePosition(100f, 700f); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte content = pdfStamper.getUnderContent(i); content.addImage(image); } pdfStamper.close(); } catch (IOException | DocumentException e) { logger.warn("failed to ...." + e); } }
From source file:me.Aron.Heinecke.fbot.lib.Converter.java
License:Apache License
/*** * Add a note to the bottom of a pdf file in italic font * @param rfile file to be read from/*from w w w . j av a2s . c om*/ * @param wfile file to be written to * @param text text to add * @return path to the resulting pdf, null if it failed */ private String addPDFNote(File rfile, File wfile, String text) { try { PdfReader pdfReader = new PdfReader(rfile.getAbsolutePath()); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(wfile)); for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { PdfContentByte cb = pdfStamper.getUnderContent(i); BaseFont bf = BaseFont.createFont(); bf.setPostscriptFontName("ITALIC"); cb.beginText(); cb.setFontAndSize(bf, 12); cb.setTextMatrix(10, 20); cb.showText(text); cb.endText(); } pdfStamper.close(); return wfile.getAbsolutePath(); } catch (IOException | DocumentException e) { fbot.getLogger().exception("converter", e); return null; } }
From source file:my.charpdf.DandDcharPDFUI.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String inputFile;//from www . j av a 2 s . c om inputFile = jTextField1.getText().replace("\n", "").replace("\r", ""); String pcName = ""; String pcRace = ""; String pcAlign = ""; String pcBackG = ""; String pcExp = ""; String pcProfBonus = ""; String pcStrScore = ""; String pcStrMod = ""; String pcDexScore = ""; String pcDexMod = ""; String pcConScore = ""; String pcConMod = ""; String pcIntScore = ""; String pcIntMod = ""; String pcWisScore = ""; String pcWisMod = ""; String pcChaScore = ""; String pcChaMod = ""; String pcClassLevel = ""; String pcPerc = ""; if (!inputFile.equals("")) { try { Builder parser = new Builder(); Document doc = parser.build("file:///" + inputFile); Element root = doc.getRootElement(); Elements character = root.getChildElements(); pcName = character.get(0).getFirstChildElement("name").getValue(); pcRace = character.get(0).getFirstChildElement("race").getValue(); pcExp = character.get(0).getFirstChildElement("exp").getValue(); pcAlign = character.get(0).getFirstChildElement("alignment").getValue(); pcBackG = character.get(0).getFirstChildElement("background").getValue(); pcProfBonus = character.get(0).getFirstChildElement("profbonus").getValue(); pcPerc = character.get(0).getFirstChildElement("perception").getValue(); //Integer numChildren = character.get(0).getChildCount(); //System.out.println(numChildren); Elements pcAttrs = character.get(0).getChildElements("abilities").get(0).getChildElements(); Elements pcClasses = character.get(0).getChildElements("classes").get(0).getChildElements(); for (int i = 0; i < pcAttrs.size(); i++) { if (pcAttrs.get(i).getLocalName().equals("strength")) { pcStrScore = pcAttrs.get(i).getChildElements("score").get(0).getValue(); pcStrMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue(); } else if (pcAttrs.get(i).getLocalName().equals("dexterity")) { pcDexScore = pcAttrs.get(i).getChildElements("score").get(0).getValue(); pcDexMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue(); } else if (pcAttrs.get(i).getLocalName().equals("constitution")) { pcConScore = pcAttrs.get(i).getChildElements("score").get(0).getValue(); pcConMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue(); } else if (pcAttrs.get(i).getLocalName().equals("intelligence")) { pcIntScore = pcAttrs.get(i).getChildElements("score").get(0).getValue(); pcIntMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue(); } else if (pcAttrs.get(i).getLocalName().equals("wisdom")) { pcWisScore = pcAttrs.get(i).getChildElements("score").get(0).getValue(); pcWisMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue(); } else if (pcAttrs.get(i).getLocalName().equals("charisma")) { pcChaScore = pcAttrs.get(i).getChildElements("score").get(0).getValue(); pcChaMod = pcAttrs.get(i).getChildElements("bonus").get(0).getValue(); } } for (int i = 0; i < pcClasses.size(); i++) { // Gets the list of classes //System.out.println(pcClasses.get(i).getLocalName()); String tempClass = pcClasses.get(i).getChildElements("name").get(0).getValue(); String tempLevel = pcClasses.get(i).getChildElements("level").get(0).getValue(); pcClassLevel += tempClass + " " + tempLevel + " / "; } pcClassLevel = pcClassLevel.substring(0, pcClassLevel.length() - 2); //for(i = 0; i < numClasses; i++) { // System.out.println(charac); //} String inputTemplate = "resources/DandD5e-template.pdf"; String outputPDF = "resources/" + pcName + ".pdf"; PdfReader reader = new PdfReader(inputTemplate); PdfStamper stamper; stamper = new PdfStamper(reader, new FileOutputStream(outputPDF)); AcroFields form = reader.getAcroFields(); Set<String> fields = form.getFields().keySet(); for (String key : fields) { //System.out.println(key); switch (form.getFieldType(key)) { case AcroFields.FIELD_TYPE_CHECKBOX: //System.out.println(key + ": Checkbox"); break; case AcroFields.FIELD_TYPE_COMBO: //System.out.println(key + ": Combo"); break; case AcroFields.FIELD_TYPE_LIST: //System.out.println(key + ": List"); break; case AcroFields.FIELD_TYPE_NONE: //System.out.println(key + ": None"); break; case AcroFields.FIELD_TYPE_PUSHBUTTON: //System.out.println(key + ": Pushbutton"); break; case AcroFields.FIELD_TYPE_RADIOBUTTON: //System.out.println(key + ": Radio"); break; case AcroFields.FIELD_TYPE_SIGNATURE: //System.out.println(key + ": Signature"); break; case AcroFields.FIELD_TYPE_TEXT: //System.out.println(key + ": Text"); break; default: //System.out.println(key + ": ???"); } } stamper.getAcroFields().setField("Race ", pcRace); stamper.getAcroFields().setField("CharacterName", pcName); stamper.getAcroFields().setField("XP", pcExp); stamper.getAcroFields().setField("Alignment", pcAlign); stamper.getAcroFields().setField("Background", pcBackG); int tempPB = Integer.parseInt(pcProfBonus); if (tempPB > 0) { pcProfBonus = "+" + pcProfBonus; } stamper.getAcroFields().setField("ProfBonus", pcProfBonus); //Attributes stamper.getAcroFields().setField("STR", pcStrScore); stamper.getAcroFields().setField("STRmod", pcStrMod); stamper.getAcroFields().setField("DEX", pcDexScore); stamper.getAcroFields().setField("DEXmod ", pcDexMod); stamper.getAcroFields().setField("CON", pcConScore); stamper.getAcroFields().setField("CONmod", pcConMod); stamper.getAcroFields().setField("INT", pcIntScore); stamper.getAcroFields().setField("INTmod", pcIntMod); stamper.getAcroFields().setField("WIS", pcWisScore); stamper.getAcroFields().setField("WISmod", pcWisMod); stamper.getAcroFields().setField("CHA", pcChaScore); stamper.getAcroFields().setField("CHamod", pcChaMod); stamper.getAcroFields().setField("ClassLevel", pcClassLevel); stamper.getAcroFields().setField("Passive", pcPerc); stamper.close(); reader.close(); } catch (java.io.IOException | DocumentException e) { System.err.println("1st Catch, that didn't go well: " + e.getMessage()); } catch (ParsingException e) { System.err.println("2nd Catch, that didn't go well: " + e.getMessage()); } } }
From source file:net.algem.edition.PdfHandler.java
License:Open Source License
public void createPdf(String fileName, ByteArrayOutputStream out, short templateType) throws IOException, DocumentException { try {/*from www .ja va 2 s .c o m*/ File tmpFile = File.createTempFile(fileName, ".pdf"); final String target = tmpFile.getPath(); PageTemplate pt = getTemplate(templateType); PdfReader reader = new com.itextpdf.text.pdf.PdfReader(out.toByteArray()); if (pt != null) { PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(target)); // PdfStamper stamper = new PdfStamper(reader, new PrintStream(new FileOutputStream(target), true, "UTF-8")); PdfReader model = new com.itextpdf.text.pdf.PdfReader(pt.getContent()); PdfImportedPage importedPage = stamper.getImportedPage(model, 1); for (int i = 1; i <= reader.getNumberOfPages(); i++) { PdfContentByte canvas = stamper.getUnderContent(i); canvas.addTemplate(importedPage, 0, 0); } stamper.getWriter().freeReader(model); model.close(); stamper.close(); } else { PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(target)); //PdfStamper stamper = new PdfStamper(reader, new PrintStream(new FileOutputStream(target), true, "UTF-8")); stamper.close(); } preview(target, null); } catch (SQLException ex) { GemLogger.logException(ex); } }
From source file:net.digitstar.vanadio.AbstractReportPdf.java
License:Apache License
private void closeDocument(Document document, ByteArrayOutputStream baos, OutputStream out) { if (document != null) { document.close();/*from w w w . jav a 2s . c o m*/ PdfStamper writer = null; try { PdfReader reader = new PdfReader(baos.toByteArray()); writer = new PdfStamper(reader, out); afterCloseDocument(document, reader, writer); } catch (IOException e) { /**/ } catch (DocumentException e) { /**/ } finally { if (writer != null) try { writer.close(); } catch (DocumentException e) { /**/} catch (IOException e) { /**/} } } }
From source file:net.ionescu.jhocr2pdf.Jhocr2pdf.java
License:Open Source License
/** * @param args/*from www . j av a 2s .com*/ * The arguments are: * [options] output_pdf * The name of the output PDF file with the added OCR information must be the last argument * Other * flags: * -i pdf_file PDF file containing the TIFF images * -hocr html_file hOCR file generated by Tesseract * -visible render the text above the image (mostry for debugging) * -font font_path path to the font file to use * -hocrnameformat string to use to construct the name of the hocr file depending on page number * * Usage example: * java -jar jhorc2pdf.jar -font /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf -i input.pdf -hocr input.html output.pdf * * @throws SAXException * @throws IOException * @throws DocumentException */ public static void main(String[] args) throws SAXException, IOException, DocumentException { /* * process input parameters * * -visible -font font_path inputPDF inputOCR outputPDF */ boolean visible = false; String fontPath = null; String fileNameFormat = null; String inputPDF = null; String inputHOCR = null; String outputPDF = null; for (int i = 0; i < args.length; i++) { if (args[i].startsWith("-")) { switch (args[i]) { case "-visible": visible = true; break; case "-font": fontPath = args[++i]; break; case "-hocrnameformat": fileNameFormat = args[++i]; break; case "-i": inputPDF = args[++i]; break; case "-hocr": inputHOCR = args[++i]; break; default: System.err.println("Invalid parameter: " + args[i]); System.exit(1); } } else { if (i < args.length - 1) { System.err.println("Ouput file name should be the last argument"); System.exit(1); } outputPDF = args[i]; } } log.fine("load the PDF file, initialize the Stamper"); PdfReader reader = new PdfReader(inputPDF); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPDF)); Map<String, String> info = reader.getInfo(); info.put("Title", new File(outputPDF).getName()); stamper.setMoreInfo(info); Jhocr2pdf ocrStamp = new Jhocr2pdf(stamper, visible, fontPath); if (null != fileNameFormat) { log.fine("iterate through all the pages looking for separate hocr files"); for (int i = 1, maxPages = reader.getNumberOfPages() + 1; i < maxPages; i++) { String hocrFileName = String.format(fileNameFormat, i); try { ocrStamp.parse(hocrFileName, i); } catch (SAXException saxException) { System.err.println("Error processing hocr file: " + hocrFileName); throw saxException; } } } else { ocrStamp.parse(inputHOCR, -1); } stamper.close(); }
From source file:nz.ac.waikato.cms.doc.OverlayFilename.java
License:Open Source License
/** * Performs the overlay.//from ww w . j a v a 2 s. c om * * @param input the input file/dir * @param output the output file/dir * @param vpos the vertical position * @param hpos the horizontal position * @param stripPath whether to strip the path * @param stripExt whether to strip the extension * @param pages the array of pages (1-based) to add the overlay to, null for all * @param evenPages whether to enforce even pages in the document * @return true if successfully overlay */ public boolean overlay(File input, File output, int vpos, int hpos, boolean stripPath, boolean stripExt, int[] pages, boolean evenPages) { PdfReader reader; PdfStamper stamper; FileOutputStream fos; PdfContentByte canvas; int i; String text; int numPages; File tmpFile; Document document; PdfWriter writer; PdfImportedPage page; PdfContentByte cb; reader = null; stamper = null; fos = null; numPages = -1; try { reader = new PdfReader(input.getAbsolutePath()); fos = new FileOutputStream(output.getAbsolutePath()); stamper = new PdfStamper(reader, fos); numPages = reader.getNumberOfPages(); if (pages == null) { pages = new int[reader.getNumberOfPages()]; for (i = 0; i < pages.length; i++) pages[i] = i + 1; } if (stripPath) text = input.getName(); else text = input.getAbsolutePath(); if (stripExt) text = text.replaceFirst("\\.[pP][dD][fF]$", ""); for (i = 0; i < pages.length; i++) { canvas = stamper.getOverContent(pages[i]); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Paragraph(text), hpos, vpos, 0.0f); } } catch (Exception e) { System.err.println("Failed to process " + input + ":"); e.printStackTrace(); return false; } finally { try { if (stamper != null) stamper.close(); } catch (Exception e) { // ignored } try { if (reader != null) reader.close(); } catch (Exception e) { // ignored } try { if (fos != null) { fos.flush(); fos.close(); } } catch (Exception e) { // ignored } } // enforce even pages? if (evenPages && (numPages > 0) && (numPages % 2 == 1)) { reader = null; fos = null; writer = null; tmpFile = new File(output.getAbsolutePath() + "tmp"); try { if (!output.renameTo(tmpFile)) { System.err.println("Failed to rename '" + output + "' to '" + tmpFile + "'!"); return false; } reader = new PdfReader(tmpFile.getAbsolutePath()); document = new Document(reader.getPageSize(1)); fos = new FileOutputStream(output.getAbsoluteFile()); writer = PdfWriter.getInstance(document, fos); document.open(); document.addCreationDate(); document.addAuthor(System.getProperty("user.name")); cb = writer.getDirectContent(); for (i = 0; i < reader.getNumberOfPages(); i++) { page = writer.getImportedPage(reader, i + 1); document.newPage(); cb.addTemplate(page, 0, 0); } document.newPage(); document.add(new Paragraph(" ")); // fake content document.close(); } catch (Exception e) { System.err.println("Failed to process " + tmpFile + ":"); e.printStackTrace(); return false; } finally { try { if (fos != null) { fos.flush(); fos.close(); } } catch (Exception e) { // ignored } try { if (reader != null) reader.close(); } catch (Exception e) { // ignored } try { if (writer != null) writer.close(); } catch (Exception e) { // ignored } if (tmpFile.exists()) { try { tmpFile.delete(); } catch (Exception e) { // ignored } } } } return true; }
From source file:nz.ac.waikato.cms.doc.SimplePDFOverlay.java
License:Open Source License
/** * Applies the instructions to the input PDF. * * @return null if successful, otherwise error message *//*from w w w . j av a2 s . co m*/ public String execute() { String result; String line; BufferedReader breader; FileReader freader; int i; int lineNo; String units; int pageNo; PdfReader reader; PdfStamper stamper; PdfContentByte cb; ColumnText ct; Font font; String[] parts; StringBuilder text; result = null; freader = null; breader = null; try { reader = new PdfReader(new FileInputStream(m_Pdf.getAbsolutePath())); stamper = new PdfStamper(reader, new FileOutputStream(m_Output.getAbsolutePath())); freader = new FileReader(m_Instructions); breader = new BufferedReader(freader); lineNo = 0; units = "pt"; pageNo = 1; cb = stamper.getOverContent(pageNo); font = null; while ((line = breader.readLine()) != null) { lineNo++; if (line.trim().startsWith(PREFIX_COMMENT)) continue; if (line.trim().length() == 0) continue; if (line.startsWith(PREFIX_UNITS)) { units = line.substring(PREFIX_UNITS.length()).trim().toLowerCase(); } else if (line.startsWith(PREFIX_PAGE)) { pageNo = Integer.parseInt(line.substring(PREFIX_PAGE.length()).trim()); cb = stamper.getOverContent(pageNo); } else if (line.startsWith(PREFIX_FONT)) { parts = line.substring(PREFIX_FONT.length()).trim().split(" "); if (parts.length == 3) font = FontFactory.getFont(parts[0], Float.parseFloat(parts[1]), new BaseColor(parseColor(parts[2]).getRGB())); else m_Logger.warning("Font instruction not in expected format (" + FORMAT_FONT + "):\n" + line); } else if (line.startsWith(PREFIX_TEXT)) { parts = line.substring(PREFIX_TEXT.length()).trim().split(" "); if (parts.length >= 7) { ct = new ColumnText(cb); ct.setSimpleColumn(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // llx parseLocation(parts[1], reader.getPageSize(pageNo).getHeight(), units), // lly parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // urx parseLocation(parts[3], reader.getPageSize(pageNo).getHeight(), units), // ury Float.parseFloat(parts[4]), // leading parseAlignment(parts[5])); // alignment text = new StringBuilder(); for (i = 6; i < parts.length; i++) { if (text.length() > 0) text.append(" "); text.append(parts[i]); } if (font == null) ct.setText(new Phrase(text.toString())); else ct.setText(new Phrase(text.toString(), font)); ct.go(); } else { m_Logger.warning("Text instruction not in expected format (" + FORMAT_TEXT + "):\n" + line); } } else if (line.startsWith(PREFIX_LINE)) { parts = line.substring(PREFIX_LINE.length()).trim().split(" "); if (parts.length >= 6) { cb.saveState(); cb.setLineWidth(Float.parseFloat(parts[4])); // line width cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // color cb.moveTo(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units)); // y cb.lineTo(parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // w parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units)); // h cb.stroke(); cb.restoreState(); } else { m_Logger.warning("Line instruction not in expected format (" + FORMAT_LINE + "):\n" + line); } } else if (line.startsWith(PREFIX_RECT)) { parts = line.substring(PREFIX_RECT.length()).trim().split(" "); if (parts.length >= 6) { cb.saveState(); cb.rectangle(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units), // y parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // w parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units) // h ); cb.setLineWidth(Float.parseFloat(parts[4])); // line width cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // stroke if (parts.length >= 7) { cb.setColorFill(new BaseColor(parseColor(parts[6]).getRGB())); // fill cb.fillStroke(); } else { cb.stroke(); } cb.restoreState(); } else { m_Logger.warning( "Rectangle instruction not in expected format (" + FORMAT_RECT + "):\n" + line); } } else if (line.startsWith(PREFIX_OVAL)) { parts = line.substring(PREFIX_OVAL.length()).trim().split(" "); if (parts.length >= 6) { cb.saveState(); cb.ellipse(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x1 parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units), // y1 parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // x2 parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units) // y2 ); cb.setLineWidth(Float.parseFloat(parts[4])); // line width cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // stroke if (parts.length >= 7) { cb.setColorFill(new BaseColor(parseColor(parts[6]).getRGB())); // fill cb.fillStroke(); } else { cb.stroke(); } cb.restoreState(); } else { m_Logger.warning("Oval instruction not in expected format (" + FORMAT_OVAL + "):\n" + line); } } else { m_Logger.warning("Unknown command on line #" + lineNo + ":\n" + line); } } stamper.close(); } catch (Exception e) { result = "Failed to process!\n" + Utils.throwableToString(e); } finally { FileUtils.closeQuietly(breader); FileUtils.closeQuietly(freader); } return result; }