List of usage examples for com.itextpdf.text.pdf PdfReader PdfReader
public PdfReader(final PdfReader reader)
From source file:pdf.letter.java
public boolean AlterLetter(String rujukan, String name, String position, String department, String gStatus, String sDate, String eDate, String taskName, String postHolderName, String postHolderEmail, String postName) {/* w w w.j a va 2 s. co m*/ DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date today = Calendar.getInstance().getTime(); String currDate = df.format(today); try { PdfReader pdfReader; pdfReader = new PdfReader("C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf"); //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Appointment letter.pdf //Create PdfStamper instance. PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream( "C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified appointment letter.pdf")); //C:\\Users\\on\\Desktop\\AD\\TFMsystem\\web\\Modified Appointment letter.pdf //Create BaseFont instance. BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED); //Get the number of pages in pdf. int pages = pdfReader.getNumberOfPages(); //Iterate the pdf through pages. for (int i = 1; i <= pages; i++) { //Contain the pdf data. PdfContentByte pageContentByte = pdfStamper.getOverContent(i); pageContentByte.beginText(); //Set text font and size. pageContentByte.setFontAndSize(baseFont, 11); //Write text pageContentByte.setTextMatrix(120, 706); pageContentByte.showText(rujukan); pageContentByte.setTextMatrix(500, 706); pageContentByte.showText(currDate); //address pageContentByte.setTextMatrix(46, 641); pageContentByte.showText(name); pageContentByte.setTextMatrix(46, 629); pageContentByte.showText(position); pageContentByte.setTextMatrix(46, 617); pageContentByte.showText(department); String gstatus; pageContentByte.setTextMatrix(157, 493); String changeCase = gStatus + ", " + taskName; pageContentByte.showText(changeCase.toUpperCase()); pageContentByte.setTextMatrix(250, 444); pageContentByte.showText(gStatus + " " + taskName + " ."); pageContentByte.setTextMatrix(180, 432); pageContentByte.showText(sDate); pageContentByte.setTextMatrix(290, 432); pageContentByte.showText(eDate + " ."); pageContentByte.setTextMatrix(46, 248); pageContentByte.showText(postHolderName); pageContentByte.setTextMatrix(46, 236); pageContentByte.showText(postName); pageContentByte.setTextMatrix(46, 224); pageContentByte.showText("Fakulti Komputeran"); pageContentByte.setTextMatrix(46, 212); pageContentByte.showText(postHolderEmail); pageContentByte.endText(); } //Close the pdfStamper. pdfStamper.close(); System.out.println("PDF modified successfully."); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:pdf.Sign.java
License:Open Source License
private String doSignPdf(String pdfFile, String pdfFileSigned) { try {//from w ww . j a v a2 s .c o m PdfReader reader = new PdfReader(pdfFile); FileOutputStream fout = new FileOutputStream(pdfFileSigned); PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0'); PdfSignatureAppearance sap = stp.getSignatureAppearance(); sap.setCrypto(null, _chain, null, PdfSignatureAppearance.SELF_SIGNED); sap.setReason("Declaratie unica"); sap.setVisibleSignature(new Rectangle(500, 775, 600, 675), 1, null); sap.setExternalDigest( new byte[((RSAPublicKey) _certAlias._cert.getPublicKey()).getModulus().bitLength() / 8], null, "RSA"); sap.preClose(); byte[] content = streamToByteArray(sap.getRangeStream()); Signature signature = Signature.getInstance("SHA1withRSA", _etpkcs11); signature.initSign((PrivateKey) _privateKey); signature.update(content); byte[] signatureBytes = signature.sign(); // Self-Sign mode PdfPKCS7 sig = sap.getSigStandard().getSigner(); sig.setExternalDigest(signatureBytes, null, "RSA"); PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.CONTENTS, new PdfString(sig.getEncodedPKCS1()).setHexWriting(true)); sap.close(dic); } catch (FileNotFoundException ex) { return ex.toString(); } catch (ProviderException ex) { if (ex.getMessage().equals("Initialization failed")) { return ex.toString() + " (Probabil aveti un alt tip de SmartCard conectat. Deconectati alte tipuri de SmartCarduri (daca exista) si folositi optiunea \"*autoDetect\")"; } else if (ex.getMessage().equals("Error parsing configuration")) { return ex.toString() + " (Calea catre driverul SmartCardului (care se afla inscrisa in fisierul .cfg corespunzator acestuia) contine unul din urmatoarele caractere: \"~()\". Solutie: Copiati continutul intregului folder in alta locatie si modificati corespunzator calea din fisierul .cfg. (vezi si http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6581254))"; } return ex.toString(); } catch (NoSuchAlgorithmException ex) { return ex.toString(); } catch (IOException ex) { return ex.toString(); } catch (DocumentException ex) { return ex.toString(); } catch (InvalidKeyException ex) { return ex.toString(); } catch (SignatureException ex) { return ex.toString(); } catch (Throwable ex) { return ex.toString(); } finally { //wwww: eliminare key pt a putea introduce un nou pin // String str = pin.getPassword().toString(); // pin.destroy(); // Security.removeProvider(_etpkcs11.getName());//"SunPKCS11-SmartCard"); //wwww } return ""; }
From source file:pdf.SplitPDF.java
public static void splitPDF(InputStream inputStream, OutputStream outputStream, int fromPage, int toPage) { Document document = new Document(); try {/*w ww .ja v a 2s . c o m*/ PdfReader inputPDF = new PdfReader(inputStream); int totalPages = inputPDF.getNumberOfPages(); //make fromPage equals to toPage if it is greater if (fromPage > toPage) { fromPage = toPage; } if (toPage > totalPages) { toPage = totalPages; } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data PdfImportedPage page; while (fromPage <= toPage) { document.newPage(); page = writer.getImportedPage(inputPDF, fromPage); cb.addTemplate(page, 0, 0); fromPage++; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) { document.close(); } try { if (outputStream != null) { outputStream.close(); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
From source file:pdf.watermark.java
License:Open Source License
public watermark() { try {/*from www .j a v a2 s . co m*/ PdfReader Read_PDF_To_Watermark = new PdfReader( "D:\\Dropbox\\Studium\\Bachelor Thesis\\Resourcen\\pdfexport\\xmltopdf\\test.pdf"); int number_of_pages = Read_PDF_To_Watermark.getNumberOfPages(); PdfStamper stamp = new PdfStamper(Read_PDF_To_Watermark, new FileOutputStream( "D:\\Dropbox\\Studium\\Bachelor Thesis\\Resourcen\\pdfexport\\xmltopdf\\New_PDF_With_Watermark_Image.pdf")); int i = 0; Image watermark_image = Image .getInstance("D:\\Dropbox\\Studium\\Bachelor Thesis\\Resourcen\\pdfexport\\xmltopdf\\desy.png"); watermark_image.setAbsolutePosition(50, 150); watermark_image.scaleToFit(500, 500); PdfContentByte add_watermark; while (i < number_of_pages) { i++; add_watermark = stamp.getUnderContent(i); add_watermark.addImage(watermark_image); } stamp.close(); } catch (IOException | DocumentException i1) { i1.printStackTrace(); } }
From source file:pdfextract.ControlPDF.java
public List<String> parsePdfToArrayList(String pdfPath) throws IOException { PdfReader reader = new PdfReader(pdfPath); PdfReaderContentParser parser = new PdfReaderContentParser(reader); List<String> arrayOftext = new ArrayList<String>(); TextExtractionStrategy strategy;/* w w w . j a va 2s . co m*/ for (int i = 1; i <= reader.getNumberOfPages(); i++) { strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); arrayOftext.add(strategy.getResultantText()); } reader.close(); return arrayOftext; }
From source file:pdfextract.ControlPDF.java
public void extractImagesFromPdf(String filename, String save) throws IOException, DocumentException { PdfReader reader = new PdfReader(filename); PdfReaderContentParser parser = new PdfReaderContentParser(reader); ImageRenderListener listener = new ImageRenderListener(save); for (int i = 1; i <= reader.getNumberOfPages(); i++) { parser.processContent(i, listener); }/* www. j a v a2 s . c o m*/ reader.close(); }
From source file:pdfextract.ControlPDF.java
public String pdfToDIrAndimgToString(String pdfPath, File destnationPath) throws IOException, DocumentException, Exception { //boolean success; String kk = ""; String desPath = null;/*from w w w.ja v a 2 s . co m*/ PdfReader reader = new PdfReader(pdfPath); PdfReaderContentParser parser = new PdfReaderContentParser(reader); try { desPath = destnationPath.getName(); new File(desPath.replace(".txt", "")).mkdir(); PrintWriter out = new PrintWriter( new FileOutputStream(desPath.replace(".txt", "") + "/" + destnationPath.getName())); TextExtractionStrategy strategy; for (int i = 1; i <= reader.getNumberOfPages(); i++) { strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); out.println(i + " " + strategy.getResultantText()); } reader.close(); out.flush(); out.close(); extractImagesFromPdf(pdfPath, desPath.replace(".txt", "") + "/" + desPath.replace(".txt", "")); File f = new File(desPath.replace(".txt", "") + "/" + desPath.replace(".txt", "") + "-16.null"); kk = imageToBase64String(f); /*File deletedFolder = new File(destnationPath.getParent()); System.out.println("here"+destnationPath.getParent()); deletedFolder.delete(); System.out.println("is delete+ " + deletedFolder.delete());*/ // FileUtils.deleteDirectory(new File(deletedFolder.getParent())); //success = true; } catch (Exception ex) { //success = false; } return kk; }
From source file:pdfextract.ExtractInfo.java
public List<String> parsePdf(String pdfPath) throws IOException { PdfReader reader = new PdfReader(pdfPath); PdfReaderContentParser parser = new PdfReaderContentParser(reader); //String [] arrayOftext= new String[300]; List<String> arrayOftext = new ArrayList<String>(); // PrintWriter out = new PrintWriter(new FileOutputStream(destnationPath)); TextExtractionStrategy strategy;/*from ww w .ja v a 2 s . c o m*/ for (int i = 1; i <= reader.getNumberOfPages(); i++) { strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); // out.println(i + " " + strategy.getResultantText()); arrayOftext.add(strategy.getResultantText()); } reader.close(); return arrayOftext; }
From source file:pdfextract.ExtractInfo.java
public void extractImagesInfo() { try {//from w w w.jav a 2s . c om PdfReader chartReader = new PdfReader("vv.pdf"); for (int i = 0; i < chartReader.getXrefSize(); i++) { PdfObject pdfobj = chartReader.getPdfObject(i); if (pdfobj != null && pdfobj.isStream()) { PdfStream stream = (PdfStream) pdfobj; PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE); //System.out.println("Stream subType: " + pdfsubtype); if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) { byte[] image = PdfReader.getStreamBytesRaw((PRStream) stream); Image imageObject = Image.getInstance(image); System.out.println("Resolution" + imageObject.getDpiX()); System.out.println("Height" + imageObject.getHeight()); System.out.println("Width" + imageObject.getWidth()); } } } } catch (Exception e) { e.printStackTrace(); } }
From source file:pdfextract.ExtractInfo.java
public void imageNew() throws IOException { PdfReader reader;/*from w ww .java 2 s . c om*/ File file = new File("vv.pdf"); reader = new PdfReader(file.getAbsolutePath()); for (int i = 0; i < reader.getXrefSize(); i++) { PdfObject pdfobj = reader.getPdfObject(i); if (pdfobj == null || !pdfobj.isStream()) { continue; } PdfStream stream = (PdfStream) pdfobj; PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE); if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) { byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream); FileOutputStream out = new FileOutputStream( new File(file.getParentFile(), String.format("%1$05d", i) + ".jpg")); out.write(img); out.flush(); out.close(); } } }