Dynamics 365 Business Central: Convert Word (.docx) To PDF (.pdf) within BC (No control add-in and APIs)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about an interesting topic, how to convert Word (.docx) To PDF (.pdf) within BC (No control add-in and APIs).

Microsoft Word is a word processing program developed by Microsoft. And in Business Central, with Word layouts, you use Word as the editor for the report.

In Business Central, you can save a report to a PDF document, Microsoft Word document, Microsoft Excel workbook, or XML document by choosing Send to, then making your selection. A file is downloaded to your computer.

This is very convenient. PDF is an abbreviation that stands for Portable Document Format. Unlike Word files, they generally cannot be modified and are mostly used to save archive files. As you know, we can convert to PDF files through Word, or through third-party websites or tools, but do you know that there is also such a method in BC?
codeunit 9651 “Document Report Mgt.” -> procedure ConvertWordToPdf(var TempBlob: Codeunit “Temp Blob”; ReportID: Integer)

Let’s look at two simple tests.

1. Convert the word file selected in Document Attachment Details into a pdf file

Test video:

2. Upload a Word document and convert it to PDF

Test video:

Very simple, give it a try!!!😁

Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

pageextension 50112 DocumentAttachmentDetailsExt extends "Document Attachment Details"
{
    actions
    {
        addafter(Preview_Promoted)
        {
            actionref(ConvertWordToPDF_Promoted; ConvertWordToPDF)
            {
            }
        }
        addfirst(processing)
        {
            action(ConvertWordToPDF)
            {
                Caption = 'Convert Word To PDF';
                ApplicationArea = All;
                Image = View;

                trigger OnAction()
                var
                    DocAttach: Record "Document Attachment";
                    DocAttachPDF: Record "Document Attachment";
                    InStr: InStream;
                    OutStr: OutStream;
                    TempBlob: Codeunit "Temp Blob";
                    DocumentReportMgt: Codeunit "Document Report Mgt.";
                    FileName: Text;
                begin
                    DocAttach.Reset();
                    CurrPage.SetSelectionFilter(DocAttach);
                    if DocAttach.FindFirst() then
                        if DocAttach."Document Reference ID".HasValue then begin
                            TempBlob.CreateOutStream(OutStr);
                            DocAttach."Document Reference ID".ExportStream(OutStr);
                            DocumentReportMgt.ConvertWordToPdf(TempBlob, 0);
                            TempBlob.CreateInStream(InStr);
                            FileName := DocAttach."File Name" + ' (1)';
                            DocAttachPDF.Init();
                            DocAttachPDF.Validate("Table ID", DocAttach."Table ID");
                            DocAttachPDF.Validate("No.", DocAttach."No.");
                            DocAttachPDF.Validate("File Name", FileName);
                            DocAttachPDF.Validate("File Extension", 'pdf');
                            DocAttachPDF."Document Reference ID".ImportStream(InStr, FileName);
                            DocAttachPDF.Insert(true);
                        end;
                end;
            }
        }
    }
}

pageextension 50100 CustomerListExt extends "Customer List"
{
    actions
    {
        addfirst(processing)
        {
            action(ConvertWordToPDF)
            {
                ApplicationArea = All;
                Caption = 'Convert Word To PDF';
                Image = Change;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction()
                var
                    WordFileName: Text[100];
                    PDFFileName: Text[100];
                    InStr: InStream;
                    OutStr: OutStream;
                    FileMgt: Codeunit "File Management";
                    TempBlob: Codeunit "Temp Blob";
                    DocumentReportMgt: Codeunit "Document Report Mgt.";
                begin
                    TempBlob.CreateOutStream(OutStr);
                    FileMgt.BLOBImport(TempBlob, WordFileName);
                    DocumentReportMgt.ConvertWordToPdf(TempBlob, 0);
                    TempBlob.CreateInStream(InStr);
                    PDFFileName := 'Converted' + '.pdf';
                    DownloadFromStream(InStr, '', '', '', PDFFileName);
                end;
            }
        }
    }
}

PS: If you encounter a file that cannot be converted, the following error message will be displayed, so it is okay not to add a control on the file type.

The document contains elements that cannot be converted to PDF. This may be caused by missing image data in the document.

Update: Dynamics 365 Business Central: Convert Word (.docx) To HTML (.html) within BC (No control add-in and APIs)

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL