We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Keep bookmark from email attach pdf file

I am using aspose-pdf-kit-4.5.0.jar to convert email msg file to pdf file.
They look ok.
But if an email message file has attached pdf file that has bookmarks.
The final bookmark does not correct.

The attached pdf file has this bookmark:
main1
main2
main 2-1
main 2-2
main 2-2-1

So the final book should be like this:
Email
The attached pdf file name
main1
main 2
main 2-1
main 2-2
main 2-2-1

But the wrong bookmark is like this:
main1
main 2
main 2-1
main 2-2
main 2-2-1
Email
The attached pdf file name

Please let me know.
Chang Shin

Hi Chang,


Thanks for contacting support.

Aspose.Pdf.Kit for Java supports the feature to manipulate existing PDF files and as per my understanding, you might be using Aspose.Email for Java to convert msg file to PDF format and problem related to Bookmarks is occurring during this conversion. Please acknowledge so we may share further details accordingly.

We are sorry for this inconvenience.

I create temporary files from email message file by using Aspose Email and then use Apsoe.Pdf.Kit tool.
It is not related with Apsose Email tool.

Here is the codes:

List bookMarkNames = new ArrayList();
for ( String fileName: attachedFiles ) {
bookMarkNames.add(fileName);
}
// for bookmarks for each pdf files
String[] bookmarkNames = new String[bookMarkNames.size()+1];
int[] bookmarkPages = new int[bookMarkNames.size()+1];
bookmarkNames[0] = “Email”;
int index = 0;
for ( String fileName : bookMarkNames ) {
index++;
bookmarkNames[index] = fileName;
}


InputStream inputStream;
InputStream[] pdfs = new InputStream[pdfFileNames.size()];
index = 0;
int pageCount = 1;
for ( String fileName : pdfFileNames ) {
PdfFileInfo pdfInfo = new PdfFileInfo(fileName);
inputStream = new FileInputStream(fileName);
pdfs[index] = inputStream;
bookmarkPages[index] = pageCount;
pageCount += pdfInfo.getNumberofPages();
index++;
}

// Merge pdf files
PdfFileEditor editor = new PdfFileEditor();
log.info(“saved pdf file name=”+this.pdfFileName);
commHistoryUpload.setSaved_file_name( this.pdfFileName );

if ( commHistoryUpload.getId() != null && !commHistoryUpload.getId().equals("") ) {
this.pdfFileName = commHistoryUpload.getId() + “-” + this.pdfFileName;
}
FileOutputStream outputStream = new FileOutputStream(baseFolder + this.pdfFileName);
editor.concatenate(pdfs, outputStream);
// close the output FileOutputStream
outputStream.close();

// Bookmarks setting
// http:// www.aspose.com/docs/display/pdfkitjava/PdfBookmarkEditor
// http:// www.aspose.com/docs/display/pdfkitjava/Bookmark
// create PdfBookmarkEditor object and bind input file


PdfBookmarkEditor pdfBookmarkEditor = new PdfBookmarkEditor();
pdfBookmarkEditor.bindPdf(baseFolder + this.pdfFileName);
// create bookmarks of multiple pages
// pdfBookmarkEditor.createBookmarkOfPage(bookmarkNames, bookmarkPages);
for ( int i =0; i< bookmarkNames.length; i++ ) {
// create another bookmark
Bookmark bookmark = new Bookmark();
bookmark.setPageNumber(bookmarkPages[i]);
bookmark.setTitle(bookmarkNames[i]);
bookmark.setLevel(1);
bookmark.setPageDisplay(“Fit”);
// bookmarkList.add(bookmark);
log.info(“bookmarkNames=”+bookmarkNames[i]);
List sheetNames = (List) excelSheetNames.get(bookmarkNames[i]);
if ( sheetNames != null && sheetNames.size() > 0 ) {
log.info(“bookmarkNames,sheetNames=”+sheetNames.size());
ArrayList childList = new ArrayList();
for ( int j = 0; j < sheetNames.size(); j++ ) {
Bookmark bookmark2 = new Bookmark();
bookmark2.setPageNumber(bookmarkPages[i]+j);
bookmark2.setTitle(sheetNames.get(j));
bookmark2.setLevel(2);
childList.add(bookmark2);
}
bookmark.setChildItem(childList);
}
pdfBookmarkEditor.createBookmarks(bookmark);

}

