Articles in this section
Category / Section

How to Get the Byte Array of the PDF File

Published:
Updated:

A PDF byte array represents the binary data of a PDF document and can perform various operations and tasks. You can get the byte array of the PDF file in Bold Reports using the ReportHelper.GetReport method. Find the following steps to get the byte array of the PDF file in a custom button and click using the ReportHelper.GetReport method.

  1. Add a button to the page and implement the functionality for the button’s click event.
<button id="GetByteArrays">Get PDF ByteArray</button>
  1. In this button, click send request to the Web API server to get the byte array of the PDF file. Use the following JavaScript code to create the event with a custom button click:
 <script type="text/javascript">
       $(function () {
           $("#viewer").boldReportViewer({
               reportServiceUrl: "https://demos.boldreports.com/services/api/ReportViewer",
               reportPath: '~/Resources/docs/sales-order-detail.rdl'
           });

           $("#GetByteArrays").click(function() {
               var proxy = $('#viewer').data('boldReportViewer');
               var Report = proxy.model.reportPath;
               var lastsIndex = Report.lastIndexOf("/");
               var reportName = Report.substring(lastsIndex + 1);
               var requrl = proxy.model.reportServiceUrl + '/GetByteArray';

               var _json = {
                   exportType: "PDF",
                   reportViewerToken: proxy._reportViewerToken,
                   ReportName: reportName
               };

               $.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   url: requrl,
                   data: JSON.stringify(_json),
                   dataType: "json",
                   crossDomain: true,
                   success: function(response) {
                       console.log(response);
                   }
               });
           });
       });
   </script>
  1. Include the following code sample in your service application controller to handle the POST request. Using the ReportHelper.GetReport method, you get the file stream, convert to a byte array, and return it.
   [HttpPost]
   public object GetByteArray([FromBody] Dictionary<string, object> jsonResult)
   {
       string _token = jsonResult["reportViewerToken"].ToString();
       var stream = ReportHelper.GetReport(_token, jsonResult["exportType"].ToString(), this, _cache);
       stream.Position = 0;
       byte[] reportPDFByteArray;
       using (var streamReader = new MemoryStream())
       {
           stream.CopyTo(streamReader);
           reportPDFByteArray = streamReader.ToArray();
       }
       return reportPDFByteArray;
   }

By following these steps, you can obtain the byte array of a PDF file using the Bold Reports ReportHelper.GetReport method and a custom button click event.

Viewer.html
Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied