Free Support Forum - aspose.com

Footer position when saving PDF from Cells

Hi,

when converting an Aspose Cells workbook to PDF, the footer will follow directly below the header on top of the page instead of being positioned at the bottom. The saved XLS document has all headers and footer in the right place. What can I do to make the pdf look alike?

Simple code example:

Workbook wb = new Workbook();
Worksheets worksheets = wb.getWorksheets();
Worksheet ws = worksheets.getActiveSheet();
PageSetup ps = ws.getPageSetup();
ps.setPaperSize(PaperSizeType.PAGE_A4);

ps.setLeftHeader(“top left”);
ps.setRightHeader(“top right”);
ps.setCenterHeader(“top center”);
ps.setLeftFooter(“bottom left”);
ps.setCenterFooter(“bottom center”);
ps.setRightFooter(“bottom right”);

Cells cells = ws.getCells();
cells.getCell(“A2”).setValue(123456);
cells.getCell(“B2”).setValue(654321);

wb.save(“C:\test\test.xls”);
wb.save(“C:\test\test.xml”, FileFormatType.ASPOSE_PDF);
Pdf pdf = Pdf.bindXML(new FileInputStream(“C:\test\test.xml”));
pdf.save(new FileOutputStream(“C:\test\test.pdf”));


Thanks a lot for your help!
Lennart

Hello Lennart,

Can you please share the source excel worksheet so that we can test the scenario at our end. We apologize for your inconvenience.

I have attached the three files that were created by my initial code example. Thanks a lot for your help!

Hello Lennart,

Thanks for sharing the source files.

I have tested the scenario using Aspose.Cells for Java 2.1.2 (which can be accessed from here) and Aspose.Pdf for Java 2.5.0 (can be accessed from here) and I am unable to notice any problem. I have used the following code snippet to perform the conversion. Can you please try using these versions and in case you still face any problem or you have any further query, please feel free to contact.

For your reference, I have also attached the resultant PDF that I have generated. Please take a look.

[Java]

//Instantiate the Workbook object
Workbook workbook = new Workbook();
workbook.open("D:/pdftest/test.xls");
//Save the document in Aspose.Pdf.Xml format
workbook.save("D:/pdftest/CellMarginTemplate/Template_new.xml", FileFormatType.ASPOSE_PDF);

//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
pdf1.bindXML("D:/pdftest/CellMarginTemplate/Template_new.xml", null);
// save the PDF file
pdf1.save("D:/pdftest/HeaderFooterPosition_test.pdf");

We apologize for your inconvenience.

Hello Nayyer,

I tried your above code snippet with the latest jars of Aspose Cells and Aspose Pdf you referred to. Unfortunately, execution halts with a null pointer at the pdf1.save(…) line with following stacktrace:

Exception in thread “main” java.lang.NullPointerException
at aspose.pdf.c.p.do(Unknown Source)
at aspose.pdf.c.aa.if(Unknown Source)
at aspose.pdf.e.m.a(Unknown Source)
at aspose.pdf.e.bm.a(Unknown Source)
at aspose.pdf.e.bo.a(Unknown Source)
at aspose.pdf.e.a0.a(Unknown Source)
at aspose.pdf.e.d.a(Unknown Source)
at aspose.pdf.e.bo.a(Unknown Source)
at aspose.pdf.e.a0.a(Unknown Source)
at aspose.pdf.e.d.a(Unknown Source)
at aspose.pdf.e.h.a(Unknown Source)
at aspose.pdf.e.ax.a(Unknown Source)
at aspose.pdf.e.bp.a(Unknown Source)
at aspose.pdf.e.a1.a(Unknown Source)
at aspose.pdf.e.bf.a(Unknown Source)
at aspose.pdf.e.ap.a(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)
at TestPdf.main(TestPdf.java:28)

I used the xls-file from my previous posting. The xml-file seems to get generated without problems (find it attached), but the final pdf remains a zero-byte-file due to the exception. I can’t seem to figure what I am doing wrong. Do you have some suggestion for me?

Thanks for your help
Lennart

Hello Lennart,

I have tried creating the PDF document using the XML document that you have shared and I am unable to notice any problem. The resultant PDF is in attachment, please take a look.

Can you please share which version of JDK you are using ? Currently I have tested it with JDK 1.6.

We apologize for your inconvenience.

Hello Nayyer,

thank you for your patience. It seems, indeed, to be a problem with my local JDK (1.6.0_17). I have tested your code with a JDK 1.6.0_12 on a different machine and it worked fine. So it seems to be rather a problem of my messed up local JDK than one concerning the Aspose libs. I’ll try to figure out what’s wrong or simply refresh my JDK… that should hopefully solve the problem.

Thank you for your help
Lennart

I am running out of ideas. It works perfectly on one computer and fails on another - despite the exact same copy of the JDK (1.6.0_17) and same settings used in Eclipse (see attachment). Environment variables are set up equally as well. The simple code use was:

