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.:thumb:
Printable View
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.:thumb:
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)
Try this function:
vb.net Code:
''' <summary> ''' Replace specified page(s) in a pdf file with a blank page ''' </summary> ''' <param name="sourcePdf">The source pdf file to have pages replaced</param> ''' <param name="pagesToReplace">List of pages in source pdf to be replaced with blank page</param> ''' <param name="outPdf">The output pdf with pages replaced by blank pages</param> ''' <param name="templatePdf">Optional template pdf to used as replacement page</param> ''' <returns>True if successful, False if failed</returns> ''' <remarks>If the template pdf parameter is left blank, a blank template pdf will be created on the fly ''' and deleted when done</remarks> Public Shared Function ReplacePagesWithBlank(ByVal sourcePdf As String, _ ByVal pagesToReplace As List(Of Integer), _ ByVal outPdf As String, _ Optional ByVal templatePdf As String = "") As Boolean Dim result As Boolean = False Dim template As iTextSharp.text.pdf.PdfReader = Nothing Dim reader As iTextSharp.text.pdf.PdfReader = Nothing Dim doc As iTextSharp.text.Document = Nothing Dim copier As iTextSharp.text.pdf.PdfCopy = Nothing Dim deleteTemplate As Boolean = False Try reader = New iTextSharp.text.pdf.PdfReader(sourcePdf) If String.IsNullOrEmpty(templatePdf) Then templatePdf = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\BlankPageTemplate.pdf" deleteTemplate = True Dim tpdoc As New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1)) Dim tpwriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(tpdoc, New IO.FileStream(templatePdf, IO.FileMode.Create)) tpdoc.Open() tpdoc.Add(New iTextSharp.text.Paragraph(" ")) tpdoc.Close() End If template = New iTextSharp.text.pdf.PdfReader(templatePdf) doc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1)) copier = New iTextSharp.text.pdf.PdfCopy(doc, New IO.FileStream(outPdf, IO.FileMode.Create)) doc.Open() For i As Integer = 1 To reader.NumberOfPages If pagesToReplace.Contains(i) Then copier.AddPage(copier.GetImportedPage(template, 1)) Else copier.AddPage(copier.GetImportedPage(reader, i)) End If Next doc.Close() template.Close() reader.Close() result = True If deleteTemplate Then IO.File.Delete(templatePdf) End If Catch ex As Exception 'Put your own code to handle exception here Debug.Write(ex.Message) End Try Return result End Function
Exactly what i want., thanks stanav..