-
Aug 7th, 2009, 01:41 AM
#1
Thread Starter
Fanatic Member
[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
-
Aug 7th, 2009, 11:14 AM
#2
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 -
-
Aug 7th, 2009, 01:06 PM
#3
Re: Delete\Insert in PDF-Itextsharp.
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
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 -
-
Aug 10th, 2009, 03:20 AM
#4
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|