I have the below VB.Net Code I want to use to split a PDF by bookmark.
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Public Sub SplitPDFByBookmarks(sourceFile As String)
Dim reader As New PdfReader(sourceFile)
Dim bookmarks As IList(Of Dictionary(Of String, Object)) = SimpleBookmark.GetBookmark(reader)
For Each bookmark As Dictionary(Of String, Object) In bookmarks
Dim title As String = CStr(bookmark("Title"))
Dim destFile As String = Path.Combine(Path.GetDirectoryName(sourceFile), $"{title}.pdf")
Dim page As Integer = CInt(bookmark("Page"))
Dim nextPage As Integer = reader.NumberOfPages + 1
If bookmarks.IndexOf(bookmark) < bookmarks.Count - 1 Then
Dim nextBookmark As Dictionary(Of String, Object) = bookmarks(bookmarks.IndexOf(bookmark) + 1)
nextPage = CInt(nextBookmark("Page"))
End If
Dim doc As New Document(reader.GetPageSizeWithRotation(page))
Dim writer As PdfCopy = New PdfCopy(doc, New FileStream(destFile, FileMode.Create))
doc.Open()
For i As Integer = page To nextPage - 1
Dim importedPage As PdfImportedPage = writer.GetImportedPage(reader, i)
writer.AddPage(importedPage)
Next
doc.Close()
Next
reader.Close()
MsgBox("PDF file has been split based on bookmarks.")
End Sub
However when I paste it into Invoke Code I juts get many errors, anyone shed some light on what I need to do
Testing/Split PDF by Page.xaml: No compiled code to run
error BC30026: 'End Sub' expected. At line 0
error BC30024: Statement is not valid inside a method. At line 1
error BC30024: Statement is not valid inside a method. At line 2
error BC30289: Statement cannot appear within a method body. End of method assumed. At line 4
error BC30429: 'End Sub' must be preceded by a matching 'Sub'. At line 38
error BC30002: Type 'PdfReader' is not defined. At line 6
error BC30451: 'SimpleBookmark' is not declared. It may be inaccessible due to its protection level. At line 7
error BC30002: Type 'Document' is not defined. At line 21
error BC30002: Type 'PdfCopy' is not defined. At line 22
error BC30002: Type 'PdfCopy' is not defined. At line 22
error BC30002: Type 'PdfImportedPage' is not defined. At line 26
Dim reader As New PdfReader(sourceFile)
Dim bookmarks As IList(Of Dictionary(Of String, Object)) = SimpleBookmark.GetBookmark(reader)
For Each bookmark As Dictionary(Of String, Object) In bookmarks
Dim title As String = CStr(bookmark("Title"))
Dim destFile As String = Path.Combine(Path.GetDirectoryName(sourceFile), $"{title}.pdf")
Dim page As Integer = CInt(bookmark("Page"))
Dim nextPage As Integer = reader.NumberOfPages + 1
If bookmarks.IndexOf(bookmark) < bookmarks.Count - 1 Then
Dim nextBookmark As Dictionary(Of String, Object) = bookmarks(bookmarks.IndexOf(bookmark) + 1)
nextPage = CInt(nextBookmark("Page"))
End If
Dim doc As New Document(reader.GetPageSizeWithRotation(page))
Dim writer As PdfCopy = New PdfCopy(doc, New FileStream(destFile, FileMode.Create))
doc.Open()
For i As Integer = page To nextPage - 1
Dim importedPage As PdfImportedPage = writer.GetImportedPage(reader, i)
writer.AddPage(importedPage)
Next
doc.Close()
Next
reader.Close()
And click on edit arguments in invoke code and create an argument for sourceFile as below and pass value to it
I can’t find iTextSharp in the package manager. I am using VB/Windows compatibility, I have also tried iText7 but that won’t even install.
I have changed and managed to install the iText7 packages and below is the code but has errors
Imports iText.Kernel.Pdf
Imports iText.Kernel.Utils
Imports iText.Layout
Imports iText.Layout.Element
Public Sub SplitPDFByBookmarks(ByVal sourceFile As String)
Dim reader As New PdfReader(sourceFile)
Dim document As New Document(reader.GetPageSize(1))
Dim outlines As IList(Of PdfOutline) = reader.GetOutlines(False)
For Each outline As PdfOutline In outlines
Dim dest As PdfDestination = outline.GetDestination()
Dim pageNumber As Integer = dest.GetPageNumber().Value
Dim title As String = outline.GetTitle()
Dim fileName As String = title & ".pdf"
Dim writer As New PdfWriter(fileName)
Dim pdfDoc As New PdfDocument(writer)
Dim copy As New PdfCopy(pdfDoc, New System.IO.FileStream(fileName, System.IO.FileMode.Create))
pdfDoc.InitializeOutlines()
copy.SetTagged()
copy.SetPdfVersion(PdfVersion.PDF_1_7)
Dim importedPage As PdfImportedPage = copy.GetImportedPage(reader, pageNumber)
Dim pageDict As PdfDictionary = reader.GetPage(pageNumber).GetPdfObject()
Dim cropBox As PdfArray = pageDict.GetAsArray(PdfName.CropBox)
copy.AddPage(importedPage)
pdfDoc.Close()
Next
reader.Close()
End Sub
Errors:
Error ERROR Validation Error No compiled code to run
error BC30026: ‘End Sub’ expected. At line 0
error BC30024: Statement is not valid inside a method. At line 1
error BC30024: Statement is not valid inside a method. At line 2
error BC30024: Statement is not valid inside a method. At line 3
error BC30024: Statement is not valid inside a method. At line 4
error BC30289: Statement cannot appear within a method body. End of method assumed. At line 6
error BC30429: ‘End Sub’ must be preceded by a matching ‘Sub’. At line 35
error BC30456: ‘GetPageSize’ is not a member of ‘PdfReader’. At line 8
error BC30456: ‘GetOutlines’ is not a member of ‘PdfReader’. At line 9
error BC30002: Type ‘PdfDestination’ is not defined. At line 12
error BC30002: Type ‘PdfCopy’ is not defined. At line 19
error BC30002: Type ‘PdfImportedPage’ is not defined. At line 25
error BC30456: ‘GetPage’ is not a member of ‘PdfReader’. At line 26 Testing/Split PDF by Page.xaml
You can install portable.itextsharp…as shown in the above image…
And as mentioned earlier…imports and sub and end sub are not to be added in the invoke code…imports to be done in the imports menu at the bottom of studio…
And input arguments or output arfuments are to be added in the edit arguments panel of invoke code
Whatever I gave you in the previous reply…that is not throwing any errors
Please follow exactly
Whatever i gave you above I have done it in vb, windows compatibility…so it should be same for you
Cool was looking in wrong place for the package. Now I get the below.
Blockquote
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.InvalidCastException: Conversion from string “1 XYZ null null null” to type ‘Integer’ is not valid. —> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
— End of inner exception stack trace —
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(Object Value)
at UiPathCodeRunner_3248e395836149eab11cde296f707389.Run(String sourceFile)
— End of inner exception stack trace —
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object providedArgs, ParameterModifier modifiers, CultureInfo culture, String namedParams)
at UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object args)
at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode, List1 inArgs, IEnumerable1 imps, Object args)
at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)
Some value that you are using or passing inside is wrong… if you see above… at some place a string is getting converted to integer which is not possible and that is the error…
We now know what the problem is, when it’s reading the bookmark and it comes back as a string that starts with the page number but also has some text which is why it cannot convert.
I have a coder using the SharpPDF library to create a package that we will release as it seems it could be useful to many