Author Topic: Randomizer Background Color Creator  (Read 4489 times)

0 Members and 1 Guest are viewing this topic.

nightslayer23

  • Newbie
  • *
  • Posts: 98
Randomizer Background Color Creator
« on: September 27, 2019, 05:40:32 AM »
Here's a weird one for you..

I'm wanting to create a bunch of background colours as a batch pdf say, with the capability of going:

How many pages? 27
Randomise create colours - be able to pick only pastel colours, or light colours or all dark, or all primary etc
Output page size: (langth) x (width)

Output file .pdf

RTT

  • Administrator
  • *****
  • Posts: 918
Re: Randomizer Background Color Creator
« Reply #1 on: September 30, 2019, 01:06:53 AM »
The next script will query the user for the number of pages, page width and height, and create a PDF (named output.pdf, in the folder of the PDF used to invoke the script) with all the pages background colorized with random colors (pastel colors, I think :-\).
Try it (it needs Acrobat Professional to work) and let me know if this is what you need.
I'm not familiar with color theory, so you have to give more details on how are these color schemes, you want as an option, are defined. This script is using the HSV color model.
Code: [Select]
try {
    var options = JSON.parse(pdfe.DialogBox(JSON.stringify({
        params: [{
            type: 'edit',
            name: 'nPages',
            label: 'Number of pages:',
            def: 10
        }, {
            type: 'edit',
            label: 'Page width [mm]:',
            name: 'PageWidth',
            def: 210
        }, {
            type: 'edit',
            label: 'Page height [mm]:',
            name: 'PageHeight',
            def: 297
        }]
    })));

    if (options.Canceled) {
        pdfe.quit();
        pdfe.echo('canceled');
    }
    options.nPages = parseIntDef(options.nPages, 10, 1, 1000);
    options.PageWidth = parseIntDef(options.PageWidth, 210, 1, 1000);
    options.PageHeight = parseIntDef(options.PageHeight, 297, 1, 1000);
} catch (err) {
    pdfe.quit();
}
pdfe.echo('Working...');
var FullPathFilename = pdfe.SelectedFiles(0).Filename;
FullPathFilename = FullPathFilename.substring(0,FullPathFilename.lastIndexOf('\\') + 1)+'Output.pdf';

var Merger = pdfe.CreateDocumentMerger();
Merger.MergeDocument('');//to circumvent a bug in versions <3.3

for (var n = 0; n < options.nPages; n++) {
    var r = Merger.MergeBlankPage(options.PageWidth, options.PageHeight);
}
Merger.EndAndSaveTo(FullPathFilename);

var app = pdfe.CreateObject("AcroExch.App");
var baseDoc = pdfe.CreateObject("AcroExch.PDDoc");
try {
    baseDoc.Open(FullPathFilename);
    try {
        var JSObject = baseDoc.getJSObject();
        JSObject.addScript('Script',

        'function hslToRgb(h, s, v) {' +
          '    var r, g, b, i, f, p, q, t;' +
          '    if (arguments.length === 1) {' +
          '        s = h.s, v = h.v, h = h.h;}' +
          '    i = Math.floor(h * 6);' +
          '    f = h * 6 - i;' +
          '    p = v * (1 - s);' +
          '    q = v * (1 - f * s);'+
          '    t = v * (1 - (1 - f) * s);' +
          '    switch (i % 6) {' +
          '        case 0: r = v, g = t, b = p; break;' +
          '        case 1: r = q, g = v, b = p; break;' +
          '        case 2: r = p, g = v, b = t; break;' +
          '        case 3: r = p, g = q, b = v; break;' +
          '        case 4: r = t, g = p, b = v; break;' +
          '        case 5: r = v, g = p, b = q; break;}' +
          'return ["RGB",r,g,b];}' +

        'function AddRandomColorBackGroundToPages(){' +
         ' for (var i=0;i<this.numPages;i++){' +
         '/*    var RandomColor=["RGB",Math.random(),Math.random(),Math.random()];*/' +
         '    var RandomColor=hslToRgb(Math.random() * 360,(25+70*Math.random())/100,(85+10*Math.random())/100);' +
         '    var cropB=this.getPageBox("Crop",i);' +
         '    this.addAnnot({' +
         '    type: "Square"' +
         '    ,page: i'+
         '    ,rect: cropB' +
         '    ,fillColor: RandomColor' +
         '    ,strokeColor: RandomColor' + '});}' +
         ' this.flattenPages();}' +         
         ' AddRandomColorBackGroundToPages();');

        JSObject.removeScript('Script');
        baseDoc.Save(1, FullPathFilename);
        var WshShell = WScript.CreateObject("WScript.Shell");
        WshShell.Run(FullPathFilename);
        pdfe.echo('Done.',0,2);       
    } catch (err) {
        pdfe.echo(err, 0xFF0000);
        pdfe.echo("");
    }
    baseDoc.Close();
} catch (err) {
    pdfe.echo(err, 0xFF0000);
    pdfe.echo("");
}
app.Exit();


/*****************************************************************/
function parseIntDef(value, def, min, max) {
    var parsed = parseInt(value, 10);
    if (isNaN(parsed)) {
        return def
    }
    return Math.min(max, Math.max(min, parsed));
}