【问题标题】:how to search in pdf files content and attach to the email如何在 pdf 文件内容中搜索并附加到电子邮件
【发布时间】:2015-11-13 17:38:47
【问题描述】:

在这里,我在一个文件夹中保存了七个 pdf 文件,这些文件保存在他们的 Invoice No 值中。例如,我的 pdf 如下所示

Bil-to 客户编号是 Delar 代码。我连接到 ms 访问数据库并能够获取电子邮件 ID 和 Delear 代码。此代码在每个 pdf 中有所不同。 Nw 我的任务是在所有 pdf 文件中搜索此 Delear 代码并附上相应的电子邮件 ID。 db内容如下

STE002 a@gmail.com
C04004 a@gmail.com
RS0002 b@gmail.com
RS0006 b@gmail.com
RS0009 c@gmail.com
RS0001 c@gmail.com
C01020 d@gmail.com

我的邮箱如下。

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:PDF1";
    Connection con = DriverManager.getConnection(url);
     java.sql.Statement st = con.createStatement();

    String sql = "SELECT * FROM new";    // Retrieve data from Person table in database
    ResultSet rs = st.executeQuery(sql);

    while(rs.next()){


    String code = rs.getString("Dealer Code");
    String email = rs.getString("Dealer Email ID");

    System.out.println(+ code + " " + email);  

    //email


        String to = email;

      String from = "abcd.gmail.com";

      final String username = "abcd.gmail.com";//change accordingly
      final String password = "*******";//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = "smtp.gmail.com";

      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "25");

      // Get the Session object.
      Session session = Session.getInstance(props,
         new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
               return new PasswordAuthentication(username, password);
            }
         });

      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject("Testing Subject");

         // Create the message part
         BodyPart messageBodyPart = new MimeBodyPart();

         // Now set the actual message
         messageBodyPart.setText("This is message body");

         // Create a multipar message
         Multipart multipart = new MimeMultipart();

         // Set text message part
         multipart.addBodyPart(messageBodyPart);

         // Part two is attachment
         messageBodyPart = new MimeBodyPart();

         String filename = "E:\\Sales.pdf";

         DataSource source = new FileDataSource(filename);
         messageBodyPart.setDataHandler(new DataHandler(source));
         messageBodyPart.setFileName(filename);
         multipart.addBodyPart(messageBodyPart);

         // Send the complete message parts
         message.setContent(multipart);

         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

【问题讨论】:

    标签: java pdfbox


    【解决方案1】:

    尝试使用Apache PDFBox 这里是text extraction tutorial

    要查找 pdf 文件,请使用 listFiles(FileFilter filter) 这是此方法的示例:

    private static String directoryPath = "/Users/aal/Documents";
    private static String extension = "pdf";
    
    public static void main(String[] args) {
    
        File file = null;
        File[] paths;
    
        try {
            file = new File(directoryPath);
    
            FileFilter fileFilter = new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(extension);
                }
            };
    
            // returns pathnames for files and directory
            paths = file.listFiles(fileFilter);
    
            for (File path : paths) {
                // prints file and directory paths
                System.out.println(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    

    【讨论】:

    • 我尝试了 PDFBox,后来拆分为 pdf 内容(即发票号)。但我再次需要在目标文件夹中搜索
    • ya 我正在尝试,我可以列出文件。但是我可以在该列表中搜索所有 PDF 文件内容以查找经销商代码(即 Bill-to Customer No.)...?
    • @KiranP 我在另一个问题中回答了这个问题: Pattern.compile("Bill\\-to Customer No\\. ([A-Z0-9]+)");并像您在问题stackoverflow.com/questions/32002830/… 中所做的那样在文本剥离的结果上使用它注意:我的意思是问题,而不是答案。您自己的问题显示了如何将 PDF 作为一个整体进行文本提取。
    • 我尝试过这种模式不起作用。错误是标题页??? " + page + " 跳过"
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2014-07-01
    • 2017-11-07
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    相关资源
    最近更新 更多