Skip to content

Commit 4e8992f

Browse files
Add 163 example(s) for working-with-annotations
1 parent 3765296 commit 4e8992f

18 files changed

Lines changed: 815 additions & 745 deletions
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Forms;
5+
using Aspose.Pdf.Annotations;
6+
using Aspose.Pdf.Drawing;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
const string inputPath = "input.pdf";
13+
const string outputPath = "output_with_button.pdf";
14+
15+
if (!File.Exists(inputPath))
16+
{
17+
Console.Error.WriteLine($"File not found: {inputPath}");
18+
return;
19+
}
20+
21+
// Load the existing PDF document
22+
using (Document doc = new Document(inputPath))
23+
{
24+
// Define the button rectangle (lower‑left‑x, lower‑left‑y, upper‑right‑x, upper‑right‑y)
25+
Aspose.Pdf.Rectangle buttonRect = new Aspose.Pdf.Rectangle(100, 500, 200, 550);
26+
27+
// Create a push button field on the first page
28+
ButtonField button = new ButtonField(doc.Pages[1], buttonRect)
29+
{
30+
Name = "ExportAnnotationsBtn",
31+
PartialName = "ExportAnnotationsBtn",
32+
NormalCaption = "Export JSON"
33+
};
34+
35+
// JavaScript that gathers all annotations, converts them to JSON, and shows the result
36+
string jsCode = @"
37+
var ann = this.getAnnots();
38+
if (ann != null) {
39+
var json = JSON.stringify(ann);
40+
app.alert(json);
41+
} else {
42+
app.alert('No annotations found.');
43+
}";
44+
JavascriptAction jsAction = new JavascriptAction(jsCode);
45+
46+
// Assign the JavaScript to the button's mouse‑press action
47+
button.Actions.OnPressMouseBtn = jsAction;
48+
49+
// Add the button to the document's form fields collection
50+
doc.Form.Add(button);
51+
52+
// Save the modified PDF
53+
doc.Save(outputPath);
54+
}
55+
56+
Console.WriteLine($"PDF saved with button annotation: {outputPath}");
57+
}
58+
}

working-with-annotations/add-js-calculation-button-to-pdf-form.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string outputPath = "output.pdf";
11+
const string videoPath = "sample.mp4";
12+
13+
// Verify that the video file exists before creating the annotation
14+
if (!File.Exists(videoPath))
15+
{
16+
Console.Error.WriteLine($"Video file not found: {videoPath}");
17+
return;
18+
}
19+
20+
// Document lifecycle must be wrapped in a using block for deterministic disposal
21+
using (Document doc = new Document())
22+
{
23+
// Add a blank page (pages are 1‑based)
24+
Page page = doc.Pages.Add();
25+
26+
// Define the rectangle where the screen annotation will appear
27+
// Rectangle(left, bottom, right, top) – fully qualified to avoid ambiguity
28+
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(100, 500, 400, 800);
29+
30+
// Create the ScreenAnnotation with the video file path
31+
ScreenAnnotation screen = new ScreenAnnotation(page, rect, videoPath);
32+
33+
// Optional: set a title and contents for the annotation
34+
screen.Title = "Embedded Video";
35+
screen.Contents = "Video plays in loop with controls disabled";
36+
37+
// Add the annotation to the page's annotation collection
38+
page.Annotations.Add(screen);
39+
40+
// Save the PDF document
41+
doc.Save(outputPath);
42+
}
43+
44+
Console.WriteLine($"PDF with ScreenAnnotation saved to '{outputPath}'.");
45+
}
46+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
using Aspose.Pdf.Text;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input.pdf";
12+
const string outputPath = "output.pdf";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"File not found: {inputPath}");
17+
return;
18+
}
19+
20+
// Load the PDF document (lifecycle rule: use using for disposal)
21+
using (Document doc = new Document(inputPath))
22+
{
23+
// Choose the page where the annotation will be placed
24+
Page page = doc.Pages[1];
25+
26+
// Define the rectangle for the free‑text annotation
27+
// Fully qualified to avoid ambiguity with System.Drawing.Rectangle
28+
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(100, 500, 300, 600);
29+
30+
// Create a DefaultAppearance for the annotation (font, size, color)
31+
// Note: DefaultAppearance constructor requires System.Drawing.Color for the third argument
32+
DefaultAppearance appearance = new DefaultAppearance("Helvetica", 12, System.Drawing.Color.Black);
33+
34+
// Create the free‑text annotation on the selected page
35+
FreeTextAnnotation freeText = new FreeTextAnnotation(page, rect, appearance)
36+
{
37+
// Set a background color for better visibility (optional)
38+
Color = Aspose.Pdf.Color.LightGray,
39+
40+
// Multiline content – use newline characters for plain text
41+
Contents = "First line of text\nSecond line of text\nThird line of text",
42+
43+
// Use RichText with CSS line-height to control line spacing (1.5 = 150%)
44+
RichText = "<body style='line-height:1.5'>First line of text<br/>Second line of text<br/>Third line of text</body>"
45+
};
46+
47+
// Add the annotation to the page
48+
page.Annotations.Add(freeText);
49+
50+
// Save the modified PDF (lifecycle rule: save inside the using block)
51+
doc.Save(outputPath);
52+
}
53+
54+
Console.WriteLine($"Free‑text annotation added and saved to '{outputPath}'.");
55+
}
56+
}

working-with-annotations/add-rotated-semi-transparent-png-watermark.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

working-with-annotations/add-submit-button-with-javascript-to-pdf.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
using Aspose.Pdf.Forms;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input.pdf";
12+
const string outputPath = "output_with_toggle_button.pdf";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"File not found: {inputPath}");
17+
return;
18+
}
19+
20+
// Load the PDF document inside a using block for proper disposal
21+
using (Document doc = new Document(inputPath))
22+
{
23+
// Get the first page (1‑based indexing)
24+
Page page = doc.Pages[1];
25+
26+
// Define the button rectangle (llx, lly, urx, ury)
27+
Aspose.Pdf.Rectangle btnRect = new Aspose.Pdf.Rectangle(100, 700, 200, 750);
28+
29+
// Create a push button field on the page
30+
ButtonField toggleButton = new ButtonField(page, btnRect)
31+
{
32+
Name = "ToggleAnnotationsButton",
33+
NormalCaption = "Toggle Annotations",
34+
Color = Aspose.Pdf.Color.LightGray,
35+
Highlighting = HighlightingMode.Push
36+
};
37+
38+
// JavaScript that toggles the hidden flag of every annotation on the current page
39+
string jsCode = @"
40+
var annots = this.getAnnots();
41+
if (annots != null) {
42+
for (var i = 0; i < annots.length; i++) {
43+
annots[i].hidden = !annots[i].hidden;
44+
}
45+
}
46+
";
47+
48+
// Assign the JavaScript to the button's mouse‑press action
49+
toggleButton.Actions.OnPressMouseBtn = new JavascriptAction(jsCode);
50+
51+
// Add the button to the page's annotation collection
52+
page.Annotations.Add(toggleButton);
53+
54+
// Save the modified document
55+
doc.Save(outputPath);
56+
}
57+
58+
Console.WriteLine($"PDF saved with toggle button: '{outputPath}'");
59+
}
60+
}

0 commit comments

Comments
 (0)