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

Free Support Forum - aspose.com

PDF page size fit issue

I am using Aspose Cells 8.1.0


To generate the PDF from the EMF file, I am inserting the EMF image to the worksheet and getting the PDF output of the sheet. The PDF output generated contains lot of white space for which I want to alter the page size of the PDF so that the given image file fits into the complete page of the PDF by maintaining the aspect ration of the given image.

Kindly suggest me a way to achieve the same.

For your reference I have attached two files: One is Aspose generated and another one contains desired output which I want to achieve using Aspose API.


Following is the code snippet that I used for generating the PDF:
FileInputStream fis = new FileInputStream(new File(“C:\temp\Chart 291.emf”));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
byte[] bytes = bos.toByteArray();

ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

//Copy the Picture to Second Worksheet
Workbook targetWorkbook = new Workbook(FileFormatType.XLSX);

WorksheetCollection targetWsc = targetWorkbook.getWorksheets();
com.aspose.cells.Worksheet targetWs = (com.aspose.cells.Worksheet)targetWsc.get(0);
targetWs.getPictures().add(0,0, 10, 6, bis);

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setPrintingPageType(PrintingPageType.IGNORE_BLANK);

//Save the workbook
targetWorkbook.save(“c:\temp\AsposeGenerated_EmfToPdf.pdf”, pdfSaveOptions);

Thanks,
Neeraj

Hi,


Thanks for the sample files.

I think you may try to use PdfSaveOptions.setOnePagePerSheet() method for your requirement. Please see the updated code segment for your reference (see the lines in bold especially):
e.g
Sample code:


FileInputStream fis = new FileInputStream(new File(“C:\temp\Chart 291.emf”));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
byte[] bytes = bos.toByteArray();

ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

//Copy the Picture to Second Worksheet
Workbook targetWorkbook = new Workbook(FileFormatType.XLSX);

WorksheetCollection targetWsc = targetWorkbook.getWorksheets();
com.aspose.cells.Worksheet targetWs = (com.aspose.cells.Worksheet)targetWsc.get(0);
targetWs.getPictures().add(0,0, 10, 6, bis);

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);

//Save the workbook
targetWorkbook.save(“c:\temp\AsposeGenerated_EmfToPdf.pdf”, pdfSaveOptions);

Thank you.

Thanks for your quick response. It worked fine.


Thanks,
Neeraj

Hi,


Good to know that it works for you. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.