-
Oct 31st, 2012, 04:11 PM
#121
New Member
removing restrictions
I know this thread is a little old but I am hoping to get some help !
I need to remove the restrictions from a PDF that is automatically generated from one of our systems. The software generating the PDF also generates a random 7 character password as the owner password which changes for each file. The user password is blank.
I need to be able to change the files metadata to allow our PDF store to index the documents appropriately.
If i use the restrictions remover in pdfmaniupation2.vb ( a great bit of code btw) then it does not remove the owner password, but does change all the permissions listed to allowed (apart from page extraction)
When I use my code to change the metadata I get an exception "PdfReader not opened with owner password"
my reading of the code in pdfmanipulation2.vb is that it should create a new PDf with the contents of the old, but it should have no encryption and have no restrictions - have I got this wrong ?, can anyone advise a better way of doing this ?
thanks
-
Nov 1st, 2012, 04:02 PM
#122
Re: removing restrictions
Originally Posted by doug_ecg
....................
my reading of the code in pdfmanipulation2.vb is that it should create a new PDf with the contents of the old, but it should have no encryption and have no restrictions - have I got this wrong ?, can anyone advise a better way of doing this ?
thanks
Yes, you got the wrong idea... Removing restrictions means just that. It won't remove the passwords.
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 -
-
Nov 1st, 2012, 05:05 PM
#123
New Member
Re: removing restrictions
Originally Posted by stanav
Yes, you got the wrong idea... Removing restrictions means just that. It won't remove the passwords.
Ah indeed !
Do you by any chance know of a way to alter the metadata without using the owner password ?
With restrictions removed is it possible to copy the contents to a new PDF ?
Thanks for your assistance !
B
-
Nov 2nd, 2012, 07:15 AM
#124
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
Print it to a new PDF using a PDF print driver such as CutePDF... The printed version of the file (reads "new copy") won't have any passwords.
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 -
-
Nov 23rd, 2012, 12:23 PM
#125
New Member
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
Thanks - that does work, I was rather hoping to handle it all within my application.
Essentially I need to alter the document, add new metadata and then save it again. Rather irritatingly the company that produces the other piece of kit that generates the PDFs appears to use a random owner password ( and doesnt wish to change their software just so we can index the pdfs in ours)
Does anyone by any chance know of a way to achieve this ?, if not I guess I shall have to get cutepdf, capture the created file and then work with that.
-
Dec 12th, 2012, 03:11 PM
#126
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
Originally Posted by doug_ecg
Thanks - that does work, I was rather hoping to handle it all within my application.
Essentially I need to alter the document, add new metadata and then save it again. Rather irritatingly the company that produces the other piece of kit that generates the PDFs appears to use a random owner password ( and doesnt wish to change their software just so we can index the pdfs in ours)
Does anyone by any chance know of a way to achieve this ?, if not I guess I shall have to get cutepdf, capture the created file and then work with that.
The custom version of Pdf Writer (reads "paid version") allows you to bypass the "save as" dialog window and thus you can silently print to pdf from your app.
More info can be found from their web site:
http://www.cutepdf.com/Solutions/pdfwriter.asp
Truly, if this is for bussiness, the one time $500 price tag of the "Custom PDF Writer with programmatic access" package is justifiable.
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 -
-
Dec 20th, 2012, 03:04 PM
#127
Frenzied Member
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
Stanav ... your AddWatermarkText function requires both a source file and a destination file. Is it possible to rewrite this function to require only one file? In your current function, using the same file for both source & destination results in an error. I just want to specify a filename and the watermark text & have the function add the text to that file. Is this possible?
-
Dec 20th, 2012, 03:38 PM
#128
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
You have the source code and thus you can see how it works. If it doesn't meet your needs, feel free to modify it the way you want...
Whenever you edir a pdf file, it has to be saved as a new file due to 2 reasons: 1. pdf files are not designed to be editable. 2. The source pdf file is being opened (since you're using it to edit), and thus the file is locked. You can't delete it until the file is closed. Now that you know this, it should be fairly straight forward to modify the existing code to do what you want... That is, instead of passing in a output file path, you declare this variable locally and generate a random temporary file name for it. When done adding the watermarks, after closing the original file, you delete it and move the temp file to replace the original file.
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 -
-
Dec 19th, 2013, 02:53 PM
#129
New Member
Re: [VB.NET] Pdf Manipulation Class Using iTextSharp
Stanav,
Great help in my project. Just running into a couple of small errors and wondered if you could point me right direction in fixing them.
Error 1 Value of type '1-dimensional array of Byte' cannot be converted to 'iTextSharp.text.pdf.RandomAccessFileOrArray'. C:\VB.Net\PDFMerge_Window\PDFMerge_Console\PdfManipulation2.vb 138 65 PDFMerge_Window
Error 2 'MessageBox' is not declared. It may be inaccessible due to its protection level. C:\VB.Net\PDFMerge_Window\PDFMerge_Console\PdfManipulation2.vb 179 13 PDFMerge_Window
-
Aug 26th, 2015, 10:29 PM
#130
New Member
Re: [VB.NET] Pdf Manipulation Class Using iTextSharp
When I search tutorial on extracting PDF pages using iText, I see a links reprint this topic here:http://zh.scribd.com/doc/208204720/V...BForums#scribd
All post on it seems should be download to display.
-
Jul 21st, 2016, 01:27 AM
#131
Member
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
hi @ stanav,
Thank you so much for your wonderful work.
I have a excel file which contains: One column as name and Other column as Email. Also, I have number of pdf files in a folder.
Is it possible, to pick a pdf based on column Name and inser the email on that PDF 's first page?
It will be very helpful for me to skip from huge manual task.
Thank you so much in advance.
-
Apr 13th, 2017, 12:20 PM
#132
New Member
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
Originally Posted by stanav
Yes, I THINK it is quite possible, but it would involve much more work (obviously). I gave it a shot as seen in the code below but frankly, the method I was using only works to some extends. It only preserves the 1st level bookmarks . My approach was to export the bookmarks in the original pdf to a collection, and, select the pages to be extract from the reader, use pdfstamper to copy the original pdf (with now only the selected pages) to a new pdf. Since pdfstamper automatically preserves ALL the bookmarks from the original, I had to edit the bookmark collection to remove the unused ones. This approach should work but I don't know why it only preserves 1st level bookmarks. Some more work is needed to work that bug out, but I don't have the time right now. I will post just what I have so far.
vb.net Code:
''' <summary>
''' Extract pages from an existing pdf file to create a new pdf with bookmarks preserved
''' </summary>
''' <param name="sourcePdf">full path to sthe source pdf</param>
''' <param name="pageNumbersToExtract">an integer array containing the page number of the pages to be extracted</param>
''' <param name="outPdf">the full path to the output pdf</param>
''' <remarks></remarks>
Public Shared Sub ExtractPdfPages(ByVal sourcePdf As String, ByVal pageNumbersToExtract As Integer(), ByVal outPdf As String)
Dim raf As iTextSharp.text.pdf.RandomAccessFileOrArray = Nothing
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim outlines As System.Collections.ArrayList = Nothing
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
Dim hshTable As System.Collections.Hashtable = Nothing
Try
raf = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
reader = New iTextSharp.text.pdf.PdfReader(raf, Nothing)
outlines = iTextSharp.text.pdf.SimpleBookmark.GetBookmark(reader)
reader.SelectPages(New System.Collections.ArrayList(pageNumbersToExtract))
stamper = New iTextSharp.text.pdf.PdfStamper(reader, New IO.FileStream(outPdf, IO.FileMode.Create))
RemoveUnusedBookmarks(outlines, pageNumbersToExtract)
stamper.Outlines = outlines
stamper.Close()
reader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Shared Sub RemoveUnusedBookmarks(ByRef bookmarks As System.Collections.ArrayList, ByVal pagesToKeep() As Integer)
Dim bookmark As System.Collections.Hashtable = Nothing
Dim obj As Object = Nothing
For i As Integer = bookmarks.Count - 1 To 0 Step -1
obj = bookmarks(i)
If TypeOf obj Is System.Collections.ArrayList Then
RemoveUnusedBookmarks(DirectCast(obj, System.Collections.ArrayList), pagesToKeep)
ElseIf TypeOf obj Is System.Collections.Hashtable Then
bookmark = DirectCast(obj, System.Collections.Hashtable)
If bookmark.ContainsKey("Page") Then
Dim value As String = DirectCast(bookmark.Item("Page"), String)
If Not String.IsNullOrEmpty(value) Then
Dim parts() As String = value.Split(" "c)
If parts.Length > 0 Then
Dim pageNum As Integer = -1
If Integer.TryParse(parts(0), pageNum) Then
Dim idx As Integer = System.Array.IndexOf(pagesToKeep, pageNum)
If idx < 0 Then
bookmarks.Remove(obj)
Else
parts(0) = (idx + 1).ToString
value = String.Join(" ", parts)
bookmark.Item("Page") = value
End If
End If
End If
End If
End If
End If
Next
End Sub
Another approach I thought of was to export the original bookmarks to an XML file and edit that file. Once done, import it back to the new pdf file (which contains only the extracted pages). But like I said, I'm currently donot have a lot of free time to play with it. So I leave it to you to try
Good luck.
THANK YOU Stanav - You got me further than I had been in two days.
I tweaked it a bit in order to return actual page numbers so I can build a call for iTextSharp to recompile with only pages needed based on finding on a page.
Code:
Public Shared Function SearchTextFromPdf(ByVal sourcePdf As String, ByVal searchPhrase As String) As Integer
Dim foundList As New Integer
Dim raf As iTextSharp.text.pdf.RandomAccessFileOrArray = Nothing
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Try
raf = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
reader = New iTextSharp.text.pdf.PdfReader(raf, Nothing)
For i As Integer = 1 To reader.NumberOfPages()
Dim pageText As String = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i)
If pageText.Contains(searchPhrase) Then
Return i
MessageBox.Show(i.ToString)
Exit Function
End If
Next
reader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return 0
End Function
-
Oct 19th, 2017, 08:22 AM
#133
New Member
Re: [VB.NET] Pdf Manipulation Class Using iTextSharp
Hi , I am trying to create PDF file from the text file using Itext . i am facing some hard time to figure out how get the data without missing the format in text file.
Readline, ReadtoEnd are not providing me what i want.
Can you please guide me in achieving the task?
Thanks
-
Oct 25th, 2017, 01:19 PM
#134
Re: [VB.NET] Pdf Manipulation Class Using iTextSharp
Originally Posted by vswargam
Hi , I am trying to create PDF file from the text file using Itext . i am facing some hard time to figure out how get the data without missing the format in text file.
Readline, ReadtoEnd are not providing me what i want.
Can you please guide me in achieving the task?
Thanks
Easiest method is to use a PDF print driver, such as Cute PDF... All you have to do is to use the default app for that document type (i.e. Notepad for .txt, MS Word for .docx...) to open the file and send print command to it, specifying the pdf print driver as the printer. So basically, instead of printing out on paper, it now creates a pdf file for you.
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 -
-
Oct 26th, 2017, 09:33 AM
#135
New Member
Re: [VB.NET] Pdf Manipulation Class Using iTextSharp
Thank you very much . But I have to insert logo on top of the file along with the conversion , for the purpose itext seems the right fit.
-
Mar 9th, 2018, 06:43 AM
#136
New Member
Re: [VB.NET] Extract Pages and Split Pdf Files Using iTextSharp
Originally Posted by nbrege
Stanav ... thanks for posting these code samples. They helped me on a project that I am currently working on. I would like to request that you post another sample: I need to be able to extract specified pages from multiple documents & save them to one combined PDF. ie. take pages 3 & 7 from Doc1.pdf, 4-6 from Doc2.pdf & 1, 5 & 12 from Doc3.pdf and save them in Doc4.pdf Is this "do-able"?
Hi,
If got solution for same kindly share code..
Thanks in Advance ..
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
|