VS2010下asp.net 对现有的PDF文档进行加密(利用iTextSharp)
2011-08-14 17:10
579 查看
到2011年8月iTextSharp最新版本下载地址:
http://download.csdn.net/source/3514917
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;
//加密代码
protected void Button1_Click(object sender, EventArgs e)
{
string PdfPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
PdfPath = PdfPath + "PDFFiles\\";
string PdfFIle = PdfPath + "008.PDF";
string sname = PdfFIle;//要加密的文件
string sname1 = PdfPath + "test.PDF";//加密后生成的文件
PdfReader reader = new PdfReader(sname);
int n = reader.NumberOfPages;
Document document = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sname1, FileMode.Create));
writer.SetEncryption(PdfWriter.STRENGTH128BITS, "123456", null, PdfWriter.AllowPrinting);
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
int i = 0;
// step 4: we add content
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
//
// step 5: we close the document
document.Close();
writer.Close();
}
说明:加密后的文件没有访问,但PDF阅读器左侧的总页数似乎有点问题,不影响使用。
有人解决的可以跟上。
http://download.csdn.net/source/3514917
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Diagnostics;
//加密代码
protected void Button1_Click(object sender, EventArgs e)
{
string PdfPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
PdfPath = PdfPath + "PDFFiles\\";
string PdfFIle = PdfPath + "008.PDF";
string sname = PdfFIle;//要加密的文件
string sname1 = PdfPath + "test.PDF";//加密后生成的文件
PdfReader reader = new PdfReader(sname);
int n = reader.NumberOfPages;
Document document = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(sname1, FileMode.Create));
writer.SetEncryption(PdfWriter.STRENGTH128BITS, "123456", null, PdfWriter.AllowPrinting);
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
int i = 0;
// step 4: we add content
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
//
// step 5: we close the document
document.Close();
writer.Close();
}
说明:加密后的文件没有访问,但PDF阅读器左侧的总页数似乎有点问题,不影响使用。
有人解决的可以跟上。
相关文章推荐
- 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版
- asp.net利用itextsharp生成pdf文档
- 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版(转)
- 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版
- 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版
- 在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本
- 一个PDF合并操作实现类,利用iTextSharp控件实现(http://www.diybl.com/course/4_webprogram/asp.net/asp_netxl/2008109/149644.html)
- 【译】在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本
- asp.net中利用加密盐进行加密字符串
- 【译】在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本
- 利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档
- 输出PDF文档到ASP.NET页面
- 利用VS2010,给自己的ASP.NET MVC添加自己的控制器模板
- 在ASP.NET中创建PDF-iTextSharp起步
- ASP.Net MVC——使用 ITextSharp 完美解决HTML转PDF(中文也可以)
- ASP.NET如何将Excel文档转换成pdf
- asp.net网站利用VS进行简易打包
- (译)利用ASP.NET加密和解密Web.config中连接字符串
- 【译】在Asp.Net中操作PDF - iTextSharp - 使用字体
- 利用iTextSharp加密PDF文件