问题描述
我在做应该与密码显示PDF文件的应用程序。这是我的code:
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(!Page.IsPostBack)
{
尝试
{
字符串文件路径=的Request.QueryString [文件路径];
如果(filePath.ToUpper()的endsWith(PDF))
{
copyPDF(文件路径);
}
}
抓住
{
字符串消息=< SCRIPT LANGUAGE =JavaScript'等>警报(找不到文件通话记录司进行!')< / SCRIPT>中;
ScriptManager.RegisterStartupScript(页,this.GetType(),消息,消息,假的);
}
}
}
公共无效copyPDF(字符串文件路径)
{
iTextSharp.text.pdf.RandomAccessFileOrArray RA =新iTextSharp.text.pdf.RandomAccessFileOrArray(使用Server.Mappath(RESOLVEURL(文件路径)));
如果(RA!= NULL)
{
System.IO.MemoryStream毫秒=新System.IO.MemoryStream();
字节[] PASSWORD = System.Text.ASCIIEncoding.ASCII.GetBytes(Secretinfo);
iTextSharp.text.pdf.PdfReader thepdfReader =新iTextSharp.text.pdf.PdfReader(RA,密码);
诠释页= thepdfReader.NumberOfPages;
iTextSharp.text.Document pdfDoc =新iTextSharp.text.Document();
iTextSharp.text.pdf.PdfCopy pdfCopy =新iTextSharp.text.pdf.PdfCopy(pdfDoc,MS); pdfDoc.Open();
INT I = 0;
而(I<页)
{
pdfCopy.AddPage(pdfCopy.GetImportedPage(thepdfReader,I + 1));
我+ = 1;
}
pdfDoc.Close();
字节] byteInfo = ms.ToArray();
Response.Clear();
Response.ContentType =应用程序/ PDF
Response.AddHeader(内容长度,byteInfo.Length.ToString());
Response.BinaryWrite(byteInfo);
Response.Flush();
到Response.End();
}
}
我的code没有问题打开PDF文件,没有密码,但它不能用密码打开的PDF,即使密码被提供。在应用程序执行捕捞来代替。似乎是什么毛病我code?
修改:
我删除的捕捉的看到抛出的异常。
It says the source of the error is Line 51.
Line 49: while (i < pages)
Line 50: {
Line 51: pdfCopy.AddPage(pdfCopy.GetImportedPage(thepdfReader, i + 1));
Line 52: i += 1;
Line 53: }
For certain operations on encrypted documents iText(Sharp) requires that the document not merely is opened with the user password but instead with the owner password. This corresponds to the definition of these passwords in the PDF specification:
iText(Sharp) currently does not check in detail the user access permissions specified in the document’s encryption dictionary but instead always requires the owner password for operations requiring certain permissions, and copying whole pages from a document definitively is one of them.
This been said, the iText(Sharp) developers are very much aware (due to many such questions asked)
- that iText(Sharp) users may be entitled to execute such operations even without the owner password on account of the before mentioned user access permissions specified in the document’s encryption dictionary,
- that there are myriad PDFs to which their respective owners applied an owner password (to prevent misuse by others) and then forgot it (or by using a randomly generated one never knew it to start with), and
- that iText(Sharp) (being open source) can easily be patched by anyone not to respect the differences between user and owner password.
To allow users to do what they are entitled to and to prevent the spreading of patched copies of the library, iText(Sharp) contains an override for this test in the PdfReader
class:
/**
* The iText developers are not responsible if you decide to change the
* value of this static parameter.
* @since 5.0.2
*/
public static bool unethicalreading = false;
Thus, by setting
PdfReader.unethicalreading = true;
you globally override this permission checking mechanism.
Please respect the rights of PDF authors and only use this override if you indeed are entitled to execute the operations in question.
这篇关于打开密码保护的PDF文件,iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!