Pdf pdf1 = new Pdf();
pdf1.bindXML(“C:/test/test.xml”, null);
pdf1.save(“C:/test/test.pdf”);

I have read in another thread that others have encountered the same problem in the past, too, but either did not give a solution or found it to be conflicting JDK5 and 6 installations, which is not the case here.

Do you have any more ideas what I should do and yet haven’t thought of?

Hello Lennart,

Thanks for sharing the detailed information.

I have tested the scenario over two different machines where one machine is having jdk1.6.0_11 and the other is having jdk1.6.0_12. I am afraid I have not tested it with jdk1.6.0_17. I will test the scenario and will update you with the status accordingly.

We apologize for your inconvenience.

Hello Nayyer,

I have tested my three-line test class on 5 different systems now… Three WinXP Pro and two Linux machines, with JDKs 1.6.0_0, 1.6.0_10, 1.6.0_12, 1.6.0_14 and 1.6.0_17. The only place where the PDF was generated successfully was a (single core) WinXP Pro / JDK 1.6.0_12, all other machines (multicore) ended up with a nullpointer.

Here is what I did on each machine:
1. copied the three attached files into an empty directory, then opened a shell for that directory
2. javac -cp aspose.pdf.jar TestPdf.java
3. java -cp .;aspose.pdf.jar TestPdf (on linux it was cp .:aspose.pdf.jar, of course)
4. seen following exception in 4 of 5 cases:

Exception in thread “main” java.lang.NullPointerException
at aspose.pdf.c.p.do(Unknown Source)
at aspose.pdf.c.aa.if(Unknown Source)
at aspose.pdf.e.m.a(Unknown Source)
at aspose.pdf.e.bm.a(Unknown Source)
at aspose.pdf.e.bo.a(Unknown Source)
at aspose.pdf.e.a0.a(Unknown Source)
at aspose.pdf.e.d.a(Unknown Source)
at aspose.pdf.e.bo.a(Unknown Source)
at aspose.pdf.e.a0.a(Unknown Source)
at aspose.pdf.e.d.a(Unknown Source)
at aspose.pdf.e.h.a(Unknown Source)
at aspose.pdf.e.ax.a(Unknown Source)
at aspose.pdf.e.bp.a(Unknown Source)
at aspose.pdf.e.a1.a(Unknown Source)
at aspose.pdf.e.bf.a(Unknown Source)
at aspose.pdf.e.ap.a(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)
at TestPdf.main(TestPdf.java:8)


Hopefully, this helps you reproduce the error or discover a mistake on my side. I am out of ideas.

Thank your for your efforts!
Lennart

Hello Lennart,

Thanks for sharing the detailed information. We would look into the details of this problem and will keep you updated on the status of correction.

We apologize for your inconvenience.

Hello Nayyer,

did you find out anything new in this issue? For our current project, we really need to get the 2.5.0 version of Aspose.Pdf for Java working, since v2.4 was working fine but didn’t handle footer positions correctly. Your support is much appreciated.

Best regards
Lennart

Hello Lennart,

Sorry for getting back to you so late. In fact we have been busy while testing the scenario using different versions of JDK over different machines with different specification. Following are the details of our test scenarios.

Single core processor

I tried the conversion using JDK 1.6.0_10, 1.6.0_11, 1.6.0_12, 1.6.0_14, 1.6.0_16, 1.6.0_17 and 1.6.0_20 and I am able to convert it into PDF format without any problem.

Multi Core processor

I also tried the conversion over JDK 1.6.0_18 and the excel worksheet is being converted into PDF format without any problem. The resultant PDF that I have generated is in attachment, please take a look.

We are still in the process of testing it over Linux platform and soon you will be updated with the appropriate status.

We are sorry for your inconvenience.

Hello Nayyer,

thanks a lot for your efforts! I finally just found the mistake by monitoring all file I/O during execution of my code using sysinternal’s filemon.exe. It looks like I simply forgot to explicitly set the ttf-fonts directory, causing the pdf lib to default to “c:/windows/fonts” which doesn’t exist on any of the machines I had tested but on my notebook. Prior versions of Aspose Pdf didn’t mind not setting the fonts path. To help people like me, in future versions of Aspose pdf you might choose to throw a more comprehensive exception in case of improper fonts directories ;).

For now, everything works fine and we are happy. Again, thanks a lot for your help!
Best regards
Lennart

Hello Lennart,

I am glad to hear that your problem has been resolved. Whereas concerning to the issue "Exception is being displayed if TrueType font directory is not specified and a proper error message should be displayed" I have logged this problem in our issues tracking system as PDFJAVA-16423. We will investigate this issue in details and will keep you updated on the status of a correction. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

In order to specify the TrueType font directory, you may try using pdf.setTruetypeFontDirectoryPath("c:/windows/fonts"); code line. In case you still face any issue or you have any further query, please feel free to contact.

We are sorry for your inconvenience.