pearowaty
pearowaty

Reputation: 1

How to convert docx to PDF without split tables

I have dynamics docx with few tables and I'm trying to convert to a PDF. When I converted to PDF then it covers two pages. I use Apache POI XWPF converter in 2.0.2 version. In docx file everything is okey but when I convert to PDF then tables are spited

Someone have any idea or better library to convert docx to pdf?

PdfOptions options = PdfOptions.getDefault();

options.fontProvider((familyName, encoding, size, style, color) -> {
    try {
        BaseFont baseFont = BaseFont.createFont("fonts/times.ttf", encoding, BaseFont.EMBEDDED);
        return new Font(baseFont, size, style, color);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
});

PdfConverter.getInstance().convert(document, out, options);

Upvotes: 0

Views: 616

Answers (1)

Renis1235
Renis1235

Reputation: 4690

There is no library to convert a doc[x] file into a completely correctly formatted PDF. The only program that can do that is Word itself. I have achieved this by using the Word API in a PowerShell script:

$document_path = $args[0]
$document_parent_folder = $args[1]
$file_name = $args[2]

$word_app = New-Object -ComObject Word.Application

$document = $word_app.Documents.Open($document_path)

$pdf_filename = "$($document_parent_folder)\$($file_name)"

$document.SaveAs([ref] $pdf_filename, [ref] 17)

$document.Close()

$word_app.Quit()

Yes it is not the best solution and it is heavily dependent on having Microsoft Office installed in the machine and a lot of other problems that accompany this solution... But it is the only solution that formatted my documents exactly how I wanted them.

The script takes three arguments

  1. The path of the document that will be converted
  2. The folder where it is located
  3. The name of the pdf file

Upvotes: 1

Related Questions