Skip to main content
Participating Frequently
July 2, 2008
Question

Using Delphi, and the InDesign automation object ("indesign

  • July 2, 2008
  • 22 replies
  • 5198 views
Using Delphi, and the InDesign automation object ("indesign.application"), I need to open a indd file, move to a bookmark and insert text or picture at that exact location. Any examples of such code I could start with?
This topic has been closed for replies.

22 replies

Participating Frequently
July 7, 2008
Thanks guys. I'll have my users create simple pages with text frames that I will fill with text and images from my Delphi database application.

My only question remains how do I label a text frame while I'm actually creating the document in InDesign? Why am I not seeing something that must be so easy?

Bill
Participating Frequently
July 7, 2008
In InDesign you need XY-coordinates to place texts or images.
This is why bookmarks are not the best choice to place something!

*Does your bookmark point to a specific page?
*When you've got the page the bookmark points to, do you know where to place your text and images then?
*Or does your bookmark point to a specific text?
*Do you want to replace this text?

You must have a page and a xy-position on that page.
THEN you can place an image or a text.

If there are frames yet to contain the texts or images, you can place the text or the images THERE. But you will have to identify them to get a pointer.

If you would send me your target document as an example, maybe I would be able to tell you some more specific.

Marc
Known Participant
July 7, 2008
Hi Bill,

Sorry to be so late getting to this thread!

If you want to add something to a page, or find an object that's on a page, you need a reference to the page. Simply displaying the page with ShowBookmark won't give you a reference to the page--InDesign scripting, for the most part, doesn't rely on the selection and current view in the way that Word scripting does (this is a good thing, but that's a topic for another day).

You had to have a reference to a page to create the HyperlinkPageDestination that you used to create the bookmark--but, if not, you can get a reference to the page using (VBScript form):

Rem Given a reference to an instance of InDesign "myInDesign"...
myInDesign.Bookmarks.Item("Description").ShowBookMark
Set myPage = myInDesign.ActiveWindow.ActivePage

In general, though, you probably don't need the bookmark and the whole "ActiveWindow" construct--just use the reference to the page itself.

Now that you have a page, you can put things on the page.

Rem Create a rectangle
Set myRectangle = myPage.Rectangles.Add
Rem Size and position the rectangle
myRectangle.GeometricBounds = Array("6p", "6p", "24p", "24p")
Rem Place a graphic in the rectangle
myRectangle.Place "c:\test.tif"

If you have a text frame on the page that has the label "myLabel", you can get a reference to the text frame and add text:

Set myTextFrames = myPage.TextFrames.Item("myLabel")
Rem myTextFrames will return an array--get the first item
Set myTextFrame = myTextFrames(0)
Rem Replace the text in the text frame:
myTextFrame.Contents = "This is some text."
Rem Or add text to the end of the text frame:
myTextFrame.InsertionPoints.Item(-1).Contents = "This is some additional text."

If you know that there is only one text frame with the label "Description" in the entire document, you can even say:

Set myTextFrames = myDocument.TextFrames.Item("myLabel")
Rem myTextFrames will return an array--get the first item
Set myTextFrame = myTextFrames(0)

...to get to it directly. If, at that point, you want to get at the page containing the text frame (and assuming that the text frame is not in a group, pasted inside text, or pasted inside another page item), you can use:

Set myPage = myTextFrame.Parent

Have you looked through the "Working with Documents" chapter of the InDesign CS3 Scripting Guide: VBScript and the associated scripts archive? I realize that you're using Delphi, but it doesn't look too difficult to convert from one to the other.

Thanks,

Ole
Participating Frequently
July 7, 2008
Marc

I can use the Object Browser in VB to view all the methods and parameters, but I still can't figure out how to simply insert text or an image once I move to a bookmark called "Description" using:

InDesignDoc.Bookmarks('Description').ShowBookmark;

Also, I still can't figure out how to label a textframe so I can access it later via the method you described earlier.
Participating Frequently
July 6, 2008
Delphi creates the .pas file after you imported the type library.
Use your Delphi3 help. There must be a possibility to import "type libraries".

Marc
Participating Frequently
July 5, 2008
I think bookmarks would work well for what I need to do, so I would like to be able to list all the bookmarks in a doc, change them if necessary, and then goto to them and place text or images.

Regarding this:
-> Import component
-> Import Type Library
->Adobe InDesign CS3 Type Library

I'm still using Delphi3 for development so it looks like I have to wing it there. Is there any type of ".pas" file out there like there is for Word.
Participating Frequently
July 4, 2008
Hi,

it's called "Resources for Visual Basic.pas".
You can import it via BDS:

Menu:Components
-> Import component
-> Import Type Library
->Adobe InDesign CS3 Type Library

In InDesign you just have to duplicate the last page:

>idDoc1.pages.lastItem().duplicate(idAtEnd);

Marc
Participating Frequently
July 2, 2008
The users will create the docs and put the proper bookmarks in them. But you mention another way, so do you label textframes so they can be referenced by their scriptlabel.

What's the name of the type library for InDesign to use with Delphi. With Word its called Word_TLB.pas

One more thing, how would you move to the end of the current page and insert a duplicate of the current page, similat to:
MSWordObj.Selection.EndKey(6);
MSWordObj.Selection.InsertBreak(7); //page break
MSWordObj.Selection.InsertFile(sOLETemplate);

Thanks again.
Participating Frequently
July 2, 2008
I don't really understand...

They have made the documents or they will make them?
Did they use "bookmarks" or will they use them if you tell them to do so?

Did you import the InDesign Typelibrary?
It's very helpful when coding ID in Delphi!

// declaration
var vIdApplication1:Olevariant;
idDoc1:Document;
idPage1:Page;
ov1:Olevariant;
s1:String;

// to get the application
vIdApplication1 := CreateOleObject('InDesign.Application.CS3');

{
to open a document
Here you see, how to typecast olevariants
You have to typecast twice: one the IDispatch, then the real
ID type from the typelibrary.
If you typecast them, you will get coding help from BDE.
}
idDoc1 := _Application(IDispatch(vIdApplication1)).Open(aDocname, True);

{
If you typecasted to a real InDesign type, you'll reget real
InDesign types (and pointers) from functions.
}
idPage1 := idDoc1.pages.firstItem();

{
Then you have to get the pointer to an object
In InDesign you need Textframes to place text, not only selections.
There are several ways to mark textframes so you can find them for
text input. The easiest is to get them by their scriptlabel.
}
s1 := 'MyTestframelabel';
vTxt1 := idDoc1.Textframes.Item(s1);

// and then you can put text into it
vTxt1.contents := 'Hello world';

{
to place images instead of text you can 'place' the file
}
ov1:= vTxt1.place('c:\images\myimage.jpg');

[t.b.c.]

This should lead you on the right track....
Don't hesitate to ask further questions....

Marc
Participating Frequently
July 2, 2008
Thanks for the info. I basically just need to open a indd file that has already been constructed by the user. They would have placed bookmarks on the page where they want information from our database to go, and in fact, some pictures also. I've got the whole thing done using the Word object via Delphi and just need to make the changes to accomadate InDesign.

Can you move to a bookmark and place text autmatically with InDesign?

With Word, I do this:

try
MSWordObj := GetActiveOleObject('Word.Application');
except
MSWordObj:=CreateOLEObject('Word.Application');
end;
MSWordObj.Documents.Open (sOLETemplate);
bm:=MSWordObj.ActiveDocument.bookmarks.count;
tsBM:=TStringlist.create;
for k:=1 to bm do
begin
strField:=MSWordObj.ActiveDocument.bookmarks.item(k).name;
tsBM.add(lowercase(strField));
next
//I need to get a list of bookmarks for other reasons

//Then move to the different bookmarks
MSWordObj.Selection.GoTo(-1,0,0,strField);
//And insert the text
MSWordObj.Selection.TypeText(s);
//or picture
MSWordObj.Selection.InlineShapes.AddPicture(s2);

//add sometimes I'll force stuff onto the
//clipboard and then paste it into Word. MSWordObj.Selection.ShapeRange.TextFrame.TextRange.Select;
MSWordObj.Selection.Paste;

//sometimes multiple pages are required, so I do this:
MSWordObj.Selection.EndKey(6);
MSWordObj.Selection.InsertBreak(7); //page break
MSWordObj.Selection.InsertFile(sOLETemplate);

Is there a better way than using bookmarks in InDesign? I really don't want to do the merge with XML's.

Thanks...