How to Convert msg outlook mail to pdf using java

0 votes
Hi Team,

I am new to java and got the below requirement.

Note: Please help this code under core java functions.

1. Folder contains set of outlook mails

2. we have to go to respective folder and open each outlook email and convert into pdf

3. follow same for other mails in that folder.

4. Finally merge all pdf into single pdf.

Please help me on this requirement.

Regards,

Basha
Aug 21, 2019 in Java by Basha
4,981 views

1 answer to this question.

0 votes

Hi @Basha!

Here's a code to help you with your problem statement. I have used ZeonPad to convert .msg to .pdf and used PDFBox to merge the pdfs files:

import java.io.File;

public class pdf {


public static void main(String[] args) {

// TODO Auto-generated method stub

String path = "path to folder";

File folder = new File(path);

File[] listOfFiles = folder.listFiles();


for (int i = 0; i < listOfFiles.length; i++) {

String name=listOfFiles[i].getName();

//System.out.println(name);

String substring = name.length() > 4 ? name.substring(name.length() - 4) : name;

//System.out.println(substring);

if (substring.equals(".msg")) {

String fullname=path+"\\\\"+name;

System.out.println(fullname);

System.out.println(substring);

String pdfname=fullname.substring(0, fullname.length() - 4);

pdfname=pdfname+".pdf";

System.out.println(pdfname);

OutlookToPdf outlookToPdf = new OutlookToPdf();

OutlookToPdf.convert(fullname, pdfname);

}


}

File file = new File("path of the new file") 

PDDocument document = PDDocument.load(file);

PDFMergerUtility PDFmerger = new PDFMergerUtility(); 

PDFmerger.setDestinationFileName(path+"/merged.pdf");

for (int i = 0; i < listOfFiles.length; i++) {

String name=listOfFiles[i].getName();

//System.out.println(name);

String substring = name.length() > 4 ? name.substring(name.length() - 4) : name;

//System.out.println(substring);

if (substring.equals(".pdf")) {

String fullname=path+"\\\\"+name;

//System.out.println(fullname);

PDFmerger.addSource(fullname);


}

PDFmerger.mergeDocuments();


}

}

}
answered Aug 21, 2019 by Kishan
thanks. Let me try if one pdf page having multiple images or not.

Hi Kishan,

the code which has given above, below 2 lines getting error. Can you please help.

 PDDocument document = PDDocument.load(file);  

  PDFMergerUtility PDFmerger = new PDFMergerUtility();

Error: PDDocument can't be resolved to a type

Regards,

Basha

To solve this error, you have to set up the PDFBox jar. Please follow these steps:

  1. Download the jar by clicking on this link
  2. Open your project in Eclipse IDE
  3. Then right-click on your project folder in eclipse. 
  4. Go to "Build Path" -> "Configure Build Path"
  5. In the "Java Build Path" section, under the "Libraries" tab, click on "Add External JARs" button.
  6. Choose the JAR file that you downloaded. Click on "Apply and Close"
  7. Close the window
  8. In your code, add the below import:
    import org.apache.pdfbox.*; 

This should solve your error.

I am trying to implement the same functionality. will this work in linux OS where i dont have installed MS office. kindly suggest

Hi, @Sara,

Could you please post your workaround, what actually you are trying? Which functionality do you want to implement?

Related Questions In Java

0 votes
1 answer

How to convert a string representation of a hex dump to a byte array using Java?

public static byte[] hexStringToByteArray(String s) { ...READ MORE

answered Sep 26, 2018 in Java by sharth
• 3,370 points
1,683 views
0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,520 points
3,497 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
1,919 views
0 votes
2 answers
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,554 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
7,964 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
16,990 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,389 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,143 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
674 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP