Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Getting the image of the master page

Community Beginner ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

Hi all!

 

Could someone help me with seemingly simple problem which I am stuck with.

 

There is a 1-page document with a 1-page master spread. Technically there are 2 pages in the document: 1 "real" page and 1 page in master spread. I need to export jpgs of both. It works fine with "real" page but image of a master page cannot be exported, at least by using its name.

// get first page of first master spread
var page = doc.masterSpreads[0].pages[0];
// specify I want to export this page
app.jpegExportPreferences.pageString = page.name;
// try export
doc.exportFile(ExportFormat.JPG, File('/page.jpg'));
// oops, InDesign cannot export this page...

I know that master pages could be printed, but that is not what I am after. So, is there other way to export master page?

 

TOPICS
Scripting

Views

157

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

quote

Technically there are 2 pages in the document: 1 "real" page and 1 page in master spread.

By @michaelm90164268

 

Not quite correct.

Technically, your document has one page and it's built using the Master Page.

The MP is not a real page; it's more like a template used to generate a live document page.

 

That's why your script can't grab the MP and export it.

Maybe one of our script gurus will have a solution for you, but as far as I know, there isn't a way to export a Master Page.

 

Bevi Chagnon | Designer & Technologist for Accessible InDesign + PDFs |
Books & Classes | PubCom

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

Hi Michael, How about your script creating a new page (with master page applied) and exporting that, then undo-ing or deleting that page?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

Thanks for replies!

 

Ugh, OK. I hoped I would not have to go that route but...

 

Googling for the answer I found articles which say that is not possible (just like the reply I got here) and instead I would have to create pages/spreads and apply masters to them and export those pages, e.g. https://community.adobe.com/t5/indesign/can-you-export-masters-only-indesign-cc/m-p/9185132
So I tried this approach as well.

 

"Real life" documents could be much more complicated than what typically used to demonstrate scripts. One of the test documents I have has 1) facing pages 2) with multi-page master spreads, 3) master pages of varying transformations (size, locations, rotations, overlapping, etc.). So simply adding a spread based on a master spread does not result in a proper page arrangement in it. Instead it may have more or less pages than in the master spread and new pages are never transformed in the right way.

 

My script can now 1) add spread, 2) apply master, 3) add/remove pages as necessary, and now...

 

Then the next question is: how do I make sure that a specific page has the very same bounds (in a spread) as its master page (in master spread)?

 

Unfortunately, bounds are read-only and I cannot just copy bounds from master page to real one, but have to use transform method. That method wants to know coordinate spaces, anchorpoint and a matrix. While I can get matrix from master page, how do I figure out the other values for an arbitrary document?

 

I tried to use parent coordinate system and compute offsets manually (translations are irrespective of anchor point), but I have no idea which anchor point is assumed by transform matrix returned by master page in respect to rotation...

 

P.S. I have read the "ultimate" document on geometry in InDesign, yet it does not answer this question.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

Just throwing in crazy ideas now... What if you duplicated (or not) the page/spread (not master) and then iterated over every page item, removing any that weren't from the master spread (see page.masterPageItems)? Maybe you could also decide what to do with overridden items (check pageItem.overriddenMasterPageItem, eg. try pageItem.removeOveride(). Then export the resulting page? A long shot, trying to understand your situation. 🙂

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

Right now I am fighting with what should be a straightforward case. And it turns out ugly.

I have created a new blank document. Set non facing pages. Disabled automatic page and spread shuffle. Added a page to the first spread. Document attached.

Now here is the basic script. Here the goal is to move first page by 100 px to the right.

var page = app.documents[0].pages[0];
var pageMatrix = page.transformValuesOf(CS.PARENT_COORDINATES)[0].matrixValues;
$.writeln("old=" + pageMatrix); // writes 'old=1,0,0,1,-595.275590551,-420.944881889'

page.transform(
CS.INNER_COORDINATES,
 [0,0], // this one does not matter
 [1, 0, 0, 1, +100, 0], // here +100 is the horizontal offset
 [],    // here we tell that we need to apply this over existing matrix rather than replace it
 false);    // this tells to ignore ruler units

