Creating Documents from Streams

Creating Documents from Streams
 1ot = impact.gui.output_toolbox
 2db = impact.active_database
 3
 4docs = db.documents
 5
 6
 7def describe_doc(doc, method):
 8    if not doc.isNone():
 9        ot.add("Document '" + doc.name + "' successfully created with " + str(method))
10        ot.add("description: " + str(doc.description))
11        ot.add("Checkin-Comment: " + str(doc.comment))
12        ot.add("Added: " + str(doc.added_date_time) + " by " + str(doc.added_by.login_id))
13        ot.add("version: " + str(doc.version.version_as_string))
14        ot.add("Size: " + str(doc.size) + " bytes")
15    else:
16        ot.add("Unable to create new document with " + str(method))
17
18def read_stream(file):
19
20    # stream = CreateObject("ADODB.Stream")
21
22    stream.open()
23    stream.type = 1  # ad_type_binary
24    stream.load_from_file(file)
25
26    read_stream = stream
27
28
29ot.clear()
30
31docs = db.documents
32
33if docs is None:
34    ot.add("No document support in the database")
35elif db.settings.other_documents:
36    ot.add("Other document support is enabled in this database")
37    ot.add("Major and minor versions support: " + str(db.settings.document_settings.supports_major_and_minor_versions))
38    ot.add(str(docs.total_count) + " documents currently in the database")
39
40    # using deprecated AddFromStream
41    doc_stream = read_stream(r"C:\\Documents\\ExampleDocument1.docx")
42    doc = docs.add_from_stream(doc_stream, "ExampleDocument1.docx", 0, "My First Document")
43
44    describe_doc(doc, "AddFromStream")
45
46    # using AddFromStream2 without values
47    doc_stream = read_stream(r"C:\\Documents\\ExampleDocument2.docx")
48    doc2 = docs.add_from_stream2(doc_stream, "ExampleDocument2.docx", 0, None, ipDocumentVersion.dvMajor, "My Second Document")
49
50    describe_doc(doc2, "AddFromStream2 (no values)")
51
52    # using AddFromStream2 with values
53    # first determine document type and columns for file
54    doc_type = db.settings.document_settings.mime_types.find_type_for_extension(r"C:\\Documents\\ExampleDocument3.docx")
55
56    ot.add("Document Type: " + str(doc_type.key))
57
58    # create some values and populate using ipDocumentKnownColumn values
59    columns = doc_type.columns
60    values = doc_type.create_column_values()
61
62    values.save(columns.known_item(ipDocumentKnownColumn.dkcName).name, "ExampleDocument3")
63    values.save(columns.known_item(ipDocumentKnownColumn.dkcDescription).name, "My Third Document")
64
65    doc_stream = read_stream(r"C:\\Documents\\ExampleDocument3.docx")
66    doc3 = docs.add_from_stream2(doc_stream, "ExampleDocument3.docx", 512, values, ipDocumentVersion.dvMinor, "My Third Document")
67
68    describe_doc(doc3, "AddFromStream2 (with values)")
69
70    # must reload IDocuments
71    docs2 = db.documents
72    ot.add(str(docs2.total_count) + " documents currently in the database")
73
74else:
75    ot.add("No other document support in the database")
76