Thursday, August 4, 2011

iTextSharp: Disable PDF Printing

It took me ages to find out how to disable printing on a PDF file. You need to encrypt the file in order set copying and printing settings.  Here's how:

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
writer.SetEncryption(null, encoding.GetBytes("12345678"), PdfWriter.ALLOW_COPY, PdfWriter.STRENGTH40BITS);

And this is how to enable printing if you still want the pdf to be encrypted.

System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
writer.SetEncryption(null, encoding.GetBytes("12345678"), PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter.STRENGTH40BITS);


  1. Thanks this saved lots of time...

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  2. The code is useful to configure the printing mechanism. It's a useful tool.

    large printing

  3. Thanks for sharing these wonderful and useful tips. I also experienced same problem and I wasn't able to resolve it, not until I read your blog.
    data protection

  4. I'm glad you shared this procedure to us. I've been thinking about it in a while now. Very useful indeed.

    long island document scanning

  5. I'm so grateful that you made this post about this common problem. The code really helps a lot.

    bristol printing

  6. This is useful for secure documents. You can prevent it from being printed without permission.

    seo company

  7. Here is the code for VB.Net in pdf files exist

    Policy: No Print


    filename =file to apply Encrypt

    strPass = password to apply

    Imports Microsoft.VisualBasic

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Web
    Imports System.Web.UI

    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    Imports iTextSharp.text.api
    Imports System.IO

    Public Class encriptaPDF

    Public Sub EncriptaArchivo(ByVal filename As String, strPass As String)

    Dim FileNew As String

    Dim Encoding As New System.Text.UTF8Encoding()

    Dim reader As New PdfReader(filename)

    FileNew = Replace(filename, ".pdf", "excrypt.pdf")

    Dim stamper = New PdfStamper(reader, New FileStream(FileNew, FileMode.Create))

    stamper.SetEncryption(Nothing, Encoding.GetBytes(strPass), PdfWriter.AllowScreenReaders, PdfWriter.STRENGTH40BITS)




    File.Copy(FileNew, filename)


    End Sub

    End Class

  8. Thanks for the tip. I often use pdf to keep my copy on being duplicated. However, the printing option is always there. Now I can change it with your instruction. eco print

  9. disabling pdf printing very easy i don't have idea of this before. SEO Company The Seo Portal

  10. i am using crystal report to export to pdf and then put it into itextsharp to set permission for printing, copy, etc..
    but when i do that i get blank pdf pages. if i take out the setencryption line, it will display fine. can you show me why?
    Dim oStream As MemoryStream
    ' using System.IO
    oStream = DirectCast(UCRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat), MemoryStream)
    Dim reader As New PdfReader(oStream.ToArray)
    Dim stamper = New PdfStamper(reader, oStream)
    Dim encoding As System.Text.UTF8Encoding = New System.Text.UTF8Encoding
    stamper.SetEncryption(Nothing, encoding.GetBytes("222"), PdfWriter.AllowScreenReaders, PdfWriter.STRENGTH40BITS)

    Response.Buffer = True
    Response.ContentType = "application/pdf"

  11. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. ClubFlyers

  12. The index PDF creator is one of the indexes creating software compatible with the adobe software that can be used to maintain an index for every PDF document. This software was originally developed and used in Washington DC.Adobe After Effects CC 2014

  13. By keeping the outline straightforward, the layout can be continued record and the information sheet might be refreshed as required.Varick Street Litho

  14. Typical measured reports and such will be simple for you to duplicate at home, and presumably more efficient that route also. In the event that you have something bigger to do, it may be a superior plan to go to a duplicate shop and have it done color copies prints


  15. Thanks for every other informative site. The place else may just I get that kind of information written in such an ideal means? I have a venture that I’m just now operating on, and I have been on the look out for such information.
    Luxury printing

  16. When it comes to running successful businesses, advertising plays a vital role in modern day times, as the business market has become exceptionally competitive in more ways than one. Many companies make use of posters, billboards, flyers and business cards which are displayed at various points. Advertising is used to increase sales and services. Roll up Stands