// here I expect to get 'new=1,0,0,1,-495.275590551,-420.944881889'
var newMatrix = page.transformValuesOf(CS.PARENT_COORDINATES)[0].matrixValues;
$.writeln("new=" + newMatrix); // this writes 'new=1,0,0,1,-545.275590551,-420.944881889'

The page is not moved properly despite that there are no scaling transforms up the parent chain whatsoever. What's going wrong here?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

Ah! It is simultaneously resizing the parent spread and reposition pages in the middle, that's why coordinate is only 1/2 of the value I am expecting.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 25, 2021 Jul 25, 2021

Copy link to clipboard

Copied

The interesting thing is InDesign allows to print master pages separete, but only all at once, not one-by-one.

Internally it has that functionality required to export them as Jpgs, it is just not exposed in the API properly.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 26, 2021 Jul 26, 2021

Copy link to clipboard

Copied

Does something like this work for you?:

 

 

 

//the name of the master spread to export
var ms = app.activeDocument.masterSpreads.itemByName("A-Master");
//add a spread with the appliedMaster after the first spread
var np = app.activeDocument.spreads.add(LocationOptions.AFTER, app.activeDocument.spreads[0], {appliedMaster:ms});
//get the page range for the export
var pn = np.pages[0].name
var thePath = Folder.desktop + "/" + ms.name + ".jpeg"
//jpeg properties. If you want to export a 2 page master spread set exportingSpread:true
app.jpegExportPreferences.properties = {antiAlias:true, exportingSpread:false, exportResolution:300, jpegExportRange:1785742674, pageString: pn.toString()}
app.activeDocument.exportFile(ExportFormat.JPG, File(thePath)); 
np.remove();

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

No it does not. Thank you!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 27, 2021 Jul 27, 2021

Copy link to clipboard

Copied

Hi @michaelm90164268 ,

 

There is no API to direct export master pages so you have do your own logic and export.

 

-Sumit

-Sumit

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 29, 2021 Jul 29, 2021

Copy link to clipboard

Copied

For someone who might need it, here is what worked for me.

function appendMasterSpreadsPages(doc)
{
    app.transformPreferences.whenScaling = WhenScalingOptions.adjustScalingPercentage;
    app.transformPreferences.transformationsAreTotals = false;
            
    var numMasterSpreads = doc.masterSpreads.count();
    for (var i = 0; i < numMasterSpreads; i++)
    {
        var masterSpread = doc.masterSpreads[i];
        var numMasterPages = masterSpread.pages.count();

        // this adds new spread based on masterSpread with a few pages (not necessarily same number of pages as in the masterSpread)
        // it will most likely be 1 page for a document with no facing pages, or 2 for a documen with facing pages
        // these pages will have no spread applied at this time
        var spread = doc.spreads.add(LocationOptions.AT_END, doc, { appliedMaster : NothingEnum.NOTHING });

        // allow moving pages into spread
        spread.allowPageShuffle = false ;

        // make sure to remove or add pages to match what we have in a spread
        if (spread.pages.count() !== numMasterPages)
        {
            // remove extra pages if any
            while (spread.pages.count() > numMasterPages)
                spread.pages[spread.pages.count()-1].remove();

            // add pages if necessary
            var numMissingPages = numMasterPages - spread.pages.count();
            while (numMissingPages-- > 0)
            {
                // new page could be added to a new spread rather than current spread, need to move it to the required spread
                var newPage = doc.pages.add({appliedMaster : NothingEnum.NOTHING});
                if (newPage.parent !== spread)
                    newPage.move(LocationOptions.AT_END, spread, BindingOptions.DEFAULT_VALUE);
            }
        }

        // with facing pages we need to make sure pages are on the correct side of the binding
        // it is only necessary if number of pages is 1 or 2. For other number of pages
        // binding side does not change the master page applied.
        if (doc.documentPreferences.facingPages)
        {
            // find first page on the right of the binding
            var p = 0;
            while (p < masterSpread.pages.count() && masterSpread.pages[p].side == PageSideOptions.LEFT_HAND)
                p++;

            // pages which are on the left of the binding must be moved to start of the spread from last to first
            // we could move from first to last but then we would need a reference to the already moved page
            // so its easier to move to the start of the spread
            var pl = p-1;
            while (pl >= 0)
            {
                spread.pages[pl].move(LocationOptions.AT_BEGINNING, spread, BindingOptions.LEFT_ALIGN);
                pl--;
            }

            // pages which are on the right of the binding must be moved to end of the spread from left to right
            pl = p;
            while (pl < spread.pages.count())
            {
                spread.pages[pl].move(LocationOptions.AT_END, spread, BindingOptions.RIGHT_ALIGN);
                pl++;
            }
        }

        // now apply master to pages and assign labels
        for (var j = 0; j < masterSpread.pages.count(); j++)
        {
            var page = spread.pages[j];
            var master= masterSpread.pages[j];
            
            // this will apply master including its size to page
            page.appliedMaster = masterSpread;
            
            // label the master page so that it knows which page represents it
            master.label = page.name;
            
            // SxHxR: apply scale, shear and rotation transforms from the mastetPage, do not apply translation
            // these we can simply replace in target matrix, however translate needs special treatment later
            var masterMatrix = master.transformValuesOf(CS.SPREAD_COORDINATES)[0].matrixValues;
            page.transform(
                CS.INNER_COORDINATES,
                [[0,0], CS.INNER_COORDINATES],
                masterMatrix,
                [MatrixContent.ROTATION_VALUE, MatrixContent.SCALE_VALUES, MatrixContent.SHEAR_VALUE],
                false);

            // T: apply translate transform, except for the first page.
            // the trick here is that we cannot position page relative to the spread because
            // InDesign recalculates coordinates for the spread and all the pages in it after every change
            // instead use bounds to position pages relative to each other
            // first page remains where it is, used as a refrerence for the 2nd page, whereas 2nd page is a reference to 3rd, etc.
            // I still do not quite understand why bounds work whereas translation values in the matrix do not
            var refIndex = j - 1;
            if (refIndex >= 0)
            {
                var refPage = spread.pages[refIndex];
                var refMaster = masterSpread.pages[refIndex];
                var dx = refPage.bounds[1] - page.bounds[1] + master.bounds[1] - refMaster.bounds[1];
                var dy = refPage.bounds[0] - page.bounds[0] + master.bounds[0] - refMaster.bounds[0];

                page.transform(
                    CS.PASTEBOARD_COORDINATES,
                    [[0,0], CS.INNER_COORDINATES],
                    [1, 0, 0, 1,  dx, dy],
                    [],
                    false);
            }
        }
    }
}

