Visualizing page elements with DocMaker 3.0

Let us take a look at a cool little feature in the DocMaker 3.0 API that allows you to visualize page elements in InDesign files (e.g. in a front end web UI). For each element that occurs on a given page, the GetShapeInPage method calculates its shape, mapped to the coordinate space and clipped to the bounds of the page. The method returns a PolygonD structure, which represents either a polygon with straight lines or a cubic Bezier polygon.

We use this functionality for instance in DocMaker Studio to highlight page elements that have been changed but have not yet been saved:

The following code snippet shows how to draw the shape of a page element onto an image canvas:

C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public static void TracePageElement(Element element, Page page, Image image)
{
    // Get the shape of the page element clipped against the page bounds.
    PolygonD shape = element.GetShapeInPage(page);

    bool isBezier = shape.HasControlPoints;

    PointD[] polygonPoints = isBezier ? shape.ControlPoints : shape.Points;

    // Make sure the coordinates fit on our image canvas.
    double scaleX = page.Bounds.Width / image.Width;
    double scaleY = page.Bounds.Height / image.Height;

    PointF[] drawingPoints = polygonPoints.Select
        (p => new PointF((float)(p.X/scaleX), (float)(p.Y/scaleY))).ToArray();

    // Draw the shape on the image canvas.
    using (Graphics graphics = Graphics.FromImage(image))
    {
        Pen pen = new Pen(Color.DeepPink, 3);
        if (isBezier)
        {
            graphics.DrawBeziers(pen, drawingPoints);
        }
        else
        {
            graphics.DrawPolygon(pen, drawingPoints);
        }
    }
}
And here we illustrate how you might be calling the above code in order to generate a "silhouette preview" of an InDesign document:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void Main()
{
    Application application = new Application();
    if (application.LogOn("userName", "password") == LogOnStatus.LoggedOn)
    {
        // Pick an arbitrary InDesign file.
        RecordCollection records = new RecordCollection(application);
        records.Load(new SearchExpression("File.Version.Extension = INDD"));
        using (Document document = Document.CreateFromFile(records[0].Files.Master))
        {
            // Trace each page in the document to a separate PNG file.
            foreach (Page page in document.Pages)
            {
                Bitmap bitmap = new Bitmap(page.PreviewWidth, page.PreviewHeight);
                foreach (Element element in document.GetAllElements(e => e.IsOnPage(page)))
                {
                    TracePageElement(element, page, bitmap);
                }
                string fileName = "Page" + page.PageNumber + ".png";
                bitmap.Save(Path.Combine(Path.GetTempPath(), fileName), System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }
}
This is what "Page1.png" looks like for the InDesign document shown in the first screenshot above:

I am sure that the web designers amongst you will come up with some pretty creative ideas for using this feature...

Comments

Leave a comment
You must be logged in to post comments.
Sign in now
 
 
Technical
Business
rss feed