web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

How do I extract pdf attachment through API

(0) ShareShare
ReportReport
Posted on by 57

I am trying to extract pdf attachment from business central through a custom api

I created an API page for the attachment document table but the pdf documents are not accessible.

  • Suggested answer
    Bilal Haider Profile Picture
    487 on at

    Hi,

    You can convert pdf to base64 text and send the base64 text to endpoint.

  • Suggested answer
    DAnny3211 Profile Picture
    11,427 Super User 2026 Season 1 on at

    hi

    Create a custom API page for the Attachment table. In the API page, you should include the fields you need to retrieve the PDF attachment, such as Attachment ID, Attachment Name, and Attachment Data.

    In the API page, add a codeunit that retrieves the Attachment Data field for the PDF attachment. You can use the "Attachment Management" codeunit to do this.

    Modify the "OnGetRecord" trigger in the API page to call the codeunit that retrieves the Attachment Data field. For example, you can add the following code to the "OnGetRecord" trigger:

    Trigger OnGetRecord()

    Var

       AttachmentMgt: Codeunit "Attachment Management";

    Begin

       AttachmentMgt.GetAttachmentStream(Attachment.ID, Attachment."File Name", PDFStream);

    End;

    In the above code, "AttachmentMgt" is the codeunit variable that references the "Attachment Management" codeunit. "PDFStream" is a variable that holds the stream of the PDF attachment.

    Return the PDF attachment as the response of the API call. You can use the "SendStream" function to do this. For example, you can add the following code to the "OnAfterGetRecord" trigger:

    Trigger OnAfterGetRecord()

    Begin

       Response.ContentDisposition := 'attachment;filename="' + Attachment."File Name" + '"';

       Response.ContentType := 'application/pdf';

       Response.SendStream(PDFStream);

    End;

    In the above code, "Response" is the response variable that is returned by the API page. "ContentDisposition" and "ContentType" are the HTTP headers that specify the file name and content type of the PDF attachment. "SendStream" sends the PDF stream as the response of the API call.

    DAniele

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 545 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 354 Super User 2026 Season 2

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 228 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans