I have a (seemingly) easy task: Replace strings in an pdf file.
I found the nice solution itext7, but even after reading through the available documentation, I cannot adapt the code to my specific file structure. My sub so far, mostly adapted from that example: https://kb.itextpdf.com/home/it7kb/examples/replacing-pdf-objects It works for the example pdf file from the itext7 team, but not for my specific pdf. I already found out, why, but I don't understand how to adjust the code now.
Imports iText.Commons.Utils
Imports iText.Kernel.Pdf
Sub PDFreplaceStrings(Filename As String, Placeholders() As String, Replacements() As String)
Dim strTempFileName As String = My.Application.Info.DirectoryPath & "\Temp.pdf"
Dim pdfDoc As PdfDocument
Dim pdfCurPage As PdfPage
Dim pdfDict As PdfDictionary
Dim pdfObj As PdfObject
Dim pdfStr As PdfStream = Nothing
Dim pdfData() As Byte
Dim strReplacement As String
'Check parameters:
If My.Computer.FileSystem.FileExists(FileName) = False or Placeholders.Length = 0 Or Replacements.Length = 0 or Placeholders.Length <> Replacements.Length Then exit sub
'The main part:
My.Computer.FileSystem.MoveFile(FileName, strTempFileName, True)
pdfDoc = New PdfDocument(New PdfReader(strTempFileName), New PdfWriter(FileName))
pdfCurPage = pdfDoc.GetFirstPage
pdfDict = pdfCurPage.GetPdfObject
pdfObj = pdfDict.Get(PdfName.Contents)
If TypeOf pdfObj Is PdfStream Then 'the ideal case
pdfStr = CType(pdfObj, PdfStream)
ElseIf TypeOf pdfObj Is PdfArray Then 'my case
'How do I get the stream now?
End If
If pdfStr IsNot Nothing Then
pdfData = pdfStr.GetBytes 'this is the part from the original code which doesn't work for my file, since my pdfObj is of type PdfArray instead of pdfStream
For i As Integer = 0 To Placeholders.Length - 1 'Since I want to replace multiple strings, I iterate now over an array of placeholders and replacement strings
strReplacement = JavaUtil.GetStringForBytes(pdfData).Replace(Placeholders(i), Replacements(i))
pdfStr.SetData((System.Text.Encoding.UTF8.GetBytes(strReplacement)))
Next
End If
'Wrap-up:
pdfDoc.Close()
My.Computer.FileSystem.DeleteFile(strTempFileName)
end sub
Sub Test
dim ar1() ar2() as string
ar1 = {"#PPF#"}
ar2 = {"30"}
PDFreplaceStrings("BLP_Report-Test.pdf",ar1,ar2)
End Sub
My key problem is: I know that in case of my example file, the content of my page is of type PdfArray instead of PdfStream and that is why the original example code doesn't work on my file. But I don't understand how to adapt it from here. I have zero knowledge in Java, so it's really difficult for me to prescind from the itext7 documentation. Here is my specific example file with the placeholders: https://shimbox.shimadzu.eu/download/c9007175-15ce-42aa-bc06-ccd15b1499f0
Can you please give me a hint how to proceed?