Dear Stanav,

Thanks for the valuable post that helped me in my projects.

I need 1 more input from your side i.e. the way out for removing the PDF file
restrictions which is encrypted with 256 bit encryption with user and/or owner password.

Really looking for your expert reply on this.

thanks n regards....
RkTech
==========================================================
Quote Originally Posted by stanav View Post
This thread was originally about extracting and merging pdf files using iTextSharp. However, as time goes by, I have added a lot more code to do other stuff and put them all together into a handy class called PdfManipulation. There are 2 classes as below (choose the one that matches the iTextSharp version you're using):

1. The original PdfManipulation.vb class is coded based on itextsharp version 4. This class is obsolete and no longer maintained.

2. The updated PdfManipulation2.vb class is for the newer itextsharp version 5. This class also contains alot more methods than the original one and I highly recommend it over the old one. I will update this class from time to time to fix bugs and/or add more functionality. Consider it's a work in progress

Please verify the version of iTextSharp you're using and download the correct class.

Below is the list of public methods in the new PdfManipulation2 class
vb.net Code:
  1. 'Remove all restrictions from a pdf file
  2.     Public Shared Function RemoveRestrictions(ByVal restrictedPdf As String, Optional ByVal password As String = Nothing, Optional ByVal saveABackup As Boolean = True) As Boolean
  3.    
  4.     'Parse text from a specified range of pdf pages    
  5.     Public Shared Function ParsePdfText(ByVal sourcePDF As String, _
  6.                                   Optional ByVal fromPageNum As Integer = 0, _
  7.                                   Optional ByVal toPageNum As Integer = 0) As String
  8.    
  9.     'Parse all text from a pdf
  10.     Public Shared Function ParseAllPdfText(ByVal sourcePDF As String) As Dictionary(Of Integer, String)
  11.    
  12.     'Page to page comparision of 2 pdf files and write the differences to a resulting text file    
  13.     Public Shared Sub ComparePdfs(ByVal pdf1 As String, ByVal pdf2 As String, _
  14.                                   ByVal resultFile As String, _
  15.                                   Optional ByVal fromPageNum As Integer = 0, _
  16.                                   Optional ByVal toPageNum As Integer = 0)
  17.    
  18.     'Extract specified pages from a pdf to create a new pdf
  19.     Public Shared Sub ExtractPdfPages(ByVal sourcePdf As String, ByVal pageNumbersToExtract As Integer(), ByVal outPdf As String)
  20.  
  21.     'Split a pdf into specified number of pdfs
  22.     Public Shared Sub SplitPdfByParts(ByVal sourcePdf As String, ByVal parts As Integer, ByVal baseNameOutPdf As String)
  23.    
  24.     'Split a pdf into multiple pdfs each containing a specified number of pages.  
  25.     Public Shared Sub SplitPdfByPages(ByVal sourcePdf As String, ByVal numOfPages As Integer, ByVal baseNameOutPdf As String)
  26.    
  27.     'Extract pages from multiple source pdfs and merge into a final pdf    
  28.     Public Shared Sub ExtractAndMergePdfPages(ByVal sourceTable As DataTable, ByVal outPdf As String)
  29.      
  30.     'Set security password on an existing pdf file  
  31.     Public Shared Sub SetSecurityPasswords(ByVal sourcePdf As String, ByVal outputPdf As String, ByVal userPassword As String, ByVal ownerPassword As String)
  32.      
  33.     'Add watermark to pdf pages using an image  
  34.     Public Shared Sub AddWatermarkImage(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkImage As String)
  35.    
  36.     'Add water mark to all pdf pages using text    
  37.     Public Shared Sub AddWatermarkText(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkText() As String, _
  38.                                        Optional ByVal watermarkFont As iTextSharp.text.pdf.BaseFont = Nothing, _
  39.                                        Optional ByVal watermarkFontSize As Single = 48, _
  40.                                        Optional ByVal watermarkFontColor As iTextSharp.text.BaseColor = Nothing, _
  41.                                        Optional ByVal watermarkFontOpacity As Single = 0.3F, _
  42.                                        Optional ByVal watermarkRotation As Single = 45.0F)
  43.  
  44.     'Merge multiple pdfs into a single one.  
  45.     Public Shared Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String, _
  46.                                          Optional ByVal authorName As String = "", _
  47.                                          Optional ByVal creatorName As String = "", _
  48.                                          Optional ByVal subject As String = "", _
  49.                                          Optional ByVal title As String = "", _
  50.                                          Optional ByVal keywords As String = "") As Boolean
  51.  
  52.     'Merge multiple pdf's into one with all bookmarks preserved
  53.     Public Shared Function MergePdfFilesWithBookmarks(ByVal sourcePdfs() As String, ByVal outputPdf As String) As Boolean
  54.        
  55.     'Add document outline (bookmarks) to a pdf
  56.     Public Shared Sub AddDocumentOutline(ByVal sourcePdf As String, ByVal outputPdf As String, ByVal outlineTable As System.Data.DataTable)
  57.      
  58.     'Extract urls from a pdf  
  59.     Public Shared Function ExtractURLs(ByVal sourcePdf As String, Optional ByVal pageNumbers() As Integer = Nothing) As System.Data.DataTable
  60.        
  61.     'Extract images from a pdf
  62.     Public Shared Function ExtractImages(ByVal sourcePdf As String) As List(Of Image)
  63.      
  64.     'Fill a form  
  65.     Public Shared Sub FillAcroForm(ByVal sourcePdf As String, ByVal fieldData As DataRow, ByVal outputPdf As String)
  66.  
  67.     Public Shared Sub FillMyForm(ByVal sourcePdf As String, ByVal fieldData As DataRow, ByVal outputPdf As String)
  68.  
  69.     'Add annotatation
  70.     Public Shared Sub AddTextAnnotation(ByVal sourcePdf As String, ByVal outputPdf As String)
  71.  
  72.     Public Shared Function GetAcroFieldData(ByVal sourcePdf As String) As Dictionary(Of String, String)
  73.        
  74.     Public Shared Function GetPdfSummary(ByVal sourcePdf As String) As DataTable
  75.        
  76.     Public Shared Function ReplacePagesWithBlank(ByVal sourcePdf As String, _
  77.                                                  ByVal pagesToReplace As List(Of Integer), _
  78.                                                  ByVal outPdf As String, _
  79.                                                  Optional ByVal templatePdf As String = "") As Boolean
  80.        
  81.     Public Shared Function InsertPages(ByVal sourcePdf As String, _
  82.                                        ByVal pagesToInsert As Dictionary(Of Integer, iTextSharp.text.pdf.PdfImportedPage), _
  83.                                        ByVal outPdf As String) As Boolean
  84.        
  85.     Public Shared Function RemovePages(ByVal sourcePdf As String, ByVal pagesToRemove As List(Of Integer), ByVal outputPdf As String) As Boolean
  86.      
  87.     'A demo on how to draw various shapes in itextsharp  
  88.     Public Shared Sub DrawShapesDemo(ByVal sourcePdf As String, ByVal outputPdf As String)
  89.          
  90.     Public Shared Sub AddImageToPage(ByVal sourcePdf As String, ByVal outputPdf As String, ByVal imgPath As String, ByVal imgLocation As Point, ByVal imgSize As Size, Optional ByVal pages() As Integer = Nothing)


Any comments are welcomed.
Happy coding
Stanav.