ASP.NET Core PDF Viewer
C#: how to display, save, convert, export PDF file in server side file system or database in asp.net mvc razor project


Online Guide for saving Acrobat PDF file into HTML PDF Editor using Web browser

















How to Save file from EdgePDF into your file system



1. Define your saving command in viewer toolbar
For ASP.NET Core web app, navigate to file {EdgePDF Demo Project}/wwwroot/RasterEdge_Resource_Files/Javascript/ RasterEdge_WebApp_Customize.js
For ASP.NET (.net framework) project, navigate to {EdgePDF Demo Project}/RasterEdge_Resource_Files/Javascript/ RasterEdge_WebApp_Customize.js
Define your command in function initCustomize()

    var myIcon = new CToolbarIcon();
    myIcon.id="myIcon";
    myIcon.event = "saveOnServer()";
    myIcon.title = "Save on server";
    myIcon.reqDoc = true;


    var myGroup = new CToolbarIconGroup();
    myGroup.name="Save Server";
    myGroup.addIcon(myIcon);

    var myTab = new CToolbar();
    myTab.id = "Customizebar";
    myTab.name = "Customize";
    myTab.addIconGroup(myGroup);

    addToolbarTab(myTab);



2. Define server side processing method to handle your command

// demo function to save the file on server
function saveOnServer() {
    // whether the file is loaded
    if (getCurrentFileId() == "")
        return;
    // get the basic save datas from API(getSaveDatas)
    var datas = getSaveDatas();
    // set the customize calling function in userCommandProcessHandler
    datas.action = "SaveFileOnServer";
    var options = {
        type: "POST",
        url: getServerHandlerUrl(),
        async: false,
        data: datas,
        success: function (result) {
            // hide the loading panel
            $("#loading").hide();
            // get the message
            var arr = eval('(' + result + ')');
            if (arr.state == "success") {
                saveFile = arr.msg;
                // open the file that store on server
                window.open(_cacheFolder + "/" + getCurrentFileId() + "/" + saveFile);
            }
            else {
                alert(arr.msg);
            }
        },
        error: function (err) {
            $("#loading").hide();
        }
    }
    // show the loading panel
    $("#loading").show();
    // send the message
    $.ajax(options);
}



3. Process your saving command in the server side
For ASP.NET Core web app, navigate to file {EdgePDF Demo Project}/UserCommandProcessMiddleware.cs
For ASP.NET (.net framework) project, navigate to {EdgePDF Demo Project}/ RasterEdge_Resource_Files/ UserCommandProcessHandler.ashx

public void SaveCallBack(string fid,string savepath,string responsemsg) {

        // you can process saved pdf document here, like add watermark, ...
        var documentPath = savepath;
        RasterEdge.Imaging.Basic.BaseDocument document = null;
        if (!string.IsNullOrEmpty(documentPath))
        {
            Logger.LogFile(fid, "SaveFileOnServer: output file path " + documentPath, LogType.DEBUG);

            if (documentPath.EndsWith(".pdf"))
            {
                document = new RasterEdge.XDoc.PDF.PDFDocument(documentPath);
            }

            if (document != null)
            {
                RasterEdge.XDoc.PDF.PDFDocument pdfDoc = document as RasterEdge.XDoc.PDF.PDFDocument;

                //  post-process
                String creatorName = "Hello";    //  creator name
                postSaveFileProcess(pdfDoc, creatorName);

                // get the upload information of the file
                RasterEdge.WDP.DocUploadInfo docinfo = RasterEdge.WDP.Manager.FileManager.getUploadinfoByFid(fid);

                string filename = docinfo.FileName;
                // get your file open url parameters value, if needed.
                string paraFilepathValue = docinfo.GetRequestParameters("yourtarget");

                RasterEdge.XDoc.PDF.PDFMetadata metadata = pdfDoc.GetDescription();
                metadata.Producer = "RasterEdge EdgePDF";
                pdfDoc.SetDescription(metadata);

                try
                {
                    //pdfDoc.Save(docinfo.LoadPath);
                    //pdfDoc.Save(@"C:\temp\test.pdf");
                    pdfDoc.Save(@"C:\temp\" + paraFilepathValue);
                }
                catch (Exception ex)
                {
                    // process error code, and return error information here
                    Logger.LogFile(fid, ex.Message + ". fail to save file to server.", LogType.ERROR);
                }
            }
            else
            {
                Logger.LogFile(fid, "output PDF file is invalid.", LogType.ERROR);
            }
        }
        else
        {
            Logger.LogFile(fid, "fail to save file to server. output file path is null or empty.", LogType.ERROR);
        }

    }



After complete the ASP.NET PDF viewer web application, you could easily publish the ASP.NET MVC web app to Azure cloud service. View details at How to deploy ASP.NET MVC PDF viewer web application to Azure service?







About document object in method "SaveCallBack"



In file UserCommandProcessHandler.ashx method "SaveCallBack", you can get a PDF document object using


                document = new RasterEdge.XDoc.PDF.PDFDocument(documentPath);



The object "document" here includes all user modifications in web browser. If a user has added new annotations on the opened PDF file, the object includes those new annotations.







Get PDF file open request url parameters' values



During pdf file opening request, you can add url parameters and values. All of these parameters and values have been stored in EdgePDF cached content.

You can get url parameters' values through the following codes:


                string paraFilepathValue = docinfo.GetRequestParameters("yourtarget");