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

Free Support Forum - aspose.com

Cell Text is Rendered on Next Line after Changing Paper Size to A4 | HTML to PDF Conversion using Java

Hello!

I am converting html files to pdf using Aspose.Words 21.9. But per default, the resulting document is in letter format. So I am forced to change the papersize to A4, which leads to inappropriate line breaks in the table header.

Code example:

Document document = new Document(stream, buildLoadOptions(bis));
for (Section section : document.getSections()) {
	PageSetup pageSetup = section.getPageSetup();
	if (PaperSize.A4 != pageSetup.getPaperSize()) {
		pageSetup.setPaperSize(PaperSize.A4);
	}
}
document.save("Auswertung.pdf");

Attached are input-html and output-pdf, one without changing the paper size, where the resut looks fine but is in letter-format, and another one, where I changed the papersize as shown above, which leads to line breaks in the table header: html2pdf_Letter_A4.zip (89.8 KB)

Is it possible to avoid this line breaks during the conversion to an A4 pdf?

Kind regards!

@dvtdaten

You can use Table.autoFit method as shown below to get the desired output.

Document document = new Document(MyDir + "Auswertung.html");
for (Section section : document.getSections()) {
	PageSetup pageSetup = section.getPageSetup();
	if (PaperSize.A4 != pageSetup.getPaperSize()) {
		pageSetup.setPaperSize(PaperSize.A4);
	}
}

for(Table table :(Iterable<Table>)document.getChildNodes(NodeType.TABLE, true))
{
     table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
}

document.updatePageLayout();
document.save(MyDir + "21.9.pdf");

Hello!
Thanks, this solution works fine for this file.
But if I open the html file with MS Word, it is in A4 format and the layout is fine. Is there a way to override the default format (letter) and mimic this behavioure with Aspose.Words?

@dvtdaten

Please note that Aspose.Words mimics the behavior of MS Word. If you open the HTML in MS Word, its paper size is Letter. Please check the attached image for detail.
paper size.png (36.9 KB)

Yes, thats exactly what I wanted to address. The MS Word default format is dependent on the region. In yours it is letter, in mine it is A4. And it is possible to change it: https://answers.microsoft.com/en-us/msoffice/forum/all/how-do-i-change-my-default-paper-size-to-a4/b9270ea1-619a-4e7a-b512-0798e7c42f4b
But is it alo possible, to change the default format in Aspose.Words?

@dvtdaten

Could you please share your region name and detail? We will investigate this issue and provide you more information on it.

Language: German, Country: Austria
But I guess in most parts of the world A4 is the default format: https://en.wikipedia.org/wiki/Paper_size

@dvtdaten

We suggest you please read the following article about setting language preferences.

Could you please try the following code and let us know how it goes on your side? If you still face problem, please share the MS Word version that you are using along with output document generated by following code.

LoadOptions loadOptions = new LoadOptions();
loadOptions.getLanguagePreferences().addEditingLanguage(EditingLanguage.GERMAN_GERMANY);
Document document = new Document(MyDir + "Auswertung.html", loadOptions);

document.save(MyDir + "21.9.docx");

No, setting the language preferences does not help, I still have to manually setting the paper size and update the page layout (look at your comment on Sept 17). But anyway, this is ok for me, because my problem is solved this way.

@dvtdaten

Yes, you can change the paper size using PageSetup.PaperSize property. However, we have logged a ticket as WORDSNET-22749 in our issue tracking system to investigate the paper size issue. We will inform you once there is an update available on it.