/**
  * Extracts document level attachments
  *
  * @param filename a file from which document level attachments will be extracted
  * @throws IOException
  */
 public void extractDocLevelAttachments(String filename) throws IOException {
   PdfReader reader = new PdfReader(filename);
   PdfDictionary root = reader.getCatalog();
   PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
   PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
   PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
   PdfDictionary filespec;
   PdfDictionary refs;
   FileOutputStream fos;
   PRStream stream;
   for (int i = 0; i < filespecs.size(); ) {
     filespecs.getAsString(i++);
     filespec = filespecs.getAsDict(i++);
     refs = filespec.getAsDict(PdfName.EF);
     for (PdfName key : refs.getKeys()) {
       fos = new FileOutputStream(String.format(PATH, filespec.getAsString(key).toString()));
       stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
       fos.write(PdfReader.getStreamBytes(stream));
       fos.flush();
       fos.close();
     }
   }
   reader.close();
 }
 private static void concatPDFs(List<String> pdfs, OutputStream outputStream, String tempDiv) {
   ByteArrayOutputStream byteStream = null;
   Document document = new Document();
   try {
     PdfCopy copy = new PdfCopy(document, outputStream);
     document.open();
     for (String pdf : pdfs) {
       String fileLocation = tempDiv + pdf;
       InputStream templateIs = new FileInputStream(fileLocation);
       PdfReader reader = new PdfReader(templateIs);
       byteStream = new ByteArrayOutputStream();
       PdfStamper stamper = new PdfStamper(reader, byteStream);
       stamper.setFreeTextFlattening(true);
       stamper.setFormFlattening(true);
       stamper.close();
       PdfReader pdfReader = new PdfReader(byteStream.toByteArray());
       for (int page = 0; page < pdfReader.getNumberOfPages(); ) {
         copy.addPage(copy.getImportedPage(pdfReader, ++page));
       }
       pdfReader.close();
       reader.close();
     }
     document.close();
     copy.close();
   } catch (Exception e) {
     logger.error(e, e);
   } finally {
     if (document.isOpen()) document.close();
     try {
       if (outputStream != null) outputStream.close();
     } catch (IOException ioe) {
       logger.error(ioe, ioe);
     }
   }
 }
 /**
  * Manipulates a PDF file src with the file dest as result
  *
  * @param src the original PDF
  * @param dest the resulting PDF
  * @throws IOException
  * @throws DocumentException
  * @throws SQLException
  */
 public void manipulatePdf(String src, String dest)
     throws IOException, DocumentException, SQLException {
   // Create a database connection
   DatabaseConnection connection = new HsqldbConnection("filmfestival");
   // Create a list with bookmarks
   ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String, Object>>();
   HashMap<String, Object> map = new HashMap<String, Object>();
   outlines.add(map);
   map.put("Title", "Calendar");
   ArrayList<HashMap<String, Object>> kids = new ArrayList<HashMap<String, Object>>();
   map.put("Kids", kids);
   int page = 1;
   List<Date> days = PojoFactory.getDays(connection);
   for (Date day : days) {
     HashMap<String, Object> kid = new HashMap<String, Object>();
     kids.add(kid);
     kid.put("Title", day.toString());
     kid.put("Action", "GoTo");
     kid.put("Page", String.format("%d Fit", page++));
   }
   // Create a reader
   PdfReader reader = new PdfReader(src);
   // Create a stamper
   PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
   stamper.setOutlines(outlines);
   // Close the stamper
   stamper.close();
   reader.close();
   // Close the database connection
   connection.close();
 }
Esempio n. 4
0
  public static void main(String[] args) {
    SearchClientServiceImpl client = new SearchClientServiceImpl();
    try {
      PdfReader reader = new PdfReader("c:\\tools\\files\\rezolutie.pdf");
      PdfReaderContentParser parser = new PdfReaderContentParser(reader);
      TextExtractionStrategy strategy;
      for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
        String it = strategy.getResultantText();
        if (it != null && !it.trim().isEmpty()) {
          System.out.println(it);
          DocumentVersions doc = new DocumentVersions();
          doc.setContent(it);
          doc.setTitle("Han7");
          doc.setDescription("Descriere");
          doc.setData(new Date());
          ObjectMapper mapper = new ObjectMapper();
          String val = mapper.writeValueAsString(doc);
          client
              .getClient()
              .prepareIndex("twitter", "tweet", String.valueOf(i++))
              .setSource(val)
              .get();
        }
      }
      reader.close();

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      client.getClient().close();
    }
  }
