Results 1 to 4 of 4

Thread: [RESOLVED] Delete\Insert in PDF-Itextsharp.

  1. #1

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Resolved [RESOLVED] Delete\Insert in PDF-Itextsharp.

    I want to delete one or two PDF page and want to insert a blank pdf page using iTextSharp. is it possible using itextsharp?

    For example, i want o delete the 25,26 th page of a pdf document, and want to insert a blank page there.
    Visual Studio.net 2010
    If this post is useful, rate it


  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: Delete\Insert in PDF-Itextsharp.

    Yes, you can certainly use iTextsharp to do this. If you have a blank page pdf file to use as the replacement page, the process is very simple using pdfCopy class (If you don't have a blank pdf, you can create 1 using iTextSharp then use it)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: Delete\Insert in PDF-Itextsharp.

    Try this function:
    vb.net Code:
    1. ''' <summary>
    2.     ''' Replace specified page(s) in a pdf file with a blank page
    3.     ''' </summary>
    4.     ''' <param name="sourcePdf">The source pdf file to have pages replaced</param>
    5.     ''' <param name="pagesToReplace">List of pages in source pdf to be replaced with blank page</param>
    6.     ''' <param name="outPdf">The output pdf with pages replaced by blank pages</param>
    7.     ''' <param name="templatePdf">Optional template pdf to used as replacement page</param>
    8.     ''' <returns>True if successful, False if failed</returns>
    9.     ''' <remarks>If the template pdf parameter is left blank, a blank template pdf will be created on the fly
    10.     ''' and deleted when done</remarks>
    11.     Public Shared Function ReplacePagesWithBlank(ByVal sourcePdf As String, _
    12.                                                  ByVal pagesToReplace As List(Of Integer), _
    13.                                                  ByVal outPdf As String, _
    14.                                                  Optional ByVal templatePdf As String = "") As Boolean
    15.         Dim result As Boolean = False
    16.         Dim template As iTextSharp.text.pdf.PdfReader = Nothing
    17.         Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    18.         Dim doc As iTextSharp.text.Document = Nothing
    19.         Dim copier As iTextSharp.text.pdf.PdfCopy = Nothing
    20.         Dim deleteTemplate As Boolean = False
    21.         Try
    22.             reader = New iTextSharp.text.pdf.PdfReader(sourcePdf)
    23.             If String.IsNullOrEmpty(templatePdf) Then
    24.                 templatePdf = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\BlankPageTemplate.pdf"
    25.                 deleteTemplate = True
    26.                 Dim tpdoc As New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
    27.                 Dim tpwriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(tpdoc, New IO.FileStream(templatePdf, IO.FileMode.Create))
    28.                 tpdoc.Open()
    29.                 tpdoc.Add(New iTextSharp.text.Paragraph(" "))
    30.                 tpdoc.Close()
    31.             End If
    32.             template = New iTextSharp.text.pdf.PdfReader(templatePdf)
    33.             doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
    34.             copier = New iTextSharp.text.pdf.PdfCopy(doc, New IO.FileStream(outPdf, IO.FileMode.Create))
    35.             doc.Open()
    36.             For i As Integer = 1 To reader.NumberOfPages
    37.                 If pagesToReplace.Contains(i) Then
    38.                     copier.AddPage(copier.GetImportedPage(template, 1))
    39.                 Else
    40.                     copier.AddPage(copier.GetImportedPage(reader, i))
    41.                 End If
    42.             Next
    43.             doc.Close()
    44.             template.Close()
    45.             reader.Close()
    46.             result = True
    47.             If deleteTemplate Then
    48.                 IO.File.Delete(templatePdf)
    49.             End If
    50.         Catch ex As Exception
    51.             'Put your own code to handle exception here
    52.             Debug.Write(ex.Message)
    53.         End Try
    54.         Return result
    55.     End Function
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Re: Delete\Insert in PDF-Itextsharp.

    Exactly what i want., thanks stanav..
    Visual Studio.net 2010
    If this post is useful, rate it


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width