Problem with format when adding multiple document types to a PDF

I have a requirements to create a pdf document that may include multiple files within it.
These file could be MS word, MS Excel, image and PDF.

To enable this I create a master pdf document and then create sub pdf documents for each file I want to add. I then add the pages from each sub document to the master document.
Finally I convert the master pdf document to PDF1A format.

My problem occurs when I have an excel file that is landscape format and include any other file. When I create the excel pdf I set the option : setAllColumnsInOnePagePerSheet(true);

However this setting gets lost when I add multiple files to the master pdf.

Please see the test class below as an example:

public class AsposeTest {

private Document addImageToDocument (Document pdfDocument, byte[] image) throws IOException {

ByteArrayInputStream imageStream = null;

try {

// Add a page to pages collection of document
Page page = pdfDocument.getPages().add();

// Set margins so image will fit, etc.
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);

page.getPageInfo().setWidth(595);
page.getPageInfo().setHeight(334);

// Create an image object
Image pdfImage = new Image();
// Add the image into paragraphs collection of the section
page.getParagraphs().add(pdfImage);

imageStream = new ByteArrayInputStream(image);

// Set the image file stream
pdfImage.setImageStream(imageStream);

} finally {
if (imageStream != null) {
imageStream.close();
}
}

return pdfDocument;
}
@Test
public void excelTest() throws Exception {
String inputFileName=“landscape_spreadsheet.xlsx”;
String outputFileName1=“landscape_spreadsheet.pdf”;
String outputFileName2=“image_and_spreadsheet.pdf”;

Path file = Paths.get(“d:/test/” + inputFileName);
byte[] data = Files.readAllBytes(file);
com.aspose.pdf.License license = new com.aspose.pdf.License();
try {
license.setLicense(getLicence());
} catch (Exception e) {
throw new CreateDocumentException(“Cannot load licence for converting document to pdf”,e);
}
Document pdfDocument = new Document();
pdfDocument = addExcelToDocument (pdfDocument, data);
pdfDocument = convertPdfToPDF1A (pdfDocument);
savePdfToFile(pdfDocument, “d:/test/” + outputFileName1);

String imageFileName=“497.jpg”;
Path file1 = Paths.get(“d:/test/” + imageFileName);
byte[] data1 = Files.readAllBytes(file1);
Document pdfDocument1 = new Document();
pdfDocument1 = addExcelToDocument (pdfDocument1, data);
pdfDocument1 = addImageToDocument (pdfDocument1, data1);
pdfDocument1 = convertPdfToPDF1A (pdfDocument1);
savePdfToFile(pdfDocument1, “d:/test/” + outputFileName2);
}

private Document convertPdfToPDF1A (Document pdfDocument) throws Exception {
boolean success = pdfDocument.convert(“pdfconvert.log”, PdfFormat.PDF_A_1A, ConvertErrorAction.Delete);
if (!success) {
throw new RuntimeException (“Could not convert the documents to a PDF/A-1A format PDF.”);
}
return pdfDocument;
}

