필터 지우기
필터 지우기

Embedding word document into pdf Report

조회 수: 3 (최근 30일)
Francesco Dini
Francesco Dini 2021년 2월 23일
답변: T.Nikhil kumar 2024년 4월 18일
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
Daniele Marchetti
Daniele Marchetti 2021년 3월 2일
Hi Francesco,
I've the same issue. I can't go on.
Any suggestion will be appreciated.
Thanks for your interest,
Daniele

댓글을 달려면 로그인하십시오.

답변 (1개)

T.Nikhil kumar
T.Nikhil kumar 2024년 4월 18일
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!

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by