List of usage examples for com.itextpdf.text.pdf PdfWriter addDirectImageSimple
public PdfName addDirectImageSimple(final Image image) throws PdfException, DocumentException
From source file:cz.muni.pdfjbim.PdfImageProcessor.java
License:Apache License
/** * replace images by they recompressed version according to JBIG2 standard * positions and image data given in imagesData * @param pdfName represents name of original PDF file * @param os represents output stream for writing changed PDF file * @param imagesData contains compressed images according to JBIG2 standard and informations about them * @throws PdfRecompressionException if version of PDF is lower than 1.4 or was catch DocumentException or IOException *//* w w w . j a v a2 s . com*/ public void replaceImageUsingIText(String pdfName, OutputStream os, Jbig2ForPdf imagesData) throws PdfRecompressionException { if (pdfName == null) { throw new NullPointerException("pdfName"); } if (os == null) { throw new NullPointerException("os"); } if (imagesData == null) { throw new NullPointerException("imagesData is null => nothing to recompress"); } Map<PdfObjId, PdfImage> jbig2Images = imagesData.getMapOfJbig2Images(); PdfReader pdf; PdfStamper stp = null; try { pdf = new PdfReader(pdfName); stp = new PdfStamper(pdf, os); PdfWriter writer = stp.getWriter(); int version; if ((version = Integer.parseInt(String.valueOf(pdf.getPdfVersion()))) < 4) { writer.setPdfVersion(PdfWriter.PDF_VERSION_1_4); } Iterator itImages = jbig2Images.values().iterator(); String key; if (itImages.hasNext()) { PdfImage myImg = (PdfImage) itImages.next(); key = myImg.getPdfImageInformation().getKey(); } else { key = "im0"; } for (int pageNum = 1; pageNum <= pdf.getNumberOfPages(); pageNum++) { PdfDictionary pg = pdf.getPageN(pageNum); PdfDictionary resPg = (PdfDictionary) PdfReader.getPdfObject(pg.get(PdfName.RESOURCES)); PdfDictionary xobjResPg = (PdfDictionary) PdfReader.getPdfObject(resPg.get(PdfName.XOBJECT)); PdfObject obj = null; if (xobjResPg != null) { for (Iterator it = xobjResPg.getKeys().iterator(); it.hasNext();) { PdfObject pdfObjIndirect = xobjResPg.get((PdfName) it.next()); if (pdfObjIndirect.isIndirect()) { PdfDictionary pdfObj2 = (PdfDictionary) PdfReader.getPdfObject(pdfObjIndirect); PdfDictionary xobj2Res = (PdfDictionary) PdfReader .getPdfObject(pdfObj2.get(PdfName.RESOURCES)); if (xobj2Res != null) { for (Iterator it2 = xobj2Res.getKeys().iterator(); it2.hasNext();) { PdfObject resObj = xobj2Res.get((PdfName) it2.next()); } PdfDictionary xobj = (PdfDictionary) PdfReader .getPdfObject(xobj2Res.get(PdfName.XOBJECT)); if (xobj == null) { continue; } obj = xobj.get(new PdfName(key)); } else { obj = xobjResPg.get(new PdfName(key)); if (obj == null) { obj = pdfObjIndirect; } } } } } if ((obj != null) && (obj.isIndirect())) { PdfDictionary tg = (PdfDictionary) PdfReader.getPdfObject(obj); if (tg == null) { continue; } PdfName type = (PdfName) PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE)); if (PdfName.IMAGE.equals(type)) { PRIndirectReference ref = (PRIndirectReference) obj; PdfObjId imId = new PdfObjId(ref.getNumber(), ref.getGeneration()); PdfImage jbImage = jbig2Images.get(imId); if (jbImage == null) { continue; } PdfImageInformation jbImageInfo = jbImage.getPdfImageInformation(); Image img = Image.getInstance(jbImageInfo.getWidth(), jbImageInfo.getHeight(), jbImage.getImageData(), imagesData.getGlobalData()); PdfReader.killIndirect(obj); Image maskImage = img.getImageMask(); if (maskImage != null) { writer.addDirectImageSimple(maskImage); } writer.addDirectImageSimple(img, (PRIndirectReference) obj); } } } stp.close(); } catch (IOException ioEx) { throw new PdfRecompressionException(ioEx); } catch (DocumentException dEx) { throw new PdfRecompressionException(dEx); } finally { Tools.deleteFilesFromList(imagesData.getJbFiles().toArray(new File[0])); } }
From source file:cz.muni.pdfjbim.PdfImageReplacer.java
License:Apache License
/** * replace images by they recompressed version according to JBIG2 standard positions and image * data given in imagesData/*w w w.j a v a 2s . co m*/ * * @param originalPdf represents name of original PDF file * @param os represents output stream for writing changed PDF file * @param imagesData contains compressed images according to JBIG2 standard and informations * about them * @throws PdfRecompressionException if version of PDF is lower than 1.4 or was catch * DocumentException or IOException */ public void replaceImageUsingIText(InputStream originalPdf, OutputStream os, List<Jbig2ForPdf> imagesDataList) throws PdfRecompressionException { if (originalPdf == null) { throw new NullPointerException("pdfName"); } if (os == null) { throw new NullPointerException("os"); } if (imagesDataList == null) { throw new NullPointerException("imagesData is null => nothing to recompress"); } log.info("Replacing old images in PDF with their equivalent encoded according to standard JBIG2"); PdfReader pdf; PdfStamper stp = null; try { pdf = new PdfReader(originalPdf); stp = new PdfStamper(pdf, os); PdfWriter writer = stp.getWriter(); int version; if ((version = Integer.parseInt(String.valueOf(pdf.getPdfVersion()))) < 4) { log.debug("PDF version of original PDF was {} => changing to PDF version 1.4", pdf.getPdfVersion()); writer.setPdfVersion(PdfWriter.PDF_VERSION_1_4); } for (Jbig2ForPdf imagesData : imagesDataList) { Map<PdfObjId, PdfImage> jbig2Images = imagesData.getMapOfJbig2Images(); Iterator itImages = jbig2Images.values().iterator(); String key; if (itImages.hasNext()) { PdfImage myImg = (PdfImage) itImages.next(); key = myImg.getPdfImageInformation().getKey(); } else { key = "im0"; } for (int pageNum = 1; pageNum <= pdf.getNumberOfPages(); pageNum++) { PdfDictionary pg = pdf.getPageN(pageNum); PdfDictionary resPg = (PdfDictionary) PdfReader.getPdfObject(pg.get(PdfName.RESOURCES)); PdfDictionary xobjResPg = (PdfDictionary) PdfReader.getPdfObject(resPg.get(PdfName.XOBJECT)); PdfObject obj = null; if (xobjResPg != null) { for (Iterator it = xobjResPg.getKeys().iterator(); it.hasNext();) { PdfObject pdfObjIndirect = xobjResPg.get((PdfName) it.next()); if (pdfObjIndirect.isIndirect()) { PdfDictionary pdfObj2 = (PdfDictionary) PdfReader.getPdfObject(pdfObjIndirect); PdfDictionary xobj2Res = (PdfDictionary) PdfReader .getPdfObject(pdfObj2.get(PdfName.RESOURCES)); if (xobj2Res != null) { for (Iterator it2 = xobj2Res.getKeys().iterator(); it2.hasNext();) { PdfObject resObj = xobj2Res.get((PdfName) it2.next()); } PdfDictionary xobj = (PdfDictionary) PdfReader .getPdfObject(xobj2Res.get(PdfName.XOBJECT)); if (xobj == null) { continue; } obj = xobj.get(new PdfName(key)); } else { obj = xobjResPg.get(new PdfName(key)); if (obj == null) { obj = pdfObjIndirect; } } } } } if ((obj != null) && (obj.isIndirect())) { PdfDictionary tg = (PdfDictionary) PdfReader.getPdfObject(obj); if (tg == null) { continue; } PdfName type = (PdfName) PdfReader.getPdfObject(tg.get(PdfName.SUBTYPE)); if (PdfName.IMAGE.equals(type)) { PRIndirectReference ref = (PRIndirectReference) obj; PdfObjId imId = new PdfObjId(ref.getNumber(), ref.getGeneration()); PdfImage jbImage = jbig2Images.get(imId); if (jbImage == null) { continue; } log.debug("Replacing image {}", jbImage); PdfImageInformation jbImageInfo = jbImage.getPdfImageInformation(); Image img = Image.getInstance(jbImageInfo.getWidth(), jbImageInfo.getHeight(), jbImage.getImageData(), imagesData.getGlobalData()); PdfReader.killIndirect(obj); Image maskImage = img.getImageMask(); if (maskImage != null) { writer.addDirectImageSimple(maskImage); } writer.addDirectImageSimple(img, (PRIndirectReference) obj); } } } } } catch (IOException ioEx) { throw new PdfRecompressionException(ioEx); } catch (DocumentException dEx) { throw new PdfRecompressionException(dEx); } finally { log.debug("Deleting temporary files created during process of PDF recompression"); for (Jbig2ForPdf imagesData : imagesDataList) { Tools.deleteFilesFromList(imagesData.getJbFiles().toArray(new File[0])); } try { if (stp != null) { stp.close(); } } catch (DocumentException ex) { log.error("Exception thrown while closing stream", ex); } catch (IOException ex) { log.error("Exception thrown while closing stream", ex); } } }
From source file:itext_result.Main.java
private int developScreeningSheet() { new SwingWorker<Object, Object>() { String filename;// w ww . j a v a 2 s . c o m @Override protected void done() { // ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. "); } public PdfPCell createBarcode(PdfWriter writer, String code) throws DocumentException, IOException { BarcodeEAN barcode = new BarcodeEAN(); barcode.setCodeType(Barcode.EAN8); barcode.setCode(code); PdfPCell cell = new PdfPCell( barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true); cell.setPadding(10); return cell; } class ImageContent implements PdfPTableEvent { protected com.itextpdf.text.Image content; public ImageContent(com.itextpdf.text.Image content) { this.content = content; } public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { try { PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; float x = widths[3][1] + 10; float y = heights[3] - 10 - content.getScaledHeight(); content.setAbsolutePosition(x, y); canvas.addImage(content); } catch (DocumentException e) { throw new ExceptionConverter(e); } } } @Override protected Object doInBackground() throws Exception { // System.err.println(" Roll No Received ....." + rollno); Document doc = new Document(); try { PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("12345" + ".pdf")); doc.open(); doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : "); doc.addSubject("Confidential Report Eyes Only"); doc.addKeywords(""); doc.addAuthor("SOS"); doc.addCreator("SOS"); // A4 = 210mm x 297mm ~ 605points x 855points doc.setPageSize(PageSize.A5); doc.setMargins(15f, 15f, 15f, 15f); ///////////////////////////////////////////////////////////// int pageno = 1; for (int i = 0; i == pageno; i++) { // doc.add(imageRight); } PdfContentByte cb = writer.getDirectContent(); //DONE BarcodeEAN codeEAN = new BarcodeEAN(); codeEAN.setCodeType(Barcode.EAN8); Barcode128 code128 = new Barcode128(); code128.setCode(String.valueOf("123456")); Barcode128 code128_jacket = new Barcode128(); code128_jacket.setCode(String.valueOf("10345")); codeEAN.setCode(String.valueOf("123456")); com.itextpdf.text.Image imageEAN = code128.createImageWithBarcode(cb, null, null); // imageEAN.scalePercent(10f); //464f, 725f // imageEAN.setAbsolutePosition(474f, 662f); int i = 1; while (i <= pageno) { doc.newPage(); // cb.addImage(imageRight); // cb.addImage(imageEAN); i++; } com.itextpdf.text.Image carcode = code128_jacket.createImageWithBarcode(cb, null, null); carcode.scaleAbsolute(100f, 35f); carcode.setAbsolutePosition(500f, 600f); writer.addDirectImageSimple(carcode); // cb.addImage(carcode); com.itextpdf.text.Image carcode2 = code128.createImageWithBarcode(cb, null, null); carcode2.scaleAbsolute(100f, 35f); carcode2.setAbsolutePosition(450f, 760f); writer.addDirectImageSimple(carcode2); // cb.addImage(carcode2); Image image1 = Image.getInstance("jklogo.gif"); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(99); table.addCell(image1); doc.add(table); table = new PdfPTable(4); table.setWidthPercentage(99); float[] columnWidths = { 6, 2 }; PdfPTable CandidateTable = new PdfPTable(columnWidths); Font f = new Font(Font.FontFamily.HELVETICA, 7, Font.NORMAL, GrayColor.BLACK); Font fsmall = new Font(Font.FontFamily.HELVETICA, 7, Font.NORMAL, GrayColor.BLACK); PdfPCell rollno = new PdfPCell(new Phrase("Roll No# " + String.valueOf("123456"), fsmall)); PdfPCell cname = new PdfPCell(new Phrase("Name #" + String.valueOf("John Doe"), fsmall)); // DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); // String today = formatter.format(cInfo.getDob()); PdfPCell dob = new PdfPCell(new Phrase("DOB#" + String.valueOf("12-03-1989"), fsmall)); PdfPCell fname = new PdfPCell(new Phrase("F/H Name #" + String.valueOf("Big John"), fsmall)); PdfPCell type = new PdfPCell(new Phrase("Total Time #" + String.valueOf("350.00"), fsmall)); // String scrtoday = formatter.format(cInfo.getScreeningdate()); // System.out.println("Today : " + today); PdfPCell scrdate = new PdfPCell(new Phrase("Laps #" + String.valueOf("4"), fsmall)); PdfPCell cell = new PdfPCell(new Phrase(" ", fsmall)); rollno.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); cname.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); rollno.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); dob.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); fname.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); type.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); scrdate.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); cell.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); CandidateTable.addCell(rollno); CandidateTable.addCell(cell); CandidateTable.addCell(cname); CandidateTable.addCell(cell); CandidateTable.addCell(dob); CandidateTable.addCell(cell); PdfPTable CandidateOtherTable = new PdfPTable(columnWidths); CandidateOtherTable.addCell(fname); CandidateOtherTable.addCell(cell); CandidateOtherTable.addCell(scrdate); CandidateOtherTable.addCell(cell); CandidateOtherTable.addCell(type); CandidateOtherTable.addCell(cell); PdfPCell race_start_time = new PdfPCell(new Phrase("Start Time :XX-XX-XX ", fsmall)); PdfPCell race_end_time = new PdfPCell(new Phrase("End Time :XX-XX-XX", fsmall)); PdfPCell race_total_time = new PdfPCell(new Phrase("Total Time : 350.00 ", fsmall)); race_start_time.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); race_end_time.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); race_total_time.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); PdfPTable CandidateRaceDetails = new PdfPTable(columnWidths); CandidateRaceDetails.addCell(race_start_time); CandidateRaceDetails.addCell(cell); CandidateRaceDetails.addCell(race_end_time); CandidateRaceDetails.addCell(cell); CandidateRaceDetails.addCell(race_total_time); CandidateRaceDetails.addCell(cell); PdfPTable tablewith3cells = new PdfPTable(3); //1 St Col for Roll No Name and DOB tablewith3cells.addCell(CandidateTable); //2 nd Col for Father Name sCREEning Date Gurkha tablewith3cells.addCell(CandidateOtherTable); //3rd Col for Barcode to be Printed tablewith3cells.addCell(CandidateRaceDetails); // Setting the Width here to 101 tablewith3cells.setWidthPercentage(99); doc.add(tablewith3cells); PdfPTable userArea = new PdfPTable(1); userArea.setWidthPercentage(99); userArea.addCell(" \n \n Congratulations \n \n "); doc.add(userArea); PdfPTable footerCSBC = new PdfPTable(2); footerCSBC.setWidthPercentage(99); PdfPCell height_box = new PdfPCell(new Phrase("Height \n\n\n", f)); height_box.setBorder(com.itextpdf.text.Rectangle.BOX); PdfPCell chest_box = new PdfPCell(new Phrase("Chest \n\n\n", f)); chest_box.setBorder(com.itextpdf.text.Rectangle.BOX); PdfPCell chest_exp_box = new PdfPCell(new Phrase("Chest Exp \n\n\n", f)); chest_exp_box.setBorder(com.itextpdf.text.Rectangle.BOX); PdfPCell pushup_box = new PdfPCell(new Phrase("Pushup \n\n\n", f)); pushup_box.setBorder(com.itextpdf.text.Rectangle.BOX); //CSignatureBox.setBorder(com.itextpdf.text.Rectangle.BOX); // ASignatureBox.setBorder(com.itextpdf.text.Rectangle.BOX); footerCSBC.addCell(height_box); footerCSBC.addCell(chest_box); footerCSBC.addCell(chest_exp_box); footerCSBC.addCell(pushup_box); doc.add(footerCSBC); float[] columnWidths_ForBarcode = { 6, 3 }; PdfPTable terminalinfo = new PdfPTable(columnWidths_ForBarcode); // terminalinfo.setWidthPercentage(99); String computername = InetAddress.getLocalHost().getHostName(); System.out.println(computername); PdfPCell pcname = new PdfPCell(new Phrase("\t Jacket \n\n ", f)); String UserMatchScore = "\t Barcode\n\n"; PdfPCell score = new PdfPCell(new Phrase(UserMatchScore, f)); PdfPCell barcode = new PdfPCell(carcode2); PdfPCell jacketnumber = new PdfPCell(carcode); pcname.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); score.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); barcode.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); jacketnumber.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); terminalinfo.addCell(score); terminalinfo.addCell(pcname); terminalinfo.addCell(barcode); terminalinfo.addCell(jacketnumber); doc.add(terminalinfo); PdfPCell eula_notice = new PdfPCell(new Phrase(" ", f)); eula_notice.setBorder(com.itextpdf.text.Rectangle.NO_BORDER); cell.setHorizontalAlignment(Element.ALIGN_CENTER); PdfPTable eula_notice_table = new PdfPTable(1); eula_notice_table.setWidthPercentage(25); eula_notice_table.addCell(eula_notice); doc.add(eula_notice_table); } catch (Exception e) { System.err.println(e.getMessage()); // ConsoleMsg(e.getMessage()); } finally { doc.close(); doc.close(); doc.close(); } //ConsoleMsg("PDF... GENERATED"); return null; // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }.execute(); return 0; }