private void savePdfToFile(Document pdfDocument, String outputFileName) throws IOException {
ByteArrayOutputStream pdfOutStream = new ByteArrayOutputStream();
pdfDocument.save(pdfOutStream);
pdfOutStream.flush();

byte[] pdfAsBytes = pdfOutStream.toByteArray();

FileOutputStream pdfAFile = new FileOutputStream(outputFileName);
pdfAFile.write(pdfAsBytes);
pdfAFile.flush();
pdfAFile.close();

}
private Document addExcelToDocument(Document pdfDocument, byte[] fileContents) throws Exception {
ByteArrayInputStream excelStream = null;
ByteArrayOutputStream excelOutStream = null;
ByteArrayInputStream convertedPdfStream = null;

try {

excelStream = new ByteArrayInputStream(fileContents);
Workbook workbook = new Workbook(excelStream);
excelOutStream = new ByteArrayOutputStream();
com.aspose.cells.PdfSaveOptions options = new com.aspose.cells.PdfSaveOptions();
//This will set things to the desired pdf format
options.setCompliance(PdfCompliance.PDF_A_1_A);
//ensures that each page has all columns, even if it has to stretch the width of the page
// the format of the page is kept intact
options.setAllColumnsInOnePagePerSheet(true);
//ensures formula in the excel spreadsheet display the result values
options.setCalculateFormula(true);
workbook.save(excelOutStream, options);
byte[] excelDocument = excelOutStream.toByteArray();
excelOutStream.flush();
convertedPdfStream = new ByteArrayInputStream(excelDocument);
Document excelPdfDocument = new Document(convertedPdfStream);
pdfDocument = addPagesToPdf(pdfDocument, excelPdfDocument.getPages());
boolean att = excelPdfDocument.getFitWindow();

} finally {
if (excelOutStream != null) {
excelOutStream.close();
}
if (excelStream != null) {
excelStream.close();
}
if (convertedPdfStream != null) {
convertedPdfStream.close();
}
}
return pdfDocument;
}
private Document addPagesToPdf(Document pdfDocument, PageCollection pages) {

int numPages = pdfDocument.getPages().size();
pdfDocument.getPages().add(pages);
for (Page newPage : pages) {
pdfDocument.getPages().get_Item(++numPages).setPageInfo(newPage.getPageInfo());
}
return pdfDocument;
}
private FileInputStream getLicence() throws CreateDocumentException{
try {
return new FileInputStream(new FileSystemResourceLoader().getResource(“classpath:Aspose.Total.Java.lic”).getFile());

} catch (IOException e) {
throw new CreateDocumentException(“Cannot load the Aspose license”,e);
}

}
}

This test class creates a file called d:\test\landscape_spreadsheet.pdf. This file has the excel spreadsheet with all columns in one page. This pdf has only the spreadsheet included.

The second file created is d:\test\image_and_spreadsheet.pdf. The file has an image and the spreadsheet loaded. The spreadsheet is truncated in the pdf.

Any help would be appreciated.
Thanks
Charlie


Hi Charlie,


Thanks for contacting support.

Can you please share the input Excel file along with other input documents, so that we can test the conversion in our environment. We are sorry for this inconvenience.

I have uploaded the files

Hi Charlie,


Thanks for sharing the document.

I have tested the scenario using one of my sample PDF files which is portrait format and when I have merged Excel converted document to portrait file, the orientation of excel file is properly maintained. Can you please share the other files, which can help us in replicating the issue in our environment.

For your reference, I have also attached the output generated over my end.

Hi,

Thanks for your help with this.

All files are now attached. Both input files (497.jpg and landcape_spreadsheet.xlsx ) and both generated pdf files (image_and_spreadsheet.pdf and landscape_spreadsheet.pdf).

Cheers
Charlie

Hi Charlie,


Thanks for sharing the documents.

I am again testing the scenario using all the documents and will keep you updated with my findings.

charles.mccree003@msd.govt.nz:
Hi,

Thanks for your help with this.

All files are now attached. Both input files (497.jpg and landcape_spreadsheet.xlsx ) and both generated pdf files (image_and_spreadsheet.pdf and landscape_spreadsheet.pdf).

Cheers
Charlie

Hi,

Thanks for using our API’s.

I have tested the scenario and have managed to reproduce same problem. During my testing, I have observed that issue occurs when we convert concatenated file to PDF/A format. For the sake of correction, I have logged it as PDFJAVA-36754 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

@charles.mccree003msd

Thanks for our patience.

Our product team has investigated the earlier logged issue and as per their feedback, it is advisable to save the edited document before converting, to the update all the necessary parameters and data inside the document.

Please, add the following code before converter:

pdfDocument1 = addImageToDocument (pdfDocument1);

// An intermediate preservation of the document.
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
pdfDocument1.save(baos);
pdfDocument1 = new Document(new ByteArrayInputStream(baos.toByteArray()));

pdfDocument1 = convertPdfToPDF1A (pdfDocument1); 

In case of any further assistance, please feel free to contact us.