Copy link to clipboard
Copied
I need help with sorting of a created portfolio in Visual Basic 6 with JavaScript and Acrobat Pro DC. I can create successfully a portfolio with some entries. I want to sort these entries by a specific sort order. I tried this by entering a sorting code into the description field as a workaround. This works. But only if I manually change the sort order at the portfolio:
Dim AcrobatApp As Object 'Acrobat.CAcroApp
Dim AcrobatDoc As Object 'Acrobat.CAcroPDDoc
Dim jso As Object
Dim Collection As Object
Set AcrobatApp = CreateObject("AcroExch.App")
Set AcrobatDoc = CreateObject("AcroExch.PDDoc")
AcrobatApp.Hide
Set jso = AcrobatDoc.GetJSObject
Set Collection = jso.App.newcollection()
Collection.addField "index", "0", "S", 3
Collection.importdataobject("readme", "/c/Test/readme")
jso.ResetAttachmentDescription Collection, "readme")
Set varAttachment = Collection.getDataObject("readme")
Debug.Print varAttachment.Name & ", " & varAttachment.path & ", " & varAttachment.Size & ", " & varAttachment.Description
varAttachment.Description = ccString("0001")
Set varAttachment = Nothing
Collection.SaveAs ("C:\Portfolio.pdf")
How can I define the default sort order, because the default order is "Name" and how can I create a new field e.g. Index and change the sort order to this new index field? The Code "Collection.addField "index", "0", "S", 3" doesn't work.
The screenshot shows what I want to do programatically with VB6 and Javascript:
Hi Parker,
thank you for your tips. You are completly right. I tried it out with the console window. Now I have the solution. Maybe somebody need this to:
My main problem were:
You have to use some stuipid:
Collection.Collection.addField
and that you have to deactivate first of all the sorting of all fields, because otherwise the new field gets automaticalle sort=2
Visual Basic 6 Code:
Set Collection = jso.App.newcollection()
' add new field from type numeric, at the end position 7
Set objCollection
...
Copy link to clipboard
Copied
Okay, I can create new columns with:
Dim a As Variant
Set a = Collection.Collection.addField("index", "index", "N", 0)
Debug.Print a.Name & "," & a.Text & "," & a.Type & "," & a.Order & "," & a.Visible
But I cant change the sort criteria from Name to another field, because I can't change or read the collectionField property "sort".
Here is the text from the api reference:
The value of sort specifies how this field contributes to the ordering of files in the collection.
If the value is 0, this field is not used to sort the files. If the absolute value is 1, this field is the primary sort field. If the
absolute value is 2, the field is the secondary sort field (used to break ties on the primary sort field), and so on.
Additionally, the sign of value indicates the direction in which the files should be sorted. Positive values indicate that the
files are sorted in ascending order, and negative values indicate that files are sorted in descending order.
Type
integer
Access
R/W
Example
To illustrate the sorting scheme, consider a simple set of 4 fields that describe an email inbox: From, Subject, To, and
Received. Each file in the collection represents a single email message. Set the sort order for each of these fields as
indicated below:
From: 0
To: 0
Subject: 1
Received: -2
The above sort order indicates that the primary sort field is "Subject" (the absolute value of the field is 1) and that files
should be sorted in ascending order (the sign is positive). If two files have the same subject, the secondary sort field is
consulted. In this case, the secondary sort field is "Received" (the absolute value of the field is 2) and it is specified to
sort in descending order. The files are sorted by Subject in ascending order. Any files that have identical Subjects will
be sorted in a subgroup by their Received field, but in descending order. The other fields do not contribute to the sort,
since they are 0.
Copy link to clipboard
Copied
What you are doing is actually an Acrobat JavaScript task. You would be better served to develop it in Acrobat using actual JavaScript. There are a couple of very good reasons this method will be helpful. First, it'll go faster. Using the Console window you can test out code and get instant results, including JavaScript specific errors. Second, your working with JavaScript objects. VBA does not handle JS object well, especially ones that are connected to Acrobat. There are incompatibilities you will not see, or be able to do anything about, from VBA. The best solution for this type of programming is to create a folder level JavaScript function for performing the Acrobat JavaScript tasks, then call it from VBA.
Copy link to clipboard
Copied
s
Copy link to clipboard
Copied
Hi Parker,
thank you for your tips. You are completly right. I tried it out with the console window. Now I have the solution. Maybe somebody need this to:
My main problem were:
You have to use some stuipid:
Collection.Collection.addField
and that you have to deactivate first of all the sorting of all fields, because otherwise the new field gets automaticalle sort=2
Visual Basic 6 Code:
Set Collection = jso.App.newcollection()
' add new field from type numeric, at the end position 7
Set objCollectionField = Collection.Collection.addField("index", "Sortierung", "N", 7)
' you have to deactivate the sort for all fields, because otherwise VB6 can't access at it and to prevent acrobat to automatically switch the sort from 1 to 2
jso.DeactivateSort Collection
' sort for the new field index
jso.SortCollectionField Collection, "index"
' to add documents to your new portfolio, use the importdataobject method
If Collection.importdataobject(/c/Test/readme)= False Then
MsgBox("Error")
End If
Set varAttachment = Collection.getDataObject("readme")
varAttachment.setFieldValue "index", lngSortCode
lngSortCode = lngSortCode + 1
' now add more documents to portfolio
Collection.SaveAs (v_strPortfolioName)
The folder level JavaScript functions:
/******************************************************************************
* Purpose: Die Routine zeigt alle CollectionFields einer PDF collection
* (auch PDF package und PDF portfolio genannt) in der Console an.
* Dies funktioniert leider nicht über Visual Basic 6.
* Standardmäßig sind das folgende Felder:
* FileName/Name, sort: 1
* Description/Beschreibung, sort: null
* ModDate/Geändert am, sort: null
* Size/Größe, sort: null
* CreationDate/Erstellt am, sort: null
* adobe:Order/Reihenfolge, sort: null
* AFRelationship/Beziehung, sort: null
* CompressedSize/Komprimierte Größe, sort: null
*
* Inputs: n.a.
*
* Assumes: collection aktuelles Portfolio
*
* Returns: n.a.
*
* Effects: Dient nur zu Testzwecken um alle verfügbaren Namen zu ermitteln.
*
* Date Created: 12.08.2020 12:27 Uhr
* Creator: dacore Datenbanksysteme AG, A. Hofmann
* Notes: Amenti V1.1.19 20200812
* 18054-AU01+SW02 PDF Portfolio und digitale Unterschrift
******************************************************************************/
function ShowCollectionFields(Collection)
{
try {
//Konsole einblenden
console.show();
for (var i in Collection.collection.fields)
console.println(Collection.collection.fields[i].name + '/' + Collection.collection.fields[i].text + ', sort: ' + Collection.collection.fields[i].sort + ', order: ' + Collection.collection.fields[i].order);
}
catch (e)
{
console.println("An error occurred while showing the collectionFields of the portfolio: " + e + '(Routine: ShowCollectionFields)');
}
}
/******************************************************************************
* Purpose: Die Routine deaktiviert die Sortierreihenfolge, damit später
* das Portfolio nach genau einem Feld sortiert werden kann.
* Dies funktioniert leider nicht über Visual Basic 6, weil
* wenn kein Wert in der Eigentschaft sort festgelegt wurde,
* für VisualBasic vermutlich kein Objekt vorhanden ist.
*
* Inputs: n.a.
*
* Assumes: collection aktuelles Portfolio
*
* Returns: n.a.
*
* Effects: Portfolio ist unsortiert, damit später nach einem Feld sortiert werden kann
*
* Date Created: 12.08.2020 12:27 Uhr
* Creator: dacore Datenbanksysteme AG, A. Hofmann
* Notes: Amenti V1.1.19 20200812
* 18054-AU01+SW02 PDF Portfolio und digitale Unterschrift
******************************************************************************/
function DeactivateSort(Collection)
{
try {
for (var i in Collection.collection.fields)
Collection.collection.fields[i].sort=0;
}
catch (e)
{
console.println("An error occurred while deactivating the sort order of the portfolio: " + e + '(Routine: DeactivateSort)');
}
}
function SortCollectionField(Collection, v_strFieldName)
{
try {
//Feld ermitteln
var f = Collection.collection.getField(v_strFieldName);
if (f == null)
{
app.alert('CollectionField "' + v_strFieldName + '" nicht gefunden. Sortierung kann nicht erfolgen!');
}
else
{
f.sort=1;
}
}
catch (e)
{
console.println("An error occurred while sorting the portfolio with field " + v_strFieldName + ": " + e + '(Routine: DeactivateSort)');
}
}
Here is the result:
Copy link to clipboard
Copied
Excellent Script!!