|
ASP.NET PDF Viewer Control
How to disable saved pdf document print, content modification permissions using C# in EdgePDF
Easy to upload, open, view, display, markup PDF document online, and disable print, modification permissions using ASP.NET C# in web browser
About this tutorial
This tutorial page will setup an ASP.NET web site which allows users to open, view, markup, redate PDF, Word, Excel, tiff files content in web browser, and save modified PDF file online without print, modification permissions.
How to view, modify PDF online, save modified PDF without print, content modification permission using C#, asp.net
Using EdgePDF, you can allow your web user to
open, view, markup PDF documents online in web browser, and save, export modified PDF file without print, content modification permissions.
The following steps and code snippet shows you how to save, export modified PDF without print, modification permission using C#, asp.net
- Setup EdgePDF demo project in IIS
- In demo project UserCommandProcessHandler.ashx, apply PDF document permission settings on user saved PDF file
- Save new/updated PDF file on the web server
The following source code shows how to apply PDF file permission settings on saved, exported pdf document in UserCommandProcessHandler.ashx
public void ApplyAddonPDFInSaveFunction(PDFDocument pdfDoc, String outputFilePath)
{
// Delete exist file
if (File.Exists(outputFilePath))
File.Delete(outputFilePath);
using (FileStream fs = File.Open(outputFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (MemoryStream ms = new MemoryStream())
{
// Save to file stream.
pdfDoc.SaveToStream(ms);
// Apply password to the output file stream with the specified access permissions.
PasswordSetting pwSetting = new PasswordSetting("OwnerPassword");
// Disable print the document
pwSetting.IsPrint = false;
// Disable modify the contents of the document
pwSetting.IsModify = false;
// Disable copy content from the document
pwSetting.IsCopy = false;
// Disable extract content from the document
pwSetting.IsExtract = false;
// Disable add/modify annotations
pwSetting.IsAnnot = false;
// Disable fill in existing form fields
pwSetting.IsFillForm = false;
// Disable assemble the document
pwSetting.IsAssemble = false;
PDFDocument.AddPassword(ms, fs, pwSetting);
}
}
}
Complete source code of UserCommandProcessHandler.ashx
You can find the complete source code for applying PDF file permission settings on saved, exported pdf document in the downloaded package
/DemoProjects/EdgePDF Demo Project/RasterEdge_Resource_Files/Tutorials/UserCommandProcessHandler-addon-pdf.ashx
To use the above tutorial, you need copy the UserCommandProcessHandler-addon-pdf.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.Web;
using System.IO;
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.
ApplyAddonPDFInSaveFunction(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 ApplyAddonPDFInSaveFunction(PDFDocument pdfDoc, String outputFilePath)
{
// Delete exist file
if (File.Exists(outputFilePath))
File.Delete(outputFilePath);
using (FileStream fs = File.Open(outputFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (MemoryStream ms = new MemoryStream())
{
// Save to file stream.
pdfDoc.SaveToStream(ms);
// Apply password to the output file stream with the specified access permissions.
PasswordSetting pwSetting = new PasswordSetting("OwnerPassword");
// Disable print the document
pwSetting.IsPrint = false;
// Disable modify the contents of the document
pwSetting.IsModify = false;
// Disable copy content from the document
pwSetting.IsCopy = false;
// Disable extract content from the document
pwSetting.IsExtract = false;
// Disable add/modify annotations
pwSetting.IsAnnot = false;
// Disable fill in existing form fields
pwSetting.IsFillForm = false;
// Disable assemble the document
pwSetting.IsAssemble = false;
PDFDocument.AddPassword(ms, fs, pwSetting);
}
}
}
}
|