Embedding word document into pdf Report
4 views (last 30 days)
Show older comments
How can I embed a complex .docx document (formatted text, tables ecc...) into my .pdf report created with report generator? I have tried the following approaches, but both haven't satisfied me completely:
APPROACH 1
% Starting word application
word = actxserver('Word.Application');
% Opening the word document
wdoc = word.Documents.Open('filename');
% Getting doc content
Content = wdoc.Content.Text;
% Creating paragraph to add
par = Paragraph(Content);
% Adding par to report
R.add(par);
This approach works fine if the word document contains only not formatted text, but has the problem that:
- Paragraph does not mantain word formatting
- Paragraph is empty if unknown text is found (e.g: tables)
APPROACH 2
I directly append to par an embedded object that refers to the specific file
par = Paragraph();
append(par,EmbeddedObject('filename'));
This approach creates an hyperlink to the document, that is not the behavior I would like to have. In fact for my purpose I would prefer to show directly the word document content inside my report.
Thanks in advance for the help,
Francesco
1 Comment
Daniele Marchetti
on 2 Mar 2021
Hi Francesco,
I've the same issue. I can't go on.
Any suggestion will be appreciated.
Thanks for your interest,
Daniele
Answers (1)
T.Nikhil kumar
on 18 Apr 2024
Hello Francesco,
I get that you are trying to embed a .docx document into your .pdf report created with report generator.
As per my knowledge, embedding .docx content with formatting into a PDF through MATLAB's report generator requires a workaround, as there's no straightforward method to retain complex formatting after merging.
One approach could be to convert the .docx document into a PDF and then merge the report PDF with the newly generated PDF.
Conversion of docx to PDF can be done programmatically using Word's ‘ExportAsFixedFormat’ method. Refer to below code snippet for this conversion logic:
word = actxserver('Word.Application');
doc = word.Documents.Open(fullfile(pwd, 'yourFileName.docx'));
outFile = fullfile(pwd, 'converted.pdf');
doc.ExportAsFixedFormat(outFile, 17); % 17 corresponds to PDF format
doc.Close();
word.Quit();
You can now merge this PDF to the report PDF using ‘append_pdfs’ function, part of the MATLAB File Exchange submission "Export_fig". First, you'll need to download and add "Export_fig" to your MATLAB path. You can find it here. Another point to note is that, for exporting to PDF, ghostscript needs to be installed on your system.
You can now use its ‘append_pdfs’ function as shown below:
append_pdfs('mergedOutput.pdf', 'InitialReport.pdf', 'converted.pdf');
Hope this helps you proceed with your work!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!