Indesign scripting - apply PDF Tags to styles
Hello Team,
Im writing a script where, for an active indesign document, for each paragraph styles we need to add PDF - tags.
Data for parastyles to pdf tags will be supplied from csv file, dynamically.
For example, para style "1hd" will be added as "H1" - PDF tag.
Below is the code, I have tried.
// Define the CSV file path
var csvFilePath =
"\path\csvFileName.csv";
// Initialize an array to store matching style names
var doc = app.activeDocument;
var matchingStyles = [];
var mappingMessages = [];
var message = "";
var csvRow = "";
var indesignStyle = "";
var pdfTag = "";
var csvRows = "";
var paraStyles = [];
var rowCount = "";
var style;
// Create UI with button to map styles
app.scriptPreferences.userInteractionLevel =
UserInteractionLevels.interactWithAll;
var dialog = new Window("dialog", "PDF Tag testing");
var mapStylesButton = dialog.add("button", undefined, "Map styles");
mapStylesButton.onClick = function () {
csvFile = File(csvFilePath);
if (!csvFile.exists) {
alert("CSV file not found!");
return;
}
// Read CSV content
csvFile.open("r");
csvContent = csvFile.read();
csvFile.close();
// alert("CSV Content "+csvContent);
csvRows = csvContent.split("\n");
rowCount = csvRows.length;
//alert("Number of rows: " + rowCount);
// Fetch paragraph styles in the active document
paraStyles = doc.paragraphStyles.everyItem().getElements();
// alert(
// "Fetched " + paraStyles.length + " paragraph styles from the document."
// );
// Iterate through CSV rows to map styles to PDF tags
for (var i = 0; i < csvRows.length; i++) {
csvRow = csvRows[i].split(",");
indesignStyle = csvRow[0];
pdfTag = csvRow[1];
for (var j = 0; j < paraStyles.length; j++) {
if (paraStyles[j].name === indesignStyle) {
style = doc.paragraphStyles.itemByName(paraStyles[j].name);
//alert("Style "+style.name);
if (style.isValid) {
//alert(style.isValid)
style.styleExportTagMaps.add("PDF", pdfTag, "", "");
}
else {
alert(error)
}
}
}
}
};
dialog.show();I was able to get the required values to execute this line.
style.styleExportTagMaps.add("PDF", pdfTag, "", ""); But unable to update the tags when checking the Tag panel.
CSV Format :
Column1 | Column2
1hd | H1
3hd | H3
Can any helpme in this functionality.
