| |
| ASP.NET |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<%@ Page language="c#" Codebehind="Security.aspx.cs" AutoEventWireup="false"
Inherits="Aspose.Pdf.Kit.Demos.WebForm1"
MasterPageFile="~/tpl/Demo.Master"
Title="Set Pdf Security - Aspose.Pdf.Kit Demos"
%>
<%@ Register TagPrefix="dem" Assembly="Aspose.Demos.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab721518795be73e" Namespace="Aspose.Demos.Common.HtmlControls" %>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainContent">
<table width="90%" align="center" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="19"><img src="../../../../Common/images/heading_lft.jpg" alt="" width="19" height="41" /></td>
<td width="100%" class="demos-heading-bg"><h2 class="demos-heading-bg"> Set PDF Security - Aspose.Pdf.Kit</h2></td>
<td width="19"><img src="../../../../Common/images/heading_rt.jpg" alt="" width="19" height="41" /></td>
</tr>
<tr> </tr>
</table>
<p class="MsoNormal"><font size=2 face="Arial" ><span style="color:black" >This demo applies <b> Security </b> settings over input Pdf document, based over users selection.
<br><br>Using <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/aspose.pdf.kit.pdffilesecurity.encryptfile_overloads.html> EncryptFile </a> method in <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/aspose.pdf.kit.pdffilesecurity.html> PdfFileSecurity </a> class of Aspose.Pdf.Kit component, developers can make PDF documents secured, by simply encrypting the documents.You can also set privileges on a PDF document without applying encryption. Using <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/aspose.pdf.kit.pdffilesecurity.setprivilege_overloads.html> SetPrivilege </a> method in <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/aspose.pdf.kit.pdffilesecurity.html> PdfFileSecurity </a> class of Aspose.Pdf.Kit component, several privileges can be applied on a PDF document.
<br><br>
For more information, please visit <a href=http://www.aspose.com/documentation/.net-components/aspose.pdf.kit-for-.net/implementing-security.html>Implementing Security. </a><br></span>
<p class="componentDescriptionTxt">
Check security options and click <b>Do</b> to see how demo takes <a href="./resources/Aspose.Pdf.Kit.pdf">input PDF file</a>
, applies security options and sends the resulting document to user for review.
</p>
<table class="genericTable">
<tr>
<td>You can allow</td>
<td>You can encrypt file with</td>
</tr>
<tr>
<td valign="top" align="left">
<asp:checkboxlist id="CheckBoxList1"
runat="server" CssClass="genericList">
<asp:ListItem Value="1">Copying</asp:ListItem>
<asp:ListItem Value="2">Printing</asp:ListItem>
<asp:ListItem Value="3">Modifying content</asp:ListItem>
<asp:ListItem Value="4">Modifying annotation</asp:ListItem>
<asp:ListItem Value="5">Filling forms</asp:ListItem>
<asp:ListItem Value="6">Screening readers</asp:ListItem>
<asp:ListItem Value="7">Assemblying</asp:ListItem>
<asp:ListItem Value="8">DegradedPrinting</asp:ListItem>
</asp:checkboxlist>
</td>
<td valign="top" align="left">
<asp:textbox id="TextBox1" runat="server" Width="132px">ownerpassword</asp:textbox><br />
<asp:textbox id="TextBox2" runat="server" Width="134px">userpassword</asp:textbox>
</td>
</tr>
</table>
<asp:button id="Button1" runat="server" ToolTip="Click to insert" Text="Do"></asp:button>
</asp:Content>
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Aspose.Pdf.Kit;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Summary of WebForm1
/// </summary>
public class WebForm1 : BasePage
{
protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlGenericControl Font1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
//privileges are setted.
PdfPrivilege privilege = 0;
ArrayList privList = new ArrayList();
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
switch (i)
{
case 0: // set the value to allow copying file
privilege = privilege | PdfPrivilege.Copy;
break;
case 1: // set the value to allow printing file
privilege = privilege | PdfPrivilege.Print;
break;
case 2: // set the value to allow modifying file
privilege = privilege | PdfPrivilege.ModifyContents;
break;
case 3: // set the value to allow modifying annotations of file
privilege = privilege | PdfPrivilege.ModifyAnnotations;
break;
case 4: // set the value to allow filling forms in file
privilege = privilege | PdfPrivilege.FillIn;
break;
case 5: // set the value to Only permits to read on screen
privilege = privilege | PdfPrivilege.ScreenReaders;
break;
case 6: // set the value to allow assemblying file
privilege = privilege | PdfPrivilege.Assembly;
break;
case 7: // set the value to allow degraded printing
privilege = privilege | PdfPrivilege.DegradedPrinting;
break;
default:
break;
}
}
}
//ower and user password
string ownerpass = TextBox1.Text;
string userpass = TextBox2.Text;
// the input file that will be encrypted and sets the document's privileges to access
string inputfile = GetResource("Aspose.Pdf.Kit.pdf");
// create an instance of SecurityDemo class
SecurityDemo security = new SecurityDemo();
// call the Secure method of SecurityDemo class to encrypt the input file
security.Secure(inputfile, userpass, ownerpass, privilege, true, Response);
}
}
}
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
using System;
using System.IO;
using System.Web;
using Aspose.Pdf.Kit;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Summary of SecurityDemo.
/// </summary>
public class SecurityDemo
{
public SecurityDemo()
{
#if SITE_BUILD
// NOTE that in production you would want to call
// new License().SetLicense("path-to-license-file")
try
{
Aspose.Demos.Common.WebOperationsBridge.InitLicense(new License());
}catch
{
;
}
#endif
}
/// <summary>
/// Secure give document based on the specified parameters.
/// </summary>
/// <param name="inputfile"></param>
/// <param name="userpass"></param>
/// <param name="ownerpass"></param>
/// <param name="privilege"></param>
/// <param name="b128bits"></param>
/// <param name="Response"></param>
public void Secure(string inputfile,
string userpass,
string ownerpass,
PdfPrivilege privilege,
bool b128bits,
HttpResponse Response)
{
//read input Pdf documents
FileStream inStream = File.Open(inputfile,FileMode.Open,FileAccess.Read,FileShare.Read);
//generate new Pdf file output stream.
MemoryStream outStream = new MemoryStream();
//secure the Pdf file with PdfFileSecurity object
PdfFileSecurity pdfSecurity = new PdfFileSecurity(inStream,outStream);
//call EncryptFile method of PdfFileSecurity to encrypt the file with ownerpassword and PdfPriviliges to access
pdfSecurity.EncryptFile(userpass,ownerpass,privilege,true);
// create a byte array that will hold the output pdf
byte[] outBuf = outStream.GetBuffer();
// specify the duration of time before a page,cached on a browser expires
Response.Expires = 0;
// Specify the property to buffer the output page
Response.Buffer = true;
// Erase any buffered HTML output
Response.ClearContent();
//Add a new HTML header and value to the response sent to the client
Response.AddHeader("content-disposition","inline; filename=" + "output.pdf");
// Specify the HTTP content type for response as Pdf
Response.ContentType = "application/pdf";
// Write specified information of current HTTP output to Byte array
Response.BinaryWrite(outBuf);
// close the output stream
outStream.Close();
//end the processing of the current page to ensure that no other HTML content is sent
Response.End();
}
}
}
|
|
|
|