[tool] Draw regular polygons or stars

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

User avatar
Mathew
User
Posts: 930
Joined: Thu Jun 19, 2014 7:30 pm

[tool] Draw regular polygons or stars

Post by Mathew »

Maybe there's a built-in way to do this, but here's a tool I made because sometimes I need to draw regular polygons to mark up symbols. This tool will draw a polygon annotation.

Installation
drawPolygon v1.0.zip
To use, unzip and copy the .js files to the JavaScripts folder and restart PDF-XChange. More details: viewtopic.php?p=196006#p196006

It will show up on the "Add-in" section of the home ribbon; or in Tools > Add-in Tools for the classic UI. Select that menu item and it opens a dialog box:
image.png

Enter number of sides (at least 3), scale (this is the radius of a circle that circumscribes the polygon), and rotation. You can manually enter the rotation, or pick an alignment from the drop-down menu.
If you select to place it by pressing the CTRL key, the polygon will follow the mouse at the alignment you picked, or at its center, until CTRL is pressed (or the number of selected items increases). If not selected it will draw the polygon at the current mouse position (probably under the dialog box). The option to place by mouse click is a bit experimental -- it puts a temporary form element in the background to detect mouse clicks:
image(1).png
You can change border color, style, fill, size, etc just as you would for any other markup - it's a Polygon annotation.

The option "Draw as Star" draws a star shape. This is a 6 sided polygon as a star:
image(2).png
There's an option in the javascript file on line 79 to change how spiky the star is. It's the ratio of the radius at the inside of the star. A smaller number will make it extend in closer to the center:

Code: Select all

// factor to scale inner point of star
const starShape = (sides > 3) ? 0.5 : 0.25;
Translations
This tool is translatable with 1ang.js and the following translation strings:

Code: Select all

# Translation file (requires 1ang v1.0 or later)
# Domain for this translation file is "regularPolygon"
# Filename must be "translationData.regularPolygon.json" and saved in the JavaScripts folder

# It is not necessary for all strings to be included in every language,
#   but don't put an empty string "" for the translation, because that's what will be shown!
# This must be formatted as a JSON file except that:
#   lines starting with # and empty lines are stripped out before processing

# "Language" is three letter code as output from app.language
# "Original Text" is case sensitive!
#   Notice that the last item in a list does not allow a comma after it.
# Format:
#  "Language 1": {
#    "Original Text": "Translated Text in Language 1",
#    "Original Text 2": ["Translated Text 2 singular", "Translated Text 2 plural"]
#   },
#  "Language 2": {
#    "Original Text": "Translated Text in Language 2"
#   }

{
 "": {
     "Add-in Tools": "",
     "Regular Polygon…": "",
     "Polygon": "",
     "Alignment:": "",
     "Point Up": "",
     "Point Down": "",
     "Point Left": "",
     "Point Right": "",
     "Flat Up": "",
     "Flat Down": "",
     "Flat Left": "",
     "Flat Right": "",
     "Regular Polygon": "",
     "Sides:": "",
     "Draw as Star": "",
     "Scale:": "",
     "points": "",
     "Rotation °": "",
     "Place by pressing CTRL key": "",
     "Place by mouse click": ""
 }
}


Limitations
  • There's no built-in way to detect mouse clicks, so this was mainly an exercise in finding workarounds. The event.modifier and event.shift are constantly updated, so I used that as a way to signal placement. There's also a mouseUp event on fields, so I tried putting in temporary form fields in the background. I think a built in way to expose mouse clicks to javascript would be better, but this is the best I can do.

This is the old version: It just places the polygon in the middle of the page.
You do not have the required permissions to view the files attached to this post.
Last edited by Mathew on Tue Sep 29, 2026 4:05 am, edited 2 times in total.
User avatar
Daniel - PDF-XChange
Site Admin
Posts: 13249
Joined: Wed Jan 03, 2018 6:52 pm

Re: [tool] Draw regular polygons or stars

Post by Daniel - PDF-XChange »

Hello, Mathew

Thank you for this, I do not believe we offer a feature to accommodate something like this just yet, so this could be very handy indeed!

Kind regards,
Dan McIntyre - Support Technician
PDF-XChange Co. LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
[email protected]
User avatar
Mathew
User
Posts: 930
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Draw regular polygons or stars

Post by Mathew »

I came up with a way to make placing the polygon better. Because there's no official way to get a mouse click, it's a little clumsy; but I'm quite proud of my workarounds :wink: It will now draw the polygon, and the polygon will follow the cursor until CTRL key is pressed. One can pick whether a side or point is aligned with the cursor, and if that's done, clicking will select the new polygon and place it.
image.png
drawPolygon v1.0.zip
Changes v1.0
  • pick alignment of polygon;
  • put the edge or point from alignment at the current cursor position;
  • make translatable on domain 'regularPolygon';
  • add icon;
  • move to Add-in Tools;
  • another simulated "click event": if number of selections changes, then end placement;
  • handle when layout is two-page;
Changes v0.3 (not posted)
  • place by pressing Ctrl or Experimental: getting mouse click from temporary form element
Changes v0.2 (not posted)
  • draw in current mouse position
You do not have the required permissions to view the files attached to this post.
User avatar
Sean - PDF-XChange
Site Admin
Posts: 1271
Joined: Wed Sep 14, 2016 5:42 pm

Re: [tool] Draw regular polygons or stars

Post by Sean - PDF-XChange »

Looks like a great improvement, thanks again Mathew.

Kind regards,
Sean Godley
Technical Writer
PDF-XChange Co LTD
Sales: +1 (250) 324-1621
Fax: +1 (250) 324-1623
TBS
User
Posts: 20
Joined: Mon Jan 05, 2026 2:52 pm

Re: [tool] Draw regular polygons or stars

Post by TBS »

The mouse click feature is excellent.
User avatar
Mathew
User
Posts: 930
Joined: Thu Jun 19, 2014 7:30 pm

Re: [tool] Draw regular polygons or stars

Post by Mathew »

:wink: