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);


41 comments:

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

    large printing

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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

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

    bristol printing

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

    seo company

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

    Policy: No Print

    Params:

    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)

    stamper.Close()

    reader.Close()


    File.Delete(filename)

    File.Copy(FileNew, filename)

    File.Delete(FileNew)


    End Sub



    End Class

    ReplyDelete
  7. 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

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

    ReplyDelete
  9. 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)
    stamper.Close()
    reader.Close()


    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/pdf"
    Response.BinaryWrite(oStream.ToArray())
    Response.Flush()
    Response.Close()
    Response.End()

    ReplyDelete
  10. 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

    ReplyDelete
  11. 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

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

    ReplyDelete
  13. 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 there.cheap color copies prints

    ReplyDelete

  14. 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

    ReplyDelete
  15. 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

    ReplyDelete
  16. Worried of being scammed while you Buy YouTube Views? At this site, we assist you doing so.
    how to buy youtube subscribers

    ReplyDelete
  17. The supplies that you'll need to make your custom printed t-shirts in addition to a computer and a printer, obviously will be t-shirts. gifts for companies

    ReplyDelete
  18. A debt of gratitude is in order for posting this information. I simply need to tell you that I simply look at your site and I discover it exceptionally fascinating and educational. I can hardly wait to peruse bunches of your posts. 3d printing

    ReplyDelete
  19. This online pdf merger or pdf combiner or pdf joiner will easily merge pdf files together with use of any software or any registration. pdf merger

    ReplyDelete
  20. I needed to thank you for this incredible read!! I unquestionably adored each and every piece of it. I have you bookmarked your site to look at the new stuff you post. cheap every door direct mail postcards print

    ReplyDelete
  21. With screen printers, the printer has to change the inks and stencils for creating newer designs.cheap 3d printer

    ReplyDelete
  22. This will assist you with understanding the usefulness of the product better. The following essential thing to note is the transformation quality. The product that you intend to purchase ought to have the capacity to do top notch transformation. www.altocompresspdf.com



    ReplyDelete
  23. Love how wonderfully every word is written with proper balance.
    Sell Printer

    ReplyDelete
  24. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. הדפסה על מוצרים

    ReplyDelete
  25. Promotional identifications are accessible in numerous styles and hues, sizes and materials. הדפסה על מוצרים

    ReplyDelete
  26. Printed limited time identifications are additionally accessible as catch, polish, adaptable 2D, adaptable 3D, bite the dust stepped or weaved identifications.
    הדפסה על מוצרים

    ReplyDelete
  27. A lot of web journals I see nowadays don't generally give anything that I'm keen on, however I'm most definitely inspired by this one. Recently felt that I would post and let you know.
    word search pdf

    ReplyDelete
  28. Numerous improvement organizations exist who are centered around displaying amazing web composition and advancement services at a really sensible rate. Cakephp Programming service

    ReplyDelete
  29. This is one of the most ordinarily neglected components when printers are assessed but one of the most significant interesting points before giving over your well deserved money. hp officejet pro 8610 setup

    ReplyDelete
  30. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. הדפסה על כוס תרמית

    ReplyDelete
  31. I quite like reading an article that can make people think. Also, thanks for allowing for me to comment! חולצות מודפסות לאירועים

    ReplyDelete
  32. A debt of gratitude is in order for sharing the information, keep doing awesome... I truly delighted in investigating your site. great asset... הדפסה על כוסות

    ReplyDelete
  33. Admiring the time and effort you put into your blog and detailed information you offer!.. הדפסה על מוצרים

    ReplyDelete
  34. The writer has truly done equity with his perusers and his work.
    CanvasPrints

    ReplyDelete
  35. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. Letterhead Printing

    ReplyDelete
  36. I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful. הדפסה על כוסות יין

    ReplyDelete
  37. I am typically to blogging i truly appreciate your articles. This great article has truly peaks my interest. Let me bookmark your internet site and maintain checking for brand new info. informasi percetakan

    ReplyDelete