iTextSharp对现有的PDF进行加密

xjzdr 2011-08-14 04:01:59
用iTextSharp对现有的PDF进行加密,总搞不定,要么加密后的文件为空了(原来是有内容的),或者出现问题:The document has no pages.(再打开PDF时错误:出现共享违规)
我的代码如下:

protected void btnEncryption3_Click(object sender, EventArgs e)
{
iTextSharp.text.Document document = new Document(PageSize.A4, 50, 50, 50, 50);
string PdfPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
PdfPath = PdfPath + "PDFFiles\\";
string PdfFIle = PdfPath + "004.PDF";
try
{


PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(PdfFIle, FileMode.Open));

writer.SetEncryption(PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);


document.Open();


document.Close();
}
catch (Exception de)
{
Console.Error.WriteLine(de.Message.ToString()+de.StackTrace);
//document.Close();
}

}

说明:我把 document.Open();


document.Close();
这两句去了,也不行,加上 writer.Close();也不行。

请做过、用过的大侠们把这段代码贴出来呀,帮帮忙。

...全文
326 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
imfor 2011-10-08
  • 打赏
  • 举报
回复
绑定!
amovingsun 2011-09-29
  • 打赏
  • 举报
回复
楼上代码少却功能实现的很充分,顶
子夜__ 2011-08-14
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text.pdf;
using System.IO;

namespace PDFSecurity
{
class PDFEncry
{
public static void Encry(string pdfSrc, string pdfDest, bool strength,
string userPassword, string ownerPassword, int permissions)
{
PdfReader reader = new PdfReader(pdfSrc);
Stream os = (Stream)(new FileStream(pdfDest, FileMode.Create));
PdfEncryptor.Encrypt(reader, os, strength, userPassword, ownerPassword, permissions);
}
}
}

代码很少。
xjzdr 2011-08-14
  • 打赏
  • 举报
回复
用过的同僚们,请把代码整出来看看呀

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