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

Free Support Forum - aspose.com

Table formatting from document to PDF

Docs.zip (93.2 KB)
Hi Aspose Team,

We are using the latest aspose words (21.9.0) library for java. One of our requirements is to read a .doc template file and populate the table grid dynamically by iterating through the various table nodes. The number of rows and columns in the table will dynamically increase/decrease depending on the data, by iteratively populating the template. The table layout is fixed width for each of the columns. After populating the dynamic data, the document will be exported as PDF (save with SaveFormat.PDF).

The dynamic tables formatting was proper when we were using the outdated aspose words library, but with the latest one it is broken. we tried document.updateTableLayout(), Table.autoFit() etc, but it did not work. We are unable to identify any other fix from the forum as well.

I am attaching a sample doc template and the corresponding pdf file, please have a look and suggest a solution.

Thanks,
RajkumarPrinterfriendly.pdf (83.4 KB)

@rajkumarp Could you please provide a simple code and data that will allow us to reproduce the problem on our side? Also, please save the output document as DOCX and if it has proper layout - attach it here for testing. We will check the issue and provide you more information.
In addition, please try calling Document.UpdatePageLayout before saving document to PDF and let me know if this fixes the problem on your side.

1 Like

Hi Alexey, Save as docx also results in the formatting issues similar to pdf. UpdatePageLayout also did not help.
I am attaching a standalone client package to this thread for you to simulate the issue at your end. Please follow the below steps to execute the client in your local

  1. Extract the contents of this zip file to your local
  2. Add aspose.words-21.9.0.jar to the lib folder of this package (not included due to space constraint)
  3. cd to the directory and set path variable as required
  4. javac -classpath .\lib*;. TestDocument.java
  5. java -classpath .\lib*;. TestDocument

Step 5 will generate the docx & pdf files with data dynamically populated to the table (although the format will be incorrect).

Please let me know if there are any issues with the standalone client or if you need any further info on the issue.

Thanks, RajkumarStandaloneClient.zip (1.8 MB)

A post was split to a new topic: Problem with table formatting from document to PDF

@rajkumarp Thank you for additional information. It looks like you have implemented some kind of your own LINQ Reporting Engine.
Could you please attach the expected output document and let me know in which version of Aspose.Words the generated output look correct?

Hi alexey, the report works fine in aspose.words-10.6.0.jar. Attaching the formatted report generated by using this version. If you need the standalone client in this version, please let me know. I can recompile and attach.Printerfriendly_correctformat.pdf (32.5 KB)

@rajkumarp Could you please also attach DOCX document generated by the old version on your side?

Alexey, Attaching the docx file as well.Printerfriendly.docx (19.3 KB)

@rajkumarp Thank you for additional information. Do you have control on your template? If so you can modify it to get correct result. Please see the attached document: in.docx (31.7 KB)
I have set auto fit wo window for tables in your template and the generated result looks much better: out.docx (23.7 KB) out.pdf (39.9 KB)
Also, I would suggest you to consider using LINQ reporting engine to fill the template with data. It provides built-in functionality for what you are doing in your custom code.

Alexey, Just to double confirm the table layout change you are suggesting. In the input file, go to Table Tools --> LayOut --> Auto Fit --> Set Auto fit to contents for all the tables. Please confirm if this is correct. And how do i change my template file format docx ? Open in word --> Save as docx ?

@rajkumarp Yes, you are correct. See the attached screenshot:

I have set this for each of three tables with data,

Alexey, I have applied the template change as suggested. The output looks fine. We are currently testing with different data combination to see if the layout holds firm. Will get back to you if this topic can be closed. In summary the lay out holds its preferred layout after the following changes.

  1. Change the template file from .doc to.docx
  2. Change the table layout setting to auto fit to contents
  3. Invoke document.updateTableLayout() before save as PDF.

We thank you for your prompt support in coming up with the solution options with a quick turn around time. We appreciate the support provided.

@rajkumarp Thank you for your feedback. I just would like to highlight that calling Document.updateTableLayout() method is not normally recommended. The method is deprecated and will be removed once we fully integrate new table layout engine. So you should be careful with using this method and do not use it if it does not give you any advantage.

Alexey, If i don’t use updateTableLayout() method, I am not getting the desired result. You can comment out this in my standalone client code and see that even with the docx template file the output is not formatted in converted pdf file. Please let me know if you have any other alternatives with the current version of words library for java.

@rajkumarp It is ok to use Document.updateTableLayout() if you are sure it gives you the expected output. So you can keep using it. Just note to use this method carefully.