改变PDF中所有形状和文本的颜色为文本框中输入的十六进制代码。

huangapple go评论65阅读模式
英文:

Changing the colour of all shapes and text in a PDF to the hex code entered in a text box

问题

我试图创建一个JavaScript函数,可以添加到Adobe InDesign中创建的PDF中的按钮,该函数可以将所有形状的颜色更改为输入到文本框中的十六进制颜色代码的颜色。例如,如果用户在文本框中输入'FFFFFF',然后单击按钮,它将更改所有形状为白色。

下一个要求是,对于放置在已更改颜色的任何形状之上的文本,将其颜色更改为与新颜色形成对比且仍然可读(在深色形状上是白色文本,在浅色形状上是黑色文本)。

最后的要求是,任何包含标记'[color]'的文本都将其填充颜色更改为输入的十六进制代码的颜色。在InDesign中设计此文档时,我将使用GREP样式使任何' [color]' 标记的实例具有0.1pt的文本大小,并没有填充颜色,因此用户不会在最终PDF上看到标记。不过,如果有任何方法可以在此函数中实现相同的结果,将不胜感激。

以下是我迄今为止编写的代码:

function changeColor() {
  // 获取用户输入的十六进制颜色值
  var hexValue = getField("colorField").value;

  // 将十六进制值转换为RGB值
  var redValue = parseInt(hexValue.substring(0, 2), 16);
  var greenValue = parseInt(hexValue.substring(2, 4), 16);
  var blueValue = parseInt(hexValue.substring(4, 6), 16);

  // 循环遍历文档中的所有页面项
  for (var i = 0; i < this.numPages; i++) {
    var currentPage = this.getPageNumWords(i);

    for (var j = 0; j < currentPage.length; j++) {
      var currentItem = currentPage[j];

      // 检查当前项目是否是形状对象
      if (currentItem.typename === "PathItem") {
        // 更改形状的填充颜色为用户输入的颜色
        currentItem.fillColor = [redValue / 255, greenValue / 255, blueValue / 255];

        // 检查新颜色的亮度
        var brightness = (redValue * 299 + greenValue * 587 + blueValue * 114) / 1000;

        // 循环遍历当前页面上的所有文本项
        for (var k = 0; k < currentPage.length; k++) {
          var textItem = currentPage[k];

          // 检查文本项是否在当前形状之上
          if (textItem.typename === "TextFrame" && textItem.geometricBounds[1] < currentItem.geometricBounds[1]) {
            // 根据形状颜色的亮度更改文本的颜色为黑色或白色
            if (brightness > 128) {
              textItem.fillColor = color.black;
            } else {
              textItem.fillColor = color.white;
            }
          }
        }
      } else if (currentItem.typename === "TextFrame") {
        // 检查当前文本项是否包含'[color]'标记
        var contents = currentItem.contents;
        if (contents.indexOf("[color]") !== -1) {
          // 删除标记并将填充颜色设置为用户输入的颜色
          contents = contents.replace("[color]", "");
          currentItem.contents = contents;
          currentItem.fillColor = [redValue / 255, greenValue / 255, blueValue / 255];
        }
      }
    }
  }
}

我将此代码添加为导出PDF上的'mouse up'触发器,但可惜单击按钮后什么都不会发生。

如果对此有任何帮助,将不胜感激。

英文:

I'm trying to create a JavaScript function that can be added to a button in a PDF (that has been created in Adobe inDesign) that changes the colour of all shapes to that of a hex colour code entered into a text box. For example, if the user entered 'FFFFFF' into the text box then clicked this button, it would change all the shapes to white.

The next requirement is for any text placed on top of any shapes that have had their colour changed, to have their colour changed to contrast the new colour and still be legible (white text on dark colour shapes, black text on light colour shapes).

The final addition is for any text that includes the marker '[color]' to have it's fill colour changed to that of the hex code entered. When designing this document in InDesign, I would use a GREP style to make any instance of the '[color]' marker have a text size of 0.1pt and have no fill colour, so the user wouldn't have to see the markers on the final PDF. Although, if there are any methods of achieving the same results within this function, it would be gratefully received.

This is the code I have so far:

function changeColor() {
// Get the user-entered hexadecimal color value
var hexValue = getField(&quot;colorField&quot;).value;
// Convert the hexadecimal value to RGB values
var redValue = parseInt(hexValue.substring(0, 2), 16);
var greenValue = parseInt(hexValue.substring(2, 4), 16);
var blueValue = parseInt(hexValue.substring(4, 6), 16);
// Loop through all page items in the document
for (var i = 0; i &lt; this.numPages; i++) {
var currentPage = this.getPageNumWords(i);
for (var j = 0; j &lt; currentPage.length; j++) {
var currentItem = currentPage[j];
// Check if the current item is a shape object
if (currentItem.typename === &quot;PathItem&quot;) {
// Change the fill color of the shape to the user-entered color
currentItem.fillColor = [redValue / 255, greenValue / 255, blueValue / 255];
// Check the brightness of the new color
var brightness = (redValue * 299 + greenValue * 587 + blueValue * 114) / 1000;
// Loop through all text items on the current page
for (var k = 0; k &lt; currentPage.length; k++) {
var textItem = currentPage[k];
// Check if the text item is above the current shape
if (textItem.typename === &quot;TextFrame&quot; &amp;&amp; textItem.geometricBounds[1] &lt; currentItem.geometricBounds[1]) {
// Change the color of the text to black or white depending on the brightness of the shape color
if (brightness &gt; 128) {
textItem.fillColor = color.black;
} else {
textItem.fillColor = color.white;
}
}
}
} else if (currentItem.typename === &quot;TextFrame&quot;) {
// Check if the current text item has the [color] marker
var contents = currentItem.contents;
if (contents.indexOf(&quot;[color]&quot;) !== -1) {
// Remove the marker and set the fill color to the user-entered color
contents = contents.replace(&quot;[color]&quot;, &quot;&quot;);
currentItem.contents = contents;
currentItem.fillColor = [redValue / 255, greenValue / 255, blueValue / 255];
}
}
}
}
}

