这里我在一个文件夹中有七个pdf文件,这些文件保存在发票中没有值。例如,我的pdf看起来像
Bil-to Customer No.是Delear Code。我连接到ms访问数据库,并能够获取电子邮件ID和Delear代码。本守则在每个pdf中都有所不同。 我的任务是在所有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....");
答案 0 :(得分:1)
尝试使用Apache PDFBox 这是text extraction tutorial
要查找pdf文件,请使用 listFiles(FileFilter过滤器) 以下是此方法的示例:
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();
}
}