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
5,675 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

Do I have download any jars related to ZeonPad or do i perfrom the above code directly in Eclipse IDE. Please guide me.

Yes, you have to download the jars and use them.
Hi Kishan,

Thanks for your prompt response. I am completely new to java and initial days for me little bit hard.

From where I can download the jar file and also provide the link and name of the jar file.

Regards,

Basha.
You can download the jar from this link: http://www.zeonpad.com/app/CodeBuild/zeonpadpdfconversion_1.0.zip

Just click on the link and the download will begin. I can understand you are learning Java and things get a little confusing at times. You are always welcome to post your queries on the community :)

Thanks Kishan.

In PDF Merger, Is it possible to two images in a single page?

Currently I am using the below code to merge the image to pdf, the result(i.e pdf document) is coming single image single page. Please help.

// This classwill take care convert images to PDF

public static void convertFromImagesToPdf(String FRDID) {

File root = new File(imagesLocation);

String outputFile = FRDID + ".pdf";

List<String> fileList = new ArrayList<String>();

File files[] = root.listFiles();

for (File file : files) {

if (file.isFile() && !file.getName().endsWith(".pdf")) {

fileList.add(file.getName());

}

}

Document document = new Document();

try {

PdfWriter.getInstance(document, new FileOutputStream(new File(root, outputFile)));

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

}

document.open();

try {

for (String f : fileList) {

document.newPage();

Image image = Image.getInstance(new File(root, f).getAbsolutePath());

image.setAbsolutePosition(0, 0);

image.setBorderWidth(0);

image.scaleAbsoluteHeight(PageSize.A4.getHeight());

image.scaleAbsoluteWidth(PageSize.A4.getWidth());

document.add(image);

}

} catch (Exception e) {

e.printStackTrace();

}

document.close();

}

If you are asking to add multiple images to the same pdf file, you can try this:

List<String> files = new ArrayList<String>();
		files.add("image1.png");
		files.add("image2.png");

		Document document = new Document();

		PdfWriter.getInstance(document, new FileOutputStream(new File(root, outputFile)));
		document.open();

		for (String f : files) {
			document.newPage();
			Image image = Image.getInstance(new File(root, f).getAbsolutePath());
			document.add(image);
		}
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
2,094 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,803 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
2,270 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 81,084 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
8,304 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
17,379 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,806 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,377 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
861 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