ASP.NET PDF Viewer Control
How to view, convert PDF file to Tiff with image options in web browser using C#, ASP.NET PDF viewer web control


Easy to read, open, view, markup PDF document online, and convert, export to Tiff with image options using ASP.NET C# in web browser











About this tutorial



This tutorial page will setup an ASP.NET web site based on EdgePDF demo project which allows your web users to read, view, annotate, redate PDF document content in web browser, and convert updated PDF file to Tiff image file with image options online.






How to read, view, update PDF document online, convert updated PDF to tiff document using C#, asp.net



Using EdgePDF ASP.NET PDF Viewer and Editor web control, you can easily allow your web user to open, view, navigate, markup, comment PDF documents online in web browser, and convert, export online updated PDF file to tiff with image options.

The following steps and code snippet shows you how to convert, export modified PDF to tiff image document with image options using C#, in asp.net


  1. Setup EdgePDF demo project in IIS
  2. In demo project UserCommandProcessHandler.ashx, convert user saved PDF file to tiff file with image settings
  3. Save new/updated tiff file on the web server


The following source code shows how to convert, export saved, modified pdf document to tiff file with image options in UserCommandProcessHandler.ashx



    public void ApplyAddonTiffInSaveFunction(PDFDocument pdfDoc, String outputFilePath)
    {
        //  Verify the output file path and ensure the extension is ".tif"/".tiff".
        if (!(outputFilePath.ToLower().EndsWith(".tif") || outputFilePath.ToLower().EndsWith(".tiff")))
            outputFilePath += ".tif";

        //  Delete exist file
        if (File.Exists(outputFilePath))
            File.Delete(outputFilePath);

        ImageOutputOption options = new ImageOutputOption();
        //  Output image is color.
        options.Color = ColorType.Color;
        //  Use LZW compression in TIFF file.
        options.Compression = ImageCompress.LZW;
        //  Set resolution to 300 dpi for each page.
        options.Resolution = 300;

        //  Save PDF as a TIFF file by the specified settings.
        using (FileStream fs = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            pdfDoc.ConvertToDocument(DocumentType.TIFF, fs, options);
        }
    }








Complete source code of UserCommandProcessHandler.ashx



You can find the complete source code for converting PDF to tiff in EdgePDF with image option settings in the downloaded package /DemoProjects/EdgePDF Demo Project/RasterEdge_Resource_Files/Tutorials/UserCommandProcessHandler-addon-tiff.ashx

To use the above tutorial, you need copy the UserCommandProcessHandler-addon-tiff.ashx to the EdgePDF project location /RasterEdge_Resource_Files/, and change file name to UserCommandProcessHandler.ashx



<%@ WebHandler Language="C#" Class="UserCommandProcessHandler" %>

using System;
using System.IO;
using System.Web;
using RasterEdge.Imaging.Basic;
using RasterEdge.XDoc.TIFF;
using RasterEdge.XDoc.PDF;
using RasterEdge.WDP;
using RasterEdge.XDoc.PDF.HTML5Editor;

public class UserCommandProcessHandler : ProcessHandler
{

    /*
         The SDK will use the following url parameter names, please do not use them
          RECommand, src, filepath, restful
    */
    public override PDFWebDocument FileProcess()
    {
        HttpRequest request = this.Context.Request;

        if (!String.IsNullOrEmpty(request.QueryString["yourtarget"]))
        {
            // load file
            String docid = request.QueryString["yourtarget"];
            if (docid == null) docid = "0";
            byte[] dataBytes = System.IO.File.ReadAllBytes(@"C:\\temp\" + docid);
            String fileName = docid;
            return REProcessControl.PageLoadFile(request, this.Manager, dataBytes, fileName);
        }
        else
        {
            Logger.Log(">>> Unknown load file mode. Load default file defined in Web.config.", LogType.DEBUG);

            // load default file. defined in Web.config
            return REProcessControl.PageLoadFile(request, this.Manager, "", LoadType.Server);
        }
    }

    //  fid: Task ID.
    //  savePath: The abosolute path of the saved PDF file in the task's output folder.
    //  responseMsg: The response message for saving the current document to the task's output folder.
    //               The string value is the relative path of the output file. "Error" means that the saving process is failed.
    public override void SaveFile_OnServer(String fid, String savePath, String responseMsg)
    {
        //  You can process saved pdf document here, like add watermark, ...
        var documentPath = savePath;
        if (!String.IsNullOrEmpty(documentPath))
        {
            Logger.LogFile(fid, "SaveFileOnServer: output file path " + documentPath, LogType.DEBUG);

            //  To verify the output file.
            RasterEdge.Imaging.Basic.BaseDocument document = null;
            if (documentPath.EndsWith(".pdf"))
            {
                // document object includes user modified content
                document = new RasterEdge.XDoc.PDF.PDFDocument(documentPath);
            }
            if (document != null)
            {
                RasterEdge.XDoc.PDF.PDFDocument pdfDoc = document as RasterEdge.XDoc.PDF.PDFDocument;
                try
                {
                    //  Post-process
                    RasterEdge.XDoc.PDF.PDFMetadata metadata = pdfDoc.GetDescription();
                    metadata.Producer = "RasterEdge EdgePDF";
                    pdfDoc.SetDescription(metadata);
                }
                catch (Exception ex)
                {
                    //  Process error code, and return error information here
                    Logger.LogFile(fid, ex.Message + ". fail to do post-process.", LogType.ERROR);
                }

                //  Get the upload information of the file
                RasterEdge.WDP.DocUploadInfo docinfo = RasterEdge.WDP.Manager.FileManager.getUploadinfoByFid(fid);
                //  Get your file open url parameters value, if needed.
                String paraFilepathValue = docinfo.GetRequestParameters("yourtarget");
                if (!String.IsNullOrEmpty(paraFilepathValue))
                {
                    try
                    {
                        //  Save file to the original path.
                        ApplyAddonTiffInSaveFunction(pdfDoc, @"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, "no 'yourtarget' in the HTTPRequest.", LogType.INFO);
                }
            }
            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);
        }
    }

    public void ApplyAddonTiffInSaveFunction(PDFDocument pdfDoc, String outputFilePath)
    {
        //  Verify the output file path and ensure the extension is ".tif"/".tiff".
        if (!(outputFilePath.ToLower().EndsWith(".tif") || outputFilePath.ToLower().EndsWith(".tiff")))
            outputFilePath += ".tif";

        //  Delete exist file
        if (File.Exists(outputFilePath))
            File.Delete(outputFilePath);

        ImageOutputOption options = new ImageOutputOption();
        //  Output image is color.
        options.Color = ColorType.Color;
        //  Use LZW compression in TIFF file.
        options.Compression = ImageCompress.LZW;
        //  Set resolution to 300 dpi for each page.
        options.Resolution = 300;

        //  Save PDF as a TIFF file by the specified settings.
        using (FileStream fs = File.Open(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            pdfDoc.ConvertToDocument(DocumentType.TIFF, fs, options);
        }
    }
}