We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to print and download the particular pages of the PDF document being displayed in PDF viewer

Platform: ASP.NET Web Forms |
Control: PdfViewer |
Published Date: August 29, 2018 |
Last Revised Date: June 19, 2019

Currently, PDF viewer does not have support to print and download the particular pages of the PDF document being displayed. However, as a workaround, you can print and download the required pages by importing the selected page using the PDF library and display in PDF viewer using load() API.

In this workaround, PDF document pages are exported into images and placed as thumbnail view along with the check boxes for each image. Then, the pages selected using the check boxes are imported into new document and loaded in the PDF viewer.

Refer to the following steps:

Step 1: Export the PDF document pages into images.

JavaScript

$.ajax({
            url: '../api/PdfViewer/ExportAsImage',
            type: 'POST',
            dataType: 'json',
            crossDomain: true,
            traditional: true,
            contentType: 'application/json; charset=utf-8',
            data: '',
            success: function (data) {
                thumbnail(data);
                //Initializes PDF viewer
                $('#container').ejPdfViewer({ serviceUrl: '../api/PdfViewer' })
            },
            error: function (msg, textStatus, errorThrown) {
                alert('Exception' + msg.responseText);
            }
        });       

C#

public object ExportAsImage()
        {
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(HttpContext.Current.Server.MapPath("~/Data/HTTP Succinctly.pdf"));
            //Exports the loaded PDF document to images
            Bitmap[] img = loadedDocument.ExportAsImage(0, loadedDocument.Pages.Count - 1);
            string outputPath = HttpContext.Current.Server.MapPath("~/Images/");
            for (int i = 0; i < img.Length; i++)
            {
                img[i].Save(outputPath + "\\Image" + (i + 1) + ".png");
            }
            string output = (loadedDocument.Pages.Count).ToString();
            return output;
        }

Step 2: Place the exported images as thumbnail view with the check boxes.

HTML

<div style="left:2px;">
        <div id="thumbnailpane" style="overflow:scroll;width:15%;height:640px">
        </div>
    </div>

JavaScript

function thumbnail(totalpage) {
            //Create thumbnail pane and place the exported images
            var thumbdiv = document.getElementById("thumbnailpane");
            for (var i = 1; i <= totalpage; i++) {
                var thumbimg = document.createElement('div');
                thumbimg.className = 'thumbnail';
                //creating check boxes
                var newCheckBox = document.createElement('input');
                newCheckBox.type = 'checkbox';
                newCheckBox.style.width = "20px";
                newCheckBox.style.height = "20px";
                newCheckBox.id = "Selectedcheckbox_" + (i - 1);
                newCheckBox.className = "thumbnailcheckbox"
                var img = document.createElement('img');
                img.src = '../images/image' + i + '.png';
                img.style.height = "210px";
                img.style.width = "150px";
                thumbimg.id = i;
                thumbimg.appendChild(newCheckBox);
                thumbimg.appendChild(img);
                thumbdiv.appendChild(thumbimg);
            }
        }

Step 3: Place a button to import the pages selected using the check boxes and the newly created document is displayed in PDF viewer.

HTML

<input type="button" style="width:80px;height:30px" value="Generate" onclick="ImportFiles()" />

JavaScript

function ImportFiles() {
            var jsonResult = {};
            var checkedpages = [];
            var checkbox = $('.thumbnailcheckbox');
            for (var i = 0; i < checkbox.length; i++) {
                if (checkbox[i].checked == true) {
                    var id = checkbox[i].id.split('_')[1];
                    checkedpages.push(id);
                }
            }
            jsonResult["checkedField"] = JSON.stringify(checkedpages);
            $.ajax({
                url: '../api/PdfViewer/ImportPages',
                type: 'POST',
                dataType: 'json',
                crossDomain: true,
                traditional: true,
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(jsonResult),
                success: function (data) {
                    var pdfviewerObject = $('#container').data('ejPdfViewer');
                    //Loading the document in PDF viewer
                    pdfviewerObject.load(data);
                },
                error: function (msg, textStatus, errorThrown) {
                    alert('Exception' + msg.responseText);
                }
            });
        }

C#

public object ImportPages(Dictionary<string, string> jsonResult)
        {
            string outputPath = HttpContext.Current.Server.MapPath("~/Data/");
            PdfLoadedDocument loadedDocument = new PdfLoadedDocument(HttpContext.Current.Server.MapPath("~/Data/HTTP Succinctly.pdf"));
            var values = jsonResult["checkedField"];
            string[] strings = JsonConvert.DeserializeObject<string[]>(values);
            PdfDocument document = new PdfDocument();
            for (var i = 0; i < strings.Length; i++)
            {
                int pages = Convert.ToInt32(strings[i]);
                document.ImportPage(loadedDocument, pages);
            }
            document.Save(outputPath + "Output.pdf");
            return "Output";
        }

Sample:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfViewer_PrintandDownload-121210770

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile