C#/JavaScript How to Set Primary Text Frame?
Hi,
I'm trying to work with InDesign scripting API with C# for my work. I'm new to both the InDesign scripting API, and using InDesign as well.
I'm trying to construct a master spread, with text frames in such a way that the text frame on the left page of the spready would flow into the text frame on the right page.
I've read there is the Primary Text Frame option for the master spread, but I can't seem to figure out how to use it.
Is there anyone who could show me an example scripting code to set the primary text frame for a master spread/page?
Here's the code I have so far.
The issue I have is in line 53, where when I try to run it, it gives me the very generic error "Invalid object for this request.", which leads me to believe I'm doing it wrong.
public static byte[] CreatePageBasedOnCreatedMasterSpread(string savePath)
{
Application application = ActivateInDesign();
// Create new document
application.Documents.Add(true, application.DocumentPresets.FirstItem());
// Get active document and change some settings
Document document = application.ActiveDocument;
document.DocumentPreferences.FacingPages = true;
document.DocumentPreferences.PageWidth = 210;
document.DocumentPreferences.PageHeight = 297;
document.DocumentPreferences.CreatePrimaryTextFrame = true;
document.TextPreferences.SmartTextReflow = true;
MasterSpread masterSpread = document.MasterSpreads.Add();
masterSpread.BaseName = "CustomMasterSpread";
masterSpread.NamePrefix = "AC";
//Set the document's ruler origin to page origin. This is very important
//--if you don't do this, getting objects to the correct position on the
//page is much more difficult.
document.ViewPreferences.RulerOrigin = idRulerOrigin.idPageOrigin;
Page leftPage = masterSpread.Pages.FirstItem();
Page rightPage = masterSpread.Pages.LastItem();
leftPage.MarginPreferences.Top = rightPage.MarginPreferences.Top = 10;
leftPage.MarginPreferences.Bottom = rightPage.MarginPreferences.Bottom = 10;
leftPage.MarginPreferences.Left = rightPage.MarginPreferences.Left = 30;
leftPage.MarginPreferences.Right = rightPage.MarginPreferences.Right = 20;
// Create a text frame. Do not add text to this, as this is a master spread text frame.
// Link their flow together
TextFrame primaryFirstPageTextFrame = leftPage.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, leftPage);
TextFrame primarySecondPageTextFrame = rightPage.TextFrames.Add(document.Layers.FirstItem(), idLocationOptions.idUnknown, rightPage);
primaryFirstPageTextFrame.NextTextFrame = primarySecondPageTextFrame;
//Set primary text frame of master spread to be the two linked textframes
primaryFirstPageTextFrame.GeometricBounds = GetPageBoundsWithMargin(leftPage, document);
primarySecondPageTextFrame.GeometricBounds = GetPageBoundsWithMargin(rightPage, document);
masterSpread.PrimaryTextFrame = primaryFirstPageTextFrame; //This causes the error
return GetFileData(document, savePath);
}
I notice JavaScript API is very similar to C# as well, so it'd also help if I get a JavaScript answer.