It works on all the documents I have, but may not work for something I have not seen just yet. So if you are good with InDesign geometery and see how it could be fixed or improved, please update this solution for the benefit of everyone.

 

Thanks!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 29, 2021 Jul 29, 2021

Copy link to clipboard

Copied

Hi Michael,

how about setting all objects on page one to invisible and exporting page one to JPEG ?

Just an idea:

var doc = app.documents[0];

var appliedMasterName = doc.pages[0].appliedMaster.name;
var exportFile = File( Folder.desktop + "/" + appliedMasterName +"-"+ Date.now() + ".jpg" );

var oldProperties = app.jpegExportPreferences.properties ;


app.jpegExportPreferences.properties = 
{
	antiAlias : true , 
	embedColorProfile : true ,
	exportingSpread : false ,
	jpegColorSpace : JpegColorSpaceEnum.CMYK ,
	exportResolution : 300 , 
	jpegExportRange : ExportRangeOrAllPages.EXPORT_RANGE , 
	jpegQuality : JPEGOptionsQuality.MAXIMUM ,
	jpegRenderingStyle : JPEGOptionsFormat.BASELINE_ENCODING ,
	pageString: "+1" ,
	simulateOverprint : true ,
	useDocumentBleeds : false
};

doc.spreads[0].pageItems.everyItem().visible = false;

doc.exportFile
(
	ExportFormat.JPG , 
	exportFile
);

doc.spreads[0].pageItems.everyItem().visible = true;
app.jpegExportPreferences.properties = oldProperties;

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 29, 2021 Jul 29, 2021

Copy link to clipboard

Copied

LATEST

It will not handle case when there is a master page which was not applied to any real page in the document.

This problem is solved for now, I made a few changes to the script to address unconvenient behavior of InDesign when it may create 1 or 2 new spreads, and may change existing spreads when I use the script to append a single new spread to the document. So far it works fine.

I consider this question closed. Thanks everyone for suggestions!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines