> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/annotation/rectangle-annotation.md).

# Add a rectangle annotation to a PDF on Server/Desktop

Learn how to add a rectangle annotation to a PDF Document page with this comprehensive code sample. Enhance your PDF annotations now! The Apryse Server SDK streamlines secure document processing with

To add a rectangle annotation to a PDF Document page.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
PDFDoc doc = new PDFDoc(filename);
Page page = doc.GetPage(1);

// Create a rectangle
Square sq = Square.Create( doc, new Rect(500,200, 580, 300 ) );
sq.SetColor(new ColorPt(1, 0, 0), 3);
sq.SetInteriorColor(new ColorPt(0, 1, 1), 3);
double[] dash = new double[2];
dash[0]=4;dash[1]=2;
sq.SetBorderStyle(new Annot.BorderStyle( Annot.BorderStyle.Style.e_dashed, 6, 0, 0, dash ) );
sq.SetPadding( new Rect(4,4,4,4) );
sq.RefreshAppearance();
page.AnnotPushBack( sq );
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
PDFDoc doc(filename);
Page page = doc.GetPage(1);

// Create a rectangle
Square sq = Square::Create( doc, Rect(500,200, 580, 300 ) );
sq.SetColor(ColorPt(1, 0, 0), 3);
sq.SetInteriorColor(ColorPt(0, 1, 1), 3);
std::vector<double> dash( 2 ); dash[0]=4;dash[1]=2;
sq.SetBorderStyle( Annot::BorderStyle( Annot::BorderStyle::e_dashed, 6, 0, 0, dash ) );
sq.SetPadding( 4 );
sq.RefreshAppearance();
page.AnnotPushBack( sq );
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
doc := NewPDFDoc(filename)
page := doc.GetPage(1)

// Create a rectangle
sq := SquareCreate( doc.GetSDFDoc(), NewRect(500.0, 200.0, 580.0, 300.0 ) )
sq.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
sq.SetInteriorColor(NewColorPt(0.0, 1.0, 1.0), 3)
var dash = NewVectorDouble()
dash.Add(4.0)
dash.Add(2.0)
sq.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 6.0, 0.0, 0.0, dash ) )
dash.Clear()
sq.SetPadding( 4.0 )
sq.RefreshAppearance()
page.AnnotPushBack( sq )
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
PDFDoc doc = new PDFDoc(filename);
Page page = doc.getPage(1);

// Create a rectangle
Square sq = Square.create(doc, new Rect(500, 200, 580, 300));
sq.setColor(new ColorPt(1, 0, 0), 3);
sq.setInteriorColor(new ColorPt(0, 1, 1), 3);
double[] dash = {4, 2};
sq.setBorderStyle(new Annot.BorderStyle(Annot.BorderStyle.e_dashed, 6, 0, 0, dash));
sq.setPadding(4);
sq.refreshAppearance();
page.annotPushBack(sq);
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);
  const page = await doc.getPage(1);

  // Create a rectangle
  const squareAnnot = await PDFNet.SquareAnnot.create(
    doc,
    new PDFNet.Rect(500, 200, 580, 300)
  );
  await squareAnnot.setColor(await PDFNet.ColorPt.init(1, 0, 0), 3);
  await squareAnnot.setInteriorColor(await PDFNet.ColorPt.init(0, 1, 1), 3);
  const dash = [4, 2];
  await squareAnnot.setBorderStyle(
    await PDFNet.AnnotBorderStyle.createWithDashPattern(
      PDFNet.AnnotBorderStyle.Style.e_dashed,
      6,
      0,
      0,
      dash
    )
  );
  await squareAnnot.setPadding(new PDFNet.Rect(4, 4, 4, 4));
  await squareAnnot.refreshAppearance();
  await page.annotPushBack(squareAnnot);
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
val doc = PDFDoc(filename)
val page = doc.getPage(1)

// Create a rectangle
val sq = Square.create(doc, Rect(500.0, 200.0, 580.0, 300.0))
sq.setColor(ColorPt(1.0, 0.0, 0.0), 3)
sq.setInteriorColor(ColorPt(0.0, 1.0, 1.0), 3)
val dash = doubleArrayOf(4.0, 2.0)
sq.borderStyle = BorderStyle(BorderStyle.e_dashed, 6, 0, 0, dash)
sq.setPadding(4.0)
sq.refreshAppearance()
page.annotPushBack(sq)
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTPage *page = [doc GetPage:1];

// Create a rectangle
PTSquare *sq = [PTSquare Create: [doc GetSDFDoc] pos: [[PTPDFRect alloc] initWithX1: 500 y1: 200 x2: 580 y2: 300]];
[sq SetColor: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0] numcomp: 3];
[sq SetInteriorColor: [[PTColorPt alloc] initWithX: 0 y: 1 z: 1 w: 0] CompNum: 3];
NSMutableArray *dash = [NSMutableArray arrayWithCapacity: 2];
[dash addObject: @4.0];
[dash addObject: @2.0];
[sq SetBorderStyle: [[PTBorderStyle alloc] initWithS: e_ptdashed b_width: 6 b_hr: 0 b_vr: 0 b_dash: dash] oldStyleOnly: NO];
[sq SetPadding: 4];
[sq RefreshAppearance];
[page AnnotPushBack: sq];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
let page: PTPage = doc.getPage(1)

// Create a rectangle
let sq: PTSquare = PTSquare.create(doc.getSDFDoc(), pos: PTPDFRect(x1: 500, y1: 200, x2: 580, y2: 300))
sq.setColor(PTColorPt(x: 1, y: 0, z: 0, w: 0), numcomp: 3)
sq.setInteriorColor(PTColorPt(x: 0, y: 1, z: 1, w: 0), compNum: 3)
let dash = NSMutableArray(capacity: 2)
dash.add(4.0)
dash.add(2.0)
sq.setBorderStyle(PTBorderStyle(s: e_ptdashed, b_width: 6, b_hr: 0, b_vr: 0, b_dash: dash), oldStyleOnly: false)
sq.setPadding(4)
sq.refreshAppearance()
page.annotPushBack(sq)
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
$doc = new PDFDoc($filename);
$page = $doc->GetPage(1);

// Create a rectangle
$sq = Square::Create( $doc->GetSDFDoc(), new Rect(500.0,200.0, 580.0, 300.0 ) );
$sq->SetColor(new ColorPt(1.0, 0.0, 0.0), 3);
$sq->SetInteriorColor(new ColorPt(0.0, 1.0, 1.0), 3);
$sq->SetBorderStyle( new BorderStyle( BorderStyle::e_dashed, 6.0, 0.0, 0.0, array(4.0, 2.0) ) );
$sq->SetPadding( 4.0 );
$sq->RefreshAppearance();
$page->AnnotPushBack( $sq );
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
doc = PDFDoc(filename)
page = doc.GetPage(1)

# Create a rectangle
sq = Square.Create( doc.GetSDFDoc(), Rect(500,200, 580, 300 ) )
sq.SetColor(ColorPt(1, 0, 0), 3)
sq.SetInteriorColor(ColorPt(0, 1, 1), 3)
sq.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 6, 0, 0, [4, 2] ) )
sq.SetPadding( 4 )
sq.RefreshAppearance()
page.AnnotPushBack( sq )
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
doc = PDFDoc.new(filename)
page = doc.GetPage(1)

# Create a rectangle
sq = Square.Create( doc.GetSDFDoc(), Rect.new(500,200, 580, 300 ) )
sq.SetColor(ColorPt.new(1, 0, 0), 3)
sq.SetInteriorColor(ColorPt.new(0, 1, 1), 3)
sq.SetBorderStyle( BorderStyle.new( BorderStyle::E_dashed, 6, 0, 0, [4, 2] ) )
sq.SetPadding( 4 )
sq.RefreshAppearance()
page.AnnotPushBack( sq )
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Dim doc As PDFDoc = New PDFDoc(filename)
Dim page As Page = doc.GetPage(1)

' Create a rectangle
Dim sq As Square = Square.Create(doc, New Rect(500, 200, 580, 300))
sq.SetColor(New ColorPt(1, 0, 0), 3)
sq.SetInteriorColor(New ColorPt(0, 1, 1), 3)
Dim dash() As Double = New Double((2) - 1) {}
dash(0) = 4
dash(1) = 2
sq.SetBorderStyle(New Annot.BorderStyle(Annot.BorderStyle.Style.e_dashed, 6, 0, 0, dash))
sq.SetPadding(New Rect(4, 4, 4, 4))
sq.RefreshAppearance()
page.AnnotPushBack(sq)
```

{% endcode %}
{% endtab %}
{% endtabs %}

[Add or edit PDF annotations sample](/core/get-started/samples/annotationtest.md) Full code sample which shows how to add or edit PDF annotations (e.g. hyperlink, intra-document link, stamp, rubber stamp, file attachment, sound, text, free-text, line, circle, square, polygon, polyline, rectangle, squiggly, caret, and ink). Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.apryse.com/core/annotation/rectangle-annotation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