I added this code as a 'mouse up' trigger on the exported PDF, but sadly nothing happens at all upon button click.

Any help with this would be very much appreciated.

答案1

得分: 1

以下是翻译好的内容:

In order to achieve this, text fields need to be the target of the colour change, instead of shapes, and the code has been simplified. Firstly, this document-level code needs to be added to remove the standard blue 'highlight' for text fields so the colour change is visible:

为了实现这一目标,需要将文本字段作为颜色更改的目标,而不是形状,并且代码已经简化。首先,需要添加以下文档级代码,以移除文本字段的标准蓝色“高亮”效果,以便颜色更改可见:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// 移除标准的浅蓝色字段颜色
app.runtimeHighlight = false;
<!-- end snippet -->

Then the following code reads and converts the hex colour code entered into the 'myHexColorField' text field and changes the fill colour of all text fields named 'myTextField'.

接下来的代码会读取并转换输入到 'myHexColorField' 文本字段的十六进制颜色代码,然后更改所有名为 'myTextField' 的文本字段的填充颜色。

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// 从十六进制文本字段 'myHexColorField' 获取十六进制值
var hexValue = getField("myHexColorField").valueAsString;

// 将十六进制值转换为 RGB 值
var redValue = parseInt(hexValue.substring(0, 2), 16);
var greenValue = parseInt(hexValue.substring(2, 4), 16);
var blueValue = parseInt(hexValue.substring(4, 6), 16);
var redRgb = redValue / 255;
var greenRgb = greenValue / 255;
var blueRgb = blueValue / 255;

// 应用颜色更改
this.getField("myTextField").fillColor = ["RGB", redRgb, greenRgb, blueRgb];

// 获取需要更改文本颜色的文本字段
var textField = getField("textColorToChange");

// 计算新颜色填充的亮度值
var lightness = redRgb + greenRgb + blueRgb;

// 根据亮度值设置文本字段的文本颜色
if (lightness > 1.5) {
  // 如果亮度大于1.5,将文本颜色设置为黑色
  textField.textColor = color.black;
} else {
  // 如果亮度小于或等于1.5,将文本颜色设置为白色
  textField.textColor = color.white;
}
<!-- end snippet -->

This code can be applied either to a button as a 'mouse up' action or to the hex code text field itself as an 'on blur' action.

此代码可以应用于按钮作为 '鼠标松开' 操作,或者应用于十六进制代码文本字段本身作为 '失焦' 操作。

Thanks to K J for the help in producing this code.

感谢 K J 提供的帮助,编写了这段代码。

英文:

In order to achieve this, text fields need to be the target of the colour change, instead of shapes, and the code has been simplified. Firstly, this document-level code needs to be added to remove the standard blue 'highlight' for text fields so the colour change is visible:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

//Remove standard light blue field colour
app.runtimeHighlight = false;

<!-- end snippet -->

Then the following code reads and converts the hex colour code entered into the 'myHexColorField' text field and changes the fill colour of all text fields named 'myTextField'.

Additionally, all text fields named 'textColorToChange' will have their text colour changed to white or black, depending on the lightness of the new fill colour.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Get the hex value from the hex text field
var hexValue = getField(&quot;myHexColorField&quot;).valueAsString;
// Convert the hexadecimal value to RGB values
var redValue = parseInt(hexValue.substring(0, 2), 16);
var greenValue = parseInt(hexValue.substring(2, 4), 16);
var blueValue = parseInt(hexValue.substring(4, 6), 16);
var redRgb = redValue / 255;
var greenRgb = greenValue / 255;
var blueRgb = blueValue / 255;
// Apply the colour change 
this.getField(&quot;myTextField&quot;).fillColor = [&quot;RGB&quot;, redRgb, greenRgb, blueRgb];
// Get the text fields that need text color change
var textField = getField(&quot;textColorToChange&quot;);
// Calculate the lightness value of the new colour fill
var lightness = redRgb + greenRgb + blueRgb;
// Set the text colour of the text field based on the lightness value
if (lightness &gt; 1.5) {
// If the lightness is greater than 1.5, set the text colour to black
textField.textColor = color.black;
} else {
// If the lightness is less than or equal to 1.5, set the text colour to white
textField.textColor = color.white;
}

<!-- end snippet -->

This code can be applied either to a button as a 'mouse up' action, or to the hex code text field itself as an 'on blur' action.

Thanks to K J for the help in producing this code.

答案2

得分: 0

如果我理解正确,您想要更改基础文档中一个单词的颜色(既不是表单字段也不是注释)。在这种情况下,您可以停止围绕其进行尝试。

Acrobat JavaScript 无法访问基本平面元素的属性,除了实际文本(PDF认为它是一个单词)及其边界框。

英文:

If I understand it correctly, you want to change the color of a word in the base document (neither form field nor comment). In this case, you can stop noodling around.

Acrobat JavaScript has no access to properties of base plane elements, with the exception of the actual text (what PDF thinks it is a word), and its bounding box.

huangapple
  • 本文由 发表于 2023年3月7日 22:44:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75663438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定