Dynamics 365 Business Central: How to get the page number of a PDF or Word file in AL (Display the page count on Attached Documents and Doc. Attachment List Factbox)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to get the page number of a PDF or Word file in AL (Display the page number on Attached Documents and Doc. Attachment List Factbox).

In Business Cental, we can save the report as PDF or Word file.

And we can also save PDF and Word files in BC when uploading attachments.

By default, Business Central does not display the page count of these files, but if we want to display it, is there any way? Yes, we can get it through the following standard method.

codeunit 3110 “PDF Document”: Codeunit that provides helper functions for PDF processing.

 procedure GetPdfPageCount: Returns the number of pages in the provided PDF stream.

But please note that this method can only get the page number of PDF files. For Word files, we need to convert them to PDF first. More details: Convert Word (.docx) To PDF (.pdf) within BC (No control add-in and APIs)

Let’s look at a simple example to get the page count of an uploaded PDF file.

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

pageextension 50100 CustomerListExt extends "Customer List"
{
    actions
    {
        addafter("C&ontact")
        {
            action(GetPageCount)
            {
                ApplicationArea = All;
                Caption = 'Get PDF Page Count';
                Image = View;
                ToolTip = 'Get the number of pages in the PDF document.';
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    PDFDocument: Codeunit "PDF Document";
                    InStr: InStream;
                    FileName: Text;
                begin
                    UploadIntoStream('Select PDF file', '', '', FileName, InStr);
                    PDFDocument.Load(InStr);
                    Message('The PDF has %1 page(s).', PDFDocument.GetPdfPageCount(InStr));
                end;
            }
        }
    }
}

For Word files, as mentioned above, this requires conversion.

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

pageextension 50100 CustomerListExt extends "Customer List"
{
    actions
    {
        addafter("C&ontact")
        {
            action(GetPageCount)
            {
                ApplicationArea = All;
                Caption = 'Get Word Page Count';
                Image = View;
                ToolTip = 'Get the number of pages in the Word document.';
                Promoted = true;
                PromotedCategory = Process;

                trigger OnAction()
                var
                    PDFDocument: Codeunit "PDF Document";
                    InStr: InStream;
                    OutStr: OutStream;
                    FileName: Text;
                    FileMgt: Codeunit "File Management";
                    TempBlob: Codeunit "Temp Blob";
                    DocumentReportMgt: Codeunit "Document Report Mgt.";
                begin
                    TempBlob.CreateOutStream(OutStr);
                    FileMgt.BLOBImport(TempBlob, FileName);
                    DocumentReportMgt.ConvertWordToPdf(TempBlob, 0);
                    TempBlob.CreateInStream(InStr);
                    PDFDocument.Load(InStr);
                    Message('The Word document has %1 page(s).', PDFDocument.GetPdfPageCount(InStr));
                end;
            }
        }
    }
}

Next question, is it possible to display the page count in the Attached Documents and Doc. Attachment List Factbox page? Yes, we’ve discussed similar topics before. At that time, we displayed the file size. More details: How to add/display file size on Attached Documents and Doc. Attachment List Factbox

Test video:

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

tableextension 50102 DocumentAttachmentExt extends "Document Attachment"
{
    fields
    {
        field(50100; PageCount; Integer)
        {
            Caption = 'Page Count';
            Editable = false;
            DataClassification = CustomerContent;
        }
    }
}
pageextension 50102 DocumentAttachmentDetailsExt extends "Document Attachment Details"
{
    layout
    {
        addafter("File Type")
        {
            field(PageCount; Rec.PageCount)
            {
                ApplicationArea = All;
                Caption = 'Page Count';
                Editable = false;
                ToolTip = 'The number of pages in the document';
            }
        }
    }
}
pageextension 50113 DocAttachmentListFactboxExt extends "Doc. Attachment List Factbox"
{
    layout
    {
        addafter("File Extension")
        {
            field(PageCount; Rec.PageCount)
            {
                ApplicationArea = All;
                Caption = 'Page Count';
                Editable = false;
                ToolTip = 'The number of pages in the document';
            }
        }
    }
}
codeunit 50102 AttachmentHandler
{
    [EventSubscriber(ObjectType::Table, Database::"Document Attachment", OnBeforeSaveAttachment, '', false, false)]
    local procedure OnBeforeSaveAttachment(var DocumentAttachment: Record "Document Attachment"; var RecRef: RecordRef; var FileName: Text; var TempBlob: Codeunit "Temp Blob")
    var
        PDFDocument: Codeunit "PDF Document";
        InStr: InStream;
        OutStr: OutStream;
        PageCount: Integer;
        FileManagement: Codeunit "File Management";
        DocumentReportMgt: Codeunit "Document Report Mgt.";
        WordTempBlob: Codeunit "Temp Blob";
    begin
        PageCount := 0;
        if FileManagement.GetFileNameMimeType(FileName) = 'application/pdf' then begin
            TempBlob.CreateInStream(InStr);
            PDFDocument.Load(InStr);
            PageCount := PDFDocument.GetPdfPageCount(InStr);
            DocumentAttachment.PageCount := PageCount;
        end;
        if FileManagement.GetFileNameMimeType(FileName) = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' then begin
            TempBlob.CreateInStream(InStr);
            WordTempBlob.CreateOutStream(OutStr);
            CopyStream(OutStr, InStr);
            DocumentReportMgt.ConvertWordToPdf(WordTempBlob, 0);
            WordTempBlob.CreateInStream(InStr);
            PDFDocument.Load(InStr);
            PageCount := PDFDocument.GetPdfPageCount(InStr);
            DocumentAttachment.PageCount := PageCount;
        end;
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL