Attribute VB_Name = "dumper"
'
' $Id: dumper.bas,v 1.7 2007/06/06 15:03:21 stm Exp $
'
'  pCOS sample application for dumping PDF information
'
' Important: the PDFlib pCOM COM component must be referenced
' via "Project", "References..."!
'
Option Explicit

Sub Main()
    Dim p As pCOS_com.pCOS
    Dim docoptlist As String
        docoptlist = "requiredmode=minimum"
    Dim globaloptlist As String
        globaloptlist = ""
    Dim count, pcosmode As Long
    Dim i, doc As Long
    Dim objtype As String
    Dim yesno As String
    Dim plainmetadata As Boolean
    Dim Result As String
        Result = ""
    Dim Args

    On Error GoTo ArgErrExit
    Dim filename As String
        filename = "../../../doc/pCOS-license.pdf"

    Args = Split(Command$)

    If filename = "" And UBound(Args) <> 0 Then
            GoTo ArgErrExit
    End If
    If UBound(Args) = 0 Then
        filename = Args(0)
    End If

    On Error GoTo ErrExit


    Set p = New pCOS_com.pCOS

    p.set_option globaloptlist

    doc = p.open_document(filename, docoptlist)
    If (doc = -1) Then
        MsgBox "ERROR " & p.get_errmsg
        End
    End If

    '* --------- general information (always available) */

    pcosmode = p.get_number(doc, "pcosmode")

    Result = Result & "File name: " & p.get_string(doc, "filename") & vbCr

    Result = Result & "PDF version: " & _
        Fix(p.get_number(doc, "pdfversion") / 10) & "." & _
        (p.get_number(doc, "pdfversion") - 10) & vbCr

    Result = Result & "Encryption: " & _
            p.get_string(doc, "encrypt/description") & vbCr

    If p.get_number(doc, "encrypt/master") <> 0 Then
        yesno = "yes"
    Else
        yesno = "no"
    End If
    Result = Result & "Master pw: " & yesno & vbCr

    If p.get_number(doc, "encrypt/user") <> 0 Then
        yesno = "yes"
    Else
        yesno = "no"
    End If
    Result = Result & "User pw: " & yesno & vbCr

    If p.get_number(doc, "encrypt/nocopy") <> 0 Then
        yesno = "no"
    Else
        yesno = "yes"
    End If
    Result = Result & "Text copying: " & yesno & vbCr

    If p.get_number(doc, "linearized") <> 0 Then
        yesno = "yes"
    Else
        yesno = "no"
    End If
    Result = Result & "Linearized: " & yesno & vbCr

    Result = Result & vbCr

    If pcosmode = 0 Then
        Result = Result & "Minimum mode: no more information available"
        MsgBox Result
        End
    End If

    '* --------- more details (requires at least user password) */

    Result = Result & "PDF/X status: " & p.get_string(doc, "pdfx") & vbCr
    Result = Result & "PDF/A status: " & p.get_string(doc, "pdfa") & vbCr

    If (p.get_number(doc, "tagged") <> 0) Then
        yesno = "yes"
    Else
        yesno = "no"
    End If
    Result = Result & "Tagged PDF: " & yesno & vbCr

    Result = Result & "No. of pages: " & p.get_number(doc, "length:pages") & vbCr

    Result = Result & "Page 1 size: width=" & _
        p.get_number(doc, "pages[0]/width") & ", height=" & _
        p.get_number(doc, "pages[0]/height") & vbCr

    count = p.get_number(doc, "length:fonts")
    Result = Result & "No. of fonts: " & count & vbCr

    For i = 0 To count - 1 Step 1
        Dim fonts

        fonts = "fonts[" & i & "]/embedded"
        If (p.get_number(doc, fonts) <> 0) Then
            Result = Result & "embedded "
        Else
            Result = Result & "unembedded "
        End If

        fonts = "fonts[" & i & "]/type"
        Result = Result & p.get_string(doc, fonts) & " font "
        fonts = "fonts[" & i & "]/name"
        Result = Result & p.get_string(doc, fonts) & vbCr
    Next

    Result = Result & vbCr

    plainmetadata = p.get_number(doc, "encrypt/plainmetadata") <> 0

    If pcosmode = 1 And Not plainmetadata And _
        p.get_number(doc, "encrypt/nocopy") <> 0 Then
        Result = Result & _
            "Restricted mode: no more information available"
        MsgBox Result
        End
    End If

    '* ----- document info keys and XMP metadata (requires master pw) */

    count = p.get_number(doc, "length:/Info")

    For i = 0 To count - 1 Step 1
        Dim info As String
        Dim key As String

        info = "type:/Info[" & i & "]"
        objtype = p.get_string(doc, info)

        info = "/Info[" & i & "].key"
        key = p.get_string(doc, info)

        Result = Result & key & ": "

        '* Info entries can be stored as string or name objects */
        If objtype = "name" Or objtype = "string" Then
            info = "/Info[" & i & "]"
            Result = Result & "'" & p.get_string(doc, info) & "'" & vbCr
        Else
            info = "type:/Info[" & i & "]"
            Result = Result & "(" & p.get_string(doc, info) & " object)" & vbCr
        End If
    Next

    Result = Result & vbCr
    Result = Result & "XMP meta data: "

    objtype = p.get_string(doc, "type:/Root/Metadata")
    If (objtype = "stream") Then
        Dim contents

        contents = p.get_stream(doc, "", "/Root/Metadata")
        Result = Result & Len(contents) & " bytes "
        Result = Result & vbCr
    Else
        Result = Result & "not present" & vbCr
    End If

    MsgBox Result

    p.close_document doc
    End

ErrExit:
    MsgBox "pCOS exception " & p.get_apiname & ": " & Err.Description
    Set p = Nothing
    End
ArgErrExit:
    MsgBox "usage: dumper <filename>"
    End
End Sub
