This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Extract content from a PDF file, how?

M-Files indexer and a number of IML services can look into the text content of text based filetypes, so methods are available. I just can't find a way to extract content in VB script (the API documentation is down at the moment, that makes it difficult to look for it!). Wonder if any of you clever folks might remember a method that would allow me to get the text content of a PDF file and parse it through a regex in order to extract metadata from that content?

Thank you, Karl

  • The API documentation being down is frustrating...  Trust me. Wink

    Doing what you want via VBScript will be painful.  Have you exhausted using some of the off-the-shelf IML components for this?

  • The IML solutions can probably do this - not sure that they have fully changed from making suggestions to actually setting metadata automatically but it should happen around this time. However, the customer's current license would have to be upgraded meaning triple payment compared to their current license. In that situation it is well worth my time spending an afternoon or a day creating a script. The use case is for standardized incoming purchase orders where it is quite simple to extract the relevant data with regex, and where file length is quite limited. So once I get the text content into a variable in the script is is fairly straight forward. Obviously, it would fail if the incoming file format changes...!

    Was able to find some old postings mentioning the GetTextContentForFile. From the context it seems like it might be the way forward. Hope to get access to the documentation sometime soon to confirm this.

  • This should be the way forward Karl, yes.

  • Got it up and running - only spent a few hours :-)

  • Would you mind to share with the community? I also could give something nice in return, I worked on Slight smile

  • Beware that computers do not always read content of a PDF in the same order as humans!
    To me the document has "Dato: " and then a date in the format DD-MM-YYYY.
    However the computer gets the date first and then the text string in this particular instance.
    I discovered this by copying the content of the PDF into Notepad and then examine it there. This procedure would be helpful anytime you need to create a regex!

    'Extract metadata from file content using regex.
    'Suited for documents with a fixed content format and limited file size.
    '2022.06.27 Karl Lausten
    Option Explicit
    
    Dim objID
    set objID = ObjVer.ObjID
    Dim objVersion
    set objVersion = Vault.ObjectOperations.GetLatestObjectVersionAndProperties(objID, true)
    Dim myFiles : Set myFiles = objVersion.VersionData.Files
    Dim myFile
    Dim fileVer
    For Each myFile in myFiles
    	set fileVer = myFile.FileVer
    Next
    'This script was made to run on single file objects. The script is not prepared to handle multiple files!
    'Running it on documents with multiple files will create problems such as propertyvalues being overwritten in the proces below.
    
    
    Dim szFullText
    szFullText = Vault.ObjectFileOperations.GetTextContentForFile(ObjVer, fileVer)
    Dim szMatch
    Dim oPropertyValue
    Dim oPattern : Set oPattern = New RegExp
    
    'get Document date, this section can be repeated with different properties and patterns as needed
    Dim iPDDocDate : iPDDocDate = Vault.PropertyDefOperations.GetPropertyDefIDbyAlias("PD.DocumentDate")
    With oPattern
    	.Pattern = "(\d{2}\-\d{2}\-\d{4})(?=\s*Dato:)"
    	.IgnoreCase = True
    	.Global = True
    End With
    set szMatch = oPattern.Execute(szFullText)
    Set oPropertyValue = PropertyValues.SearchForProperty(iPDDocDate)
    oPropertyValue.TypedValue.SetValue MFDatatypeDate, szMatch.Item(0).SubMatches(0)
    Vault.ObjectPropertyOperations.SetProperty ObjVer,oPropertyValue

  • What do you guys think about creating a github repo for collecting such code snippets?

  • It's something I (at M-Files) have toyed with.  The issue is maintaining all the random ones.  If this were a community-oriented one, very explicitly saying that it's nothing to do with our official ones, then that may work.

    I do very much worry about the maintenance though.