Esempio n. 5
0
  private static void merge(String output, Collection<Path> all) {

    try {
      Document document = new Document();

      PdfCopy copy = new PdfCopy(document, new FileOutputStream(output));
      document.open();

      PdfReader reader;
      int n;

      for (Path pathTemp : all) {

        if (pathTemp.toFile().getAbsolutePath().endsWith(".pdf")) {

          reader = new PdfReader(pathTemp.toFile().getAbsolutePath());

          n = reader.getNumberOfPages();
          for (int page = 0; page < n; ) {
            copy.addPage(copy.getImportedPage(reader, ++page));
          }
          copy.freeReader(reader);
          reader.close();
        }
      }
      document.close();

    } catch (Exception e) {
      System.out.println(e);
    }
  }
Esempio n. 6
0
 // FIXME: Manage Exceptions
 private OutputStream marcarReporteProgramaRechazado(final Programa programa, InputStream src)
     throws IOException, DocumentException {
   PdfReader reader = null;
   reader = new PdfReader(src);
   int n = reader.getNumberOfPages();
   ByteArrayOutputStream os = new ByteArrayOutputStream();
   PdfStamper stamper = null;
   stamper = new PdfStamper(reader, os);
   // text watermark
   Font f = new Font(Font.FontFamily.HELVETICA, 65);
   Phrase p = new Phrase("RECHAZADO", f);
   // transparency
   PdfGState gs1 = new PdfGState();
   gs1.setFillOpacity(0.3f);
   // properties
   PdfContentByte over;
   Rectangle pagesize;
   float x, y;
   // loop over every page
   for (int i = 1; i <= n; i++) {
     pagesize = reader.getPageSizeWithRotation(i);
     x = (pagesize.getLeft() + pagesize.getRight()) / 2;
     y = (pagesize.getTop() + pagesize.getBottom()) / 2;
     over = stamper.getOverContent(i);
     over.saveState();
     over.setGState(gs1);
     ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 45);
   }
   stamper.close();
   reader.close();
   return os;
 }
  /**
   * Creates a PDF document.
   *
   * @param filename the path to the new PDF document
   * @throws DocumentException
   * @throws IOException
   */
  public void createPdf(String filename) throws IOException, DocumentException {
    // step 1
    Rectangle rect = new Rectangle(-595, -842, 595, 842);
    Document document = new Document(rect);
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    // draw the coordinate system
    PdfContentByte canvas = writer.getDirectContent();
    canvas.moveTo(-595, 0);
    canvas.lineTo(595, 0);
    canvas.moveTo(0, -842);
    canvas.lineTo(0, 842);
    canvas.stroke();

    // read the PDF with the logo
    PdfReader reader = new PdfReader(RESOURCE);
    PdfTemplate template = writer.getImportedPage(reader, 1);
    // add it at different positions using different transformations
    canvas.addTemplate(template, 0, 0);
    canvas.addTemplate(template, 0.5f, 0, 0, 0.5f, -595, 0);
    canvas.addTemplate(template, 0.5f, 0, 0, 0.5f, -297.5f, 297.5f);
    canvas.addTemplate(template, 1, 0, 0.4f, 1, -750, -650);
    canvas.addTemplate(template, 0, -1, -1, 0, 650, 0);
    canvas.addTemplate(template, 0, -0.2f, -0.5f, 0, 350, 0);
    // step 5
    document.close();
    reader.close();
  }
Esempio n. 8
0
 public static int getPageCount(File pdf) throws IOException {
   PdfReader reader = new PdfReader(pdf.getAbsolutePath());
   try {
     return reader.getNumberOfPages();
   } finally {
     if (reader != null) {
       reader.close();
     }
   }
 }
Esempio n. 9
0
  public List<Contacts> readContacts(ContactsInitializer contactsInitializer) {
    Properties properties;
    String pdfinput;
    String contents;
    String[] content;
    String[] contact;

    int no_of_pages;
    int page_no = ApplicationConstants.TWO;

    PdfReader reader = null;
    try {
      properties = contactsInitializer.getProperties();
      pdfinput = properties.getProperty(ApplicationConstants.PDF_INPUT);
      reader = new PdfReader(pdfinput);

      contents = PdfTextExtractor.getTextFromPage(reader, ApplicationConstants.ONE);
      content = contents.split(ApplicationConstants.NEW_LINE, ApplicationConstants.TWO);
      contact = content[1].split(ApplicationConstants.NEW_LINE);
      genarateContacts(contact);

      no_of_pages = reader.getNumberOfPages();

      while (page_no <= no_of_pages) {
        contents = PdfTextExtractor.getTextFromPage(reader, page_no);
        contact = contents.split(ApplicationConstants.NEW_LINE);
        genarateContacts(contact);
        page_no++;
      }
      logger.info(list.size() + " contacts added to list from pdf file successfully !");
      System.out.println(list.size() + " contacts added to list from pdf file successfully !");

    } catch (Exception exception) {
      System.out.println("contacts not created !");
      logger.error("contacts not created !");
      logger.error(exception);
      exception.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
          logger.debug(LoggerConstants.RESOURCES_RELEASED);
        } catch (Exception exception) {
          logger.error(exception);
          exception.printStackTrace();
        }
      }
    }
    return list;
  }