Inserting XRef in document fails for book component
Dear all,
Script part InsertXRef2 works correctly when starting in a document (case a). It does not insert the cross reference if the document is a book component (case b).
In case a the footnote references and the endnotes are in the same document (at end).
In case b the footnote references are in one book component and the endnotes are collected in a new book component.
When calling InsertXRef2 the arguments are:
a: oDoc = app.ActiveDoc;
b: oDoc = SimpleOpen (goFno.aoBkDocsCollected[jDoc].Name);
a,b: oTR = new TextRange (oTL1, oTL2); // the footnote reference
a,b: sXRefFmt = "zenref-endnote-reference" containing "<hypertext><super>\ <$paranumonly>\ "
a: oTgtDoc = oDoc;
b: oTgtDoc = SimpleNewDoc (oTplDoc.Name, 0);
a,b: oTgtTL = goFno.aoEndNoteTL
; // Begin of endnote in oTgtDoc
I get this output for the first footnote reference to be replaced by a cross reference to the corresponding endnote:
oTR...offset: 214, 215
oXRef.TextRange...offset: 214, 215
The latter is strange since the XRef should have a length of 3 ($paranumonly is figure 1).
Terminating the script shows no inserted XRef and inspecting the current 'selection' yields:
app.ActiveDoc...offset: 216, 216
Which is even more strange, because this is 2 characters behind the deleted footnote ref (which was a superscripted figure 1).
→ I don't have the faintest idea what's going on here.
→ I also have no idea how to strip the situation down to a singe test script.
→ Hence I have ZIPped the full script and the book files at https://daube.ch/zz_tests/ConvertFootnotes.zip
Test 1: have one of the book files active before you start the script and run it through.
The cross reference will be there
Test 2: Have the book active before yu start the script.
The cross references are not inserted, albeit all footnote refs with the footnotes are deleted.
function InsertXRef2 (oDoc, oTR, sXRefFmt, oTgtDoc, oTgtTL) { //=== Replace TR by XRef ================
// Arguments oDoc: Document receiving the XRef
// oTR: Text Range which shall be replaced by the cross reference
// sXrefFmt: Cross reference format name from the catalogue
// oTgtDoc: Document in which the endnotes are (same as oDoc if just document processing)
// This document gets the cross ref marker
// oTgtTL: Where the XRef marker shall be inserted (beg of endnote ¶)
// Comment FDK reference: "XRefFile" The filename of the file containing the crossreference source.
// If the cross-reference source is in the same document as the cross reference,
// the filename is an empty string ("").
// Reference https://forums.adobe.com/thread/2088696
var oHTR, oMarker, oXRef, sXRefID, oEnPgf, oTL;
// --- Prepare the required information ------------
sXRefID = oTgtTL.obj.id + ":" + oTgtTL.obj.Unique; // take properties from the oTgtObj
// --- Create the Cross reference Marker -----------
app.ActiveDoc = oTgtDoc;
oMarker = oTgtDoc.NewAnchoredMarker(oTgtTL); // Marker to be inserted in target object
oMarker.MarkerTypeId = oTgtDoc.GetNamedMarkerType ("Cross-Ref"); //Make it a Cross-Ref marker
oMarker.MarkerText = sXRefID; // Marker text needs to be unique
// --- Create the Cross reference ------------------
app.ActiveDoc = oDoc;
oDoc.TextSelection = oTR;
oDoc.DeleteText (oTR); // delete the footnote
oTL = oTL = oTR.beg; // identicak to new TextLoc (oTR.beg.obj, oTR.beg.offset);
oXRef = oDoc.NewAnchoredFormattedXRef(sXRefFmt, oTL); // Insert a new xref object
$.writeln("oTR...offset: " + oTR.beg.offset + ", " + oTR.end.offset +
"\noXRef.TextRange...offset: " + oXRef.TextRange.beg.offset + ", " + oXRef.TextRange.end.offset);
$.bp(true);
oXRef.XRefSrcIsElem = false; // Required to make it an unstructured xref
oXRef.XRefFile = oTgtDoc.Name; // OK ???
oXRef.XRefSrcText = oMarker.MarkerText;
oDoc.UpdateXRef(oDoc, oXRef); //Update the new xref.
} //--- end InsertXRef2