// bookkmark.setAction(action)
// save final PDF file
pdfBookmarkEditor.save(baseFolder + this.pdfFileName);

// PDFinitial setting
// http:// www.aspose.com/docs/display/pdfkitjava/PdfContentEditor
// http:// www.aspose.com/docs/display/pdfkitjava/ViewerPreference
PdfContentEditor pdfContentEditor = new PdfContentEditor();
pdfContentEditor.bindPdf(baseFolder + this.pdfFileName);
// pdfContentEditor.createBookmarks();
// ViewerPreference viewerPreference = new ViewerPreference();
pdfContentEditor.changeViewerPreference(ViewerPreference.PageModeUseOutlines); // show bookmark
pdfContentEditor.changeViewerPreference(ViewerPreference.CenterWindow);
pdfContentEditor.changeViewerPreference(ViewerPreference.FitWindow);
// pdfContentEditor.changeViewerPreference(ViewerPreference.PageLayoutSinglePage);
pdfContentEditor.changeViewerPreference(ViewerPreference.DisplayDocTitle);
pdfContentEditor.save(baseFolder + this.pdfFileName);

// Pdf pdf = new Pdf();
// OpenType.Bookmarks;
// http:// www.aspose.com/docs/display/pdfjava/Adding+bookmarks+in+the+PDF+document
// http:// www.aspose.com/docs/display/pdfjava/Add+a+Bookmark+in+the+PDF+Document
```v
deleteTempFiles(attachedFiles,attachedPDFFiles); // Delete attached files and created pdf filed

Thanks,
Chang Shin

Hi Chang,


Thanks for sharing the code snippet and sorry for the delayed response.

I have tried executing the code and have made necessary changes but I am afraid I am getting an error message while adding ChildItem to bookmark object. Rest of the code is being compiled without any issue. Can you please take a look over at your end and share the code which I can execute and replicate the issue. We are sorry for this inconvenience.

PS, I have tested the scenario using Aspose.Pdf for Java 4.0.0.

The new jar file name is “aspose.pdf-new-4.0.0.jar”.
I am not sure about this part:
Can you put the right code for pdf bookmark in line ??? and //TODO?

//pdfBookmarkEditor.createBookmarkOfPage(bookmarkNames, bookmarkPages);
for ( int i =0; i< bookmarkNames.length; i++ ) {
//create another bookmark
Bookmark bookmark = new Bookmark();
bookmark.setPageNumber(bookmarkPages[i]);
bookmark.setTitle(bookmarkNames[i]);
bookmark.setLevel(1);
bookmark.setPageDisplay(“Fit”);
//bookmarkList.add(bookmark);
log.info(“bookmarkNames=”+bookmarkNames[i]);
if ( bookmarkNames[i].endsWith(".pdf") && ??? ) {
//TODO…
} else { // for Excel
List sheetNames = (List) excelSheetNames.get(bookmarkNames[i]);
if ( sheetNames != null && sheetNames.size() > 0 ) {
log.info(“bookmarkNames,sheetNames=”+sheetNames.size());
Bookmarks childList = new Bookmarks();
for ( int j = 0; j < sheetNames.size(); j++ ) {
com.aspose.pdf.facades.Bookmark bookmark2 = new com.aspose.pdf.facades.Bookmark();
bookmark2.setPageNumber(bookmarkPages[i]+j);
bookmark2.setTitle(sheetNames.get(j));
bookmark2.setLevel(2);
childList.add(bookmark2);
}
bookmark.setChildItem(childList);
}

}
pdfBookmarkEditor.createBookmarks(bookmark);

}

Thanks,
Chang Shin

Hi Chang,

Thanks for sharing the code snippet.

Now I am able to execute the code and PDF files are being concatenated as well as the Bookmarks are being added to resultant PDF file. As per your statement in original problem description,
Chang Shin:
I am using aspose-pdf-kit-4.5.0.jar to convert email msg file to pdf file.
They look ok.
But if an email message file has attached pdf file that has bookmarks.
The final bookmark does not correct.

The attached pdf file has this bookmark:
main1
main2
main 2-1
main 2-2
main 2-2-1

So the final book should be like this:
Email
The attached pdf file name
main1
main 2
main 2-1
main 2-2
main 2-2-1

But the wrong bookmark is like this:
main1
main 2
main 2-1
main 2-2
main 2-2-1
Email
The attached pdf file name
One of the input PDF file has bookmarks and once you have added more bookmarks, the "Email" bookmark and bookmark related to individual file names, should appear before the bookmarks which were present in input PDF file. Please acknowledge if my understanding is correct.

Furthermore, I have tested the scenario using Aspose.Pdf for Java 4.0.0 using following code snippet and as per my observations, the bookmark Email does not appear in resultant document. Once you have confirmed the above stated behavior, I will further look into this matter. We are sorry for this inconvenience.

[Java]

java.io.File folder = new java.io.File("C:/pdftest/samplefiles/");

java.io.File[] listOfFiles = folder.listFiles();

for (java.io.File file : listOfFiles) {

if (file.isFile()) {

System.out.println(file.getName());

}

}

// for bookmarks for each pdf files

String[] bookmarkNames = new String[listOfFiles.length];

int[] bookmarkPages = new int[listOfFiles.length];

bookmarkNames[0] = "Email";

int index = 0;

for ( java.io.File fileName : listOfFiles ) {

bookmarkNames[index] = listOfFiles[index].getName();

index++;

}

InputStream inputStream;

InputStream[] pdfs = new InputStream[listOfFiles.length];

index = 0;

int pageCount = 1;

for ( java.io.File fileName : listOfFiles)

{

com.aspose.pdf.facades.PdfFileInfo pdfInfo = new com.aspose.pdf.facades.PdfFileInfo(listOfFiles[index].toString());

inputStream = new FileInputStream(fileName);

pdfs[index] = inputStream;

bookmarkPages[index] = pageCount;

pageCount += pdfInfo.getNumberOfPages();

index++;

}

// Merge pdf files

com.aspose.pdf.kit.PdfFileEditor editor = new com.aspose.pdf.kit.PdfFileEditor();

//log.info("saved pdf file name="+this.pdfFileName);

//commHistoryUpload.setSaved_file_name( this.pdfFileName );

// if ( commHistoryUpload.getId() != null && !commHistoryUpload.getId().equals("") ) {

// this.pdfFileName = commHistoryUpload.getId() + "-" + this.pdfFileName;

// }

java.io.FileOutputStream outputStream = new java.io.FileOutputStream("C:\\samplefiles\\ConcatenatedFile.pdf");

editor.concatenate(pdfs, outputStream);

//close the output FileOutputStream

outputStream.close();

com.aspose.pdf.facades.PdfBookmarkEditor pdfBookmarkEditor = new com.aspose.pdf.facades.PdfBookmarkEditor();

pdfBookmarkEditor.bindPdf("C:\\samplefiles\\ConcatenatedFile.pdf");

//create bookmarks of multiple pages

// pdfBookmarkEditor.createBookmarkOfPage(bookmarkNames, bookmarkPages);

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

{

//create another bookmark

com.aspose.pdf.facades.Bookmark bookmark = new com.aspose.pdf.facades.Bookmark();

bookmark.setPageNumber(bookmarkPages[i]);

bookmark.setTitle(bookmarkNames[i]);

bookmark.setLevel(1);

bookmark.setPageDisplay("Fit");

//bookmarkList.add(bookmark);

// List sheetNames = (List) excelSheetNames.get(bookmarkNames[i]);

//if ( sheetNames != null && sheetNames.size() > 0 ) {

com.aspose.pdf.facades.Bookmarks childList = new com.aspose.pdf.facades.Bookmarks();

// ArrayList childList = new ArrayList();

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

{

com.aspose.pdf.facades.Bookmark bookmark2 = new com.aspose.pdf.facades.Bookmark();

bookmark2.setPageNumber(bookmarkPages[i]+j);

bookmark2.setTitle(listOfFiles[j].getName());

bookmark2.setLevel(2);

childList.add(bookmark2);

}

bookmark.setChildItem(childList);

// }

pdfBookmarkEditor.createBookmarks(bookmark);

}

//bookkmark.setAction(action)

//save final PDF file

pdfBookmarkEditor.save(“C:\samplefiles\ConcatenatedFile_UpdatedBookmark.pdf”);

No, It does not work.

I use aspose.pdf-new-4.0.0.jar.

Thanks,
Chang Shin

Chang Shin:
No, It does not work.
Hi Chang,

From your above statement, do you mean "the "Email" bookmark and bookmark related to individual file names, should appear before the bookmarks which were present in input PDF file". Please acknowledge so we may proceed with our investigations, accordingly.