This example shows you how to parse a text representation of the current print job. The parsed values can be used to modify the configuration. An OnPreprocessText event handler is implemented in the example code below. It will count instances of all words in the document and sort them to get the top 5 words used in the document. A list of keywords will be created from this from the most used words and the configuration is changed to set the keywords document property for the created PDF document.

Please note that the OnConfigLoaded() event is used to change the setting ExtractText to yes. This makes the PDF writer create the text file representation of the current print job. The ExtractText configuration can also be specified in the runonce.ini or any other configuration file.

  1. Rem -- This script will illustrate how to extract and process the text
  2. Rem -- of the printed output.
  3.  
  4. Sub OnConfigLoaded()
  5.     Rem -- Modify the configuration to extract text from the printer
  6.   Rem -- output.
  7.     Context("Config")("extracttext") = "yes"
  8. End Sub
  9.  
  10. Sub OnPreprocessText()
  11.     Const ForReading = 1
  12.     Dim fn, f, fso, cnt
  13.     Dim d, i, word, a
  14.   Dim keywords
  15.    
  16.     Rem -- Get the name of the text file from the context object
  17.     fn = Context("TextFileName")
  18.    
  19.     Rem -- Count the pages of the text file. Each page is separated
  20.   Rem -- by a formfeed character chr(12).
  21.     Set d = CreateObject("Scripting.Dictionary")
  22.     Set fso = CreateObject("Scripting.FilesystemObject")
  23.     Set f = fso.OpenTextFile(fn, ForReading)
  24.     While Not f.AtEndOfStream
  25.         l = f.ReadLine()
  26.  
  27.         Rem -- Count the words
  28.         a = Split(l, " ")
  29.         For i = LBound(a) To UBound(a)
  30.             word = LCase(a(i))
  31.             If Len(word) > 3 Then d(word) = d(word) + 1
  32.         Next
  33.     Wend
  34.     f.Close
  35.  
  36.     Rem -- Sort the list of words
  37.     SortDictionary d, "desc"
  38.  
  39.     Rem -- Pick the first 5 words
  40.     keywords = ""
  41.     cnt = 0
  42.     For Each word In d.keys
  43.         cnt = cnt + 1
  44.         If keywords <> "" Then keywords = keywords & " "
  45.         keywords = keywords & word
  46.         If cnt = 5 Then Exit For
  47.     Next
  48.    
  49.     Rem -- Set the author value in the configuration
  50.     Context("Config")("keywords") = keywords
  51. End Sub
  52.  
  53. Rem -- Sort the dictionary values.
  54. Rem -- The direction parameter must be either "asc" or "desc".
  55. Sub SortDictionary(ByRef d, ByVal direction)
  56.     Dim retv
  57.     Dim max, k, maxkey
  58.    
  59.     direction = LCase(direction)
  60.     If direction <> "asc" And direction <> "desc" Then
  61.     Err.Raise 1000, , "Direction parameter must be " & _
  62.       "either asc or desc in call to SortDictionary."
  63.   End If
  64.    
  65.     Set retv = CreateObject("Scripting.Dictionary")
  66.     While d.Count > 0
  67.         max = Empty
  68.         maxkey = ""
  69.         For Each k In d.keys
  70.             If (d(k) > max And direction = "desc") Or _
  71.         (d(k) < max And direction = "asc") Or _
  72.         IsEmpty(max) Then
  73.  
  74.                 max = d(k)
  75.                 maxkey = k
  76.             End If
  77.         Next
  78.         retv(maxkey) = d(maxkey)
  79.         d.Remove maxkey
  80.     Wend
  81.     Set d = retv
  82. End Sub
  83.  

Download Example Files

You can download and run the example yourself. The files needed are available here. The VBS file must be placed in the macros sub folder of the PDF writer installation. You can use the MacroDir setting to change the location of the VBS files if needed.

Downloads

Attachment Size
Example file 1.15 KB

LiveZilla Live Chat Software
Top