locked
asp.net itextsharp set IsCopyAllowed to false, how ? RRS feed

  • Question

  • User-775831949 posted

    I am using itextsharp in my aspx page

    What is the code to set output IsCopyAllowed and IsPrintingAllowed to false ?

    I tried

    PdfEncryptor.IsCopyAllowed(False)

    It does not work. Pls help.

    Thursday, November 13, 2014 4:18 AM

Answers

  • User-821857111 posted

    Permissions in iTextSharp work on the basis that you encrypt the document and set a user password to lock it, and once you have done that, you specifically allow selected actions. The following code stops anyone doing anything to the PDF:

    var path = Server.MapPath("PDFs");
    var doc1 = new Document();
    PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + "/PermissionsTest.pdf", FileMode.Create));
    writer.SetEncryption(PdfWriter.ENCRYPTION_AES_128, null, "mike", 0);

    That will achieve what you want. However, if you want to permit people to do certain things, you pass the permissions in as ints (you can use the built-in constants):

    writer.SetEncryption(PdfWriter.ENCRYPTION_AES_128, null, "mike", PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders);

    Note that you use the bitwise OR ( | ) operator

    PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders

    to set the permissions flags.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 18, 2014 4:22 AM
  • User-821857111 posted

    The VB equivalent to | is Or.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 20, 2014 2:48 AM
  • User-821857111 posted

    Try a VB version of the code I posted originally. It works for me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 20, 2014 11:26 AM
  • User-821857111 posted

    Don't give the user the same password as the owner of the document, otherwise they become the owner and can do everything. Either give the user a different password if the document shouldn't even be opened by non-authenticated people, or pass null to the UserPassword parameter.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 2:32 AM
  • User-821857111 posted

    As I said in my last post, all you need to do is to make sure the user and owner don't have the same password, then your code will work. Try it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 2:59 AM
  • User-821857111 posted

    When you encrypt the document, no permissions are allowed. You have to allow them one by one. I copied your code and all I did was remove the user password and the permissions worked as you want. Can you show the code you are using? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 3:41 AM

All replies

  • User-821857111 posted

    Permissions in iTextSharp work on the basis that you encrypt the document and set a user password to lock it, and once you have done that, you specifically allow selected actions. The following code stops anyone doing anything to the PDF:

    var path = Server.MapPath("PDFs");
    var doc1 = new Document();
    PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + "/PermissionsTest.pdf", FileMode.Create));
    writer.SetEncryption(PdfWriter.ENCRYPTION_AES_128, null, "mike", 0);

    That will achieve what you want. However, if you want to permit people to do certain things, you pass the permissions in as ints (you can use the built-in constants):

    writer.SetEncryption(PdfWriter.ENCRYPTION_AES_128, null, "mike", PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders);

    Note that you use the bitwise OR ( | ) operator

    PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders

    to set the permissions flags.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 18, 2014 4:22 AM
  • User-775831949 posted

    Thanks Mike,

    I get this error

    BC30037: Character is not valid.

    Intellisense says "|" is not a valid Character.

    My code is as follows

    Dim pdfDocument As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
            Using memoryStream As New MemoryStream()
                PdfWriter.GetInstance(pdfDocument, memoryStream)
                pdfDocument.Open()
                pdfDocument.Add(pdftable)
                pdfDocument.Close()
                Dim bytes As Byte() = memoryStream.ToArray()
                memoryStream.Close()
                Using input As New MemoryStream(bytes)
                    Using output As New MemoryStream()
                        Dim userpassword As String = "123"
                        Dim ownerpassword As String = "123"
                        Dim reader As New PdfReader(input)
                        PdfEncryptor.Encrypt(reader, output, True, userpassword, ownerpassword, 0)
        
                        
                        bytes = output.ToArray()
                        Response.ContentType = "application/pdf"
                        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
                        Response.Cache.SetCacheability(HttpCacheability.NoCache)
                        Response.BinaryWrite(bytes)
                        Response.End()
                    End Using
                End Using
            End Using

    How should I modify the code ? Thanks

    Thursday, November 20, 2014 12:15 AM
  • User-821857111 posted

    The VB equivalent to | is Or.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 20, 2014 2:48 AM
  • User-775831949 posted

    Thanks

    Mo more error message

    But in pdf, all permissions are allowed

    How to disallow?

    Thursday, November 20, 2014 10:05 AM
  • User-821857111 posted

    Try a VB version of the code I posted originally. It works for me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 20, 2014 11:26 AM
  • User-775831949 posted

    Thanks a lot Mike, really appreciate it
    However, my skill is limited and I am not able to apply your code to modify my code.

    Would you please help to modify my code below to disallow user from editing and printing the pdf ?

    Currently, the file output from this code below when opened up I found all permissions are allowed..

    Pls help. Thanks

    protected void Button2_Click(object sender, System.EventArgs e)
    {
    	Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    	using (MemoryStream memoryStream = new MemoryStream()) {
    		PdfWriter.GetInstance(pdfDocument, memoryStream);
    		pdfDocument.Open();
    		pdfDocument.Add(pdftable);
    		pdfDocument.Close();
    		byte[] bytes = memoryStream.ToArray();
    		memoryStream.Close();
    		using (MemoryStream input = new MemoryStream(bytes)) {
    			using (MemoryStream output = new MemoryStream()) {
    				string userpassword = "123";
    				string ownerpassword = "123";
    				PdfReader reader = new PdfReader(input);
    				PdfEncryptor.Encrypt(reader, output, true, userpassword, ownerpassword, PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders);
    				// PdfEncryptor.IsPrintingAllowed()
    				bytes = output.ToArray();
    				Response.ContentType = "application/pdf";
    				Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    				Response.Cache.SetCacheability(HttpCacheability.NoCache);
    				Response.BinaryWrite(bytes);
    				Response.End();
    			}
    		}
    	}
    }
    Thursday, November 20, 2014 10:04 PM
  • User-821857111 posted

    Don't give the user the same password as the owner of the document, otherwise they become the owner and can do everything. Either give the user a different password if the document shouldn't even be opened by non-authenticated people, or pass null to the UserPassword parameter.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 2:32 AM
  • User-775831949 posted

    Thanks for the tips The above is the test code.

    Would you pls modify my C# code above so disable permission in printing etc ?

    Thanks

    Friday, November 21, 2014 2:57 AM
  • User-821857111 posted

    As I said in my last post, all you need to do is to make sure the user and owner don't have the same password, then your code will work. Try it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 2:59 AM
  • User-775831949 posted

    I make it diff and the pdf permissions are still all "Allowed"... pls help...but the pdf is password protected as expected.

    So only permission not working as expected

    Do we need to disallow all first then allow one by one ?

    Friday, November 21, 2014 3:25 AM
  • User-821857111 posted

    When you encrypt the document, no permissions are allowed. You have to allow them one by one. I copied your code and all I did was remove the user password and the permissions worked as you want. Can you show the code you are using? 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 21, 2014 3:41 AM
  • User-775831949 posted

    Dear Mike, thanks for the great comment. I changed user password to nothing

    PdfEncryptor.Encrypt(reader, output, True, Nothing, ownerpassword, PdfWriter.AllowFillIn Or PdfWriter.AllowScreenReaders)

    Now (1) Permissions are disabled now as expected, which is good!!

    (2) However, user does not need password to open the document.

    What I need is both (1) and (2). ie disallow extract/copy/printing AND they need password to open too.

    How to fix it ? I want both. Thanks

    Friday, November 21, 2014 5:09 AM
  • User-775831949 posted

    OK now. I now set ownerpassword to null and set userpassword.

    Now permission is as expected and user need password to open pdf.

    I get what I want. Thanks for your help once again Mike.Really appreciate it.

    Just one last question, why I cannot have ownerpassword and set permission at the same time ?

    Thanks

    Friday, November 21, 2014 5:24 AM
  • User-821857111 posted

    You can set a user password and an owner password, but they must be different.

    protected void Button2_Click(object sender, System.EventArgs e)
    {
    	Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
    	using (MemoryStream memoryStream = new MemoryStream()) {
    		PdfWriter.GetInstance(pdfDocument, memoryStream);
    		pdfDocument.Open();
    		pdfDocument.Add(pdftable);
    		pdfDocument.Close();
    		byte[] bytes = memoryStream.ToArray();
    		memoryStream.Close();
    		using (MemoryStream input = new MemoryStream(bytes)) {
    			using (MemoryStream output = new MemoryStream()) {
    				string userpassword = "abc";
    				string ownerpassword = "123";
    				PdfReader reader = new PdfReader(input);
    				PdfEncryptor.Encrypt(reader, output, true, userpassword, ownerpassword, PdfWriter.AllowFillIn | PdfWriter.AllowScreenReaders);
    				// PdfEncryptor.IsPrintingAllowed()
    				bytes = output.ToArray();
    				Response.ContentType = "application/pdf";
    				Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
    				Response.Cache.SetCacheability(HttpCacheability.NoCache);
    				Response.BinaryWrite(bytes);
    				Response.End();
    			}
    		}
    	}
    }

    Friday, November 21, 2014 10:32 AM