Articles in this section
Category / Section

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

2 mins read

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-121210770https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfViewer_PrintandDownload-121210770

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied