Skip to content

Commit 512975e

Browse files
authored
Merge pull request #118 from aspose-pdf-cloud/pdfcloud-5050-added-snippets-annotations
PDFCLOUD-5050: added snippets for Annotations
2 parents d47aa94 + d32c341 commit 512975e

12 files changed

+480
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\src\Aspose.Pdf.Cloud.Sdk\Aspose.Pdf.Cloud.Sdk.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Aspose.Pdf.Cloud.Sdk.Api;
2+
using Aspose.Pdf.Cloud.Sdk.Model;
3+
using Newtonsoft.Json;
4+
5+
namespace Annotations
6+
{
7+
public class ConfigParams
8+
{
9+
public string CrdentialPath { get; } = "..\\credentials.json ";
10+
public string LOCAL_FOLDER { get; } = "C:\\Samples";
11+
public string REMOTE_TEMP_FOLDER { get; } = "TempPdfCloud";
12+
public string PDF_DOCUMENT { get; } = "sample.pdf";
13+
public string PDF_OUTPUT { get; } = "output_sample.pdf";
14+
public int PAGE_NUMBER { get; } = 1;
15+
16+
public string ANNOTATION_ID { get; } = "GE5TAOZTHA2CYMRZGUWDIMBZFQZTEMA";
17+
18+
public string NEW_HL_ANNOTATION_TEXT { get; } = "NEW HIGHLIGHT TEXT ANNOTATION";
19+
public string NEW_HL_ANNOTATION_DESCRIPTION { get; } = "This is a sample highlight annotation";
20+
public string NEW_HL_ANNOTATION_SUBJECT { get; } = "Highlight Text Box Subject";
21+
public string NEW_HL_ANNOTATION_CONTENTS { get; } = "Highlight annotation sample contents";
22+
23+
public string NEW_SO_ANNOTATION_TEXT { get; } = "NEW STRIKEOUT TEXT ANNOTATION";
24+
public string NEW_SO_ANNOTATION_DESCRIPTION { get; } = "This is a sample strikeout annotation";
25+
public string NEW_SO_ANNOTATION_SUBJECT { get; } = "Strikeout Text Box Subject";
26+
public string NEW_SO_ANNOTATION_CONTENTS { get; } = "Strikeout annotation sample contents";
27+
28+
public string NEW_UL_ANNOTATION_TEXT { get; } = "NEW UNDERLINE TEXT ANNOTATION";
29+
public string NEW_UL_ANNOTATION_DESCRIPTION { get; } = "This is a sample underline annotation";
30+
public string NEW_UL_ANNOTATION_SUBJECT { get; } = "Underline Text Box Subject";
31+
public string NEW_UL_ANNOTATION_CONTENTS { get; } = "Underline annotation sample contents";
32+
33+
public string NEW_FT_ANNOTATION_TEXT { get; } = "NEW FREE TEXT ANNOTATION";
34+
public string NEW_FT_ANNOTATION_DESCRIPTION { get; } = "This is a sample annotation";
35+
public string NEW_FT_ANNOTATION_SUBJECT { get; } = "Free Text Box Subject";
36+
public string NEW_FT_ANNOTATION_CONTENTS { get; } = "Free Text annotation sample contents";
37+
38+
public string REPLACED_CONTENT { get; } = "This is a replaced sample annotation";
39+
}
40+
41+
public class Credentials
42+
{
43+
public string Id { get; set; }
44+
public string Key { get; set; }
45+
}
46+
47+
public class AnnotationsHelper
48+
{
49+
public PdfApi pdfApi { get; private set; }
50+
public ConfigParams config { get; private set; }
51+
52+
public AnnotationsHelper()
53+
{
54+
config = new ConfigParams();
55+
string jsCredText = File.ReadAllText(config.CrdentialPath);
56+
Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText);
57+
pdfApi = new PdfApi(cred.Key, cred.Id);
58+
}
59+
60+
public async Task UploadFile(string fileName)
61+
{
62+
using (var file = File.OpenRead(Path.Combine(config.LOCAL_FOLDER, fileName)))
63+
{
64+
FilesUploadResult response = await pdfApi.UploadFileAsync(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName), file);
65+
if (response == null)
66+
Console.WriteLine("UploadFile(): Unexpected error - no response!");
67+
else if (response.Errors != null && response.Errors.Count > 0)
68+
foreach (var error in response.Errors)
69+
Console.WriteLine("UploadFile(): {0} -> {1}", [error.Code, error.Message]);
70+
else
71+
Console.WriteLine("UploadFile(): File '{0}' successfully uploaded.", fileName);
72+
}
73+
}
74+
75+
public async Task DownloadFile(string fileName, string outputName, string outputPrefix)
76+
{
77+
Stream stream = pdfApi.DownloadFile(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName));
78+
using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, outputPrefix + outputName));
79+
stream.Position = 0;
80+
await stream.CopyToAsync(fileStream);
81+
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + outputName);
82+
}
83+
84+
public async Task DeletePopupAnnotationsAsync(string documentName, string parentAnnotation, string remoteFolder)
85+
{
86+
// Delete popup annotations for typed parent annotation in the page in the PDF document.
87+
PopupAnnotationsResponse response = await pdfApi.GetDocumentPopupAnnotationsAsync(documentName, folder: config.REMOTE_TEMP_FOLDER);
88+
if (response == null)
89+
Console.WriteLine("DeletePopupAnnotations(): Unexpected error!");
90+
else if (response.Code < 200 || response.Code > 299)
91+
Console.WriteLine("DeletePopupAnnotations(): Unexpected error '{0}'!", response.Status);
92+
else
93+
{
94+
foreach (PopupAnnotationWithParent annotation in response.Annotations.List)
95+
{
96+
if (annotation.Parent.Id == parentAnnotation)
97+
{
98+
string popupContents = annotation.Contents;
99+
AsposeResponse response2 = await pdfApi.DeleteAnnotationAsync(documentName, annotation.Id, folder: remoteFolder);
100+
if (response2 == null)
101+
Console.WriteLine("DeletePopupAnnotations(): Unexpected error!");
102+
else if (response2.Code < 200 || response2.Code > 299)
103+
Console.WriteLine("DeletePopupAnnotations(): Failed to delete popup annotation '{0}' from the document.", popupContents);
104+
else
105+
Console.WriteLine("DeletePopupAnnotations(): popup annotations '{0}' with '{1}' contents deleted from the document '{1}.", annotation.Id, popupContents, documentName);
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class DeletePageAnnotations
6+
{
7+
public async static Task MakeDeleteAsync(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
8+
{
9+
// Delete annotation from the PDF document.
10+
await helper.UploadFile(documentName);
11+
AsposeResponse response = await helper.pdfApi.DeletePageAnnotationsAsync(documentName, pageNumber, folder: remoteFolder);
12+
if (response == null)
13+
Console.WriteLine("DeletePageAnnotations(): Unexpected error!");
14+
else if (response.Code < 200 || response.Code > 299)
15+
Console.WriteLine("DeletePageAnnotations(): Failed to delete annotation from the document.");
16+
else {
17+
Console.WriteLine("DeletePageAnnotations(): annotations on page '{0}' deleted from the document '{1}.", pageNumber, documentName);
18+
await helper.DownloadFile(documentName, outputName, "del_page_annotations_");
19+
}
20+
}
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class DeleteTextAnnotation
6+
{
7+
public async static Task MakeDeleteAsync(AnnotationsHelper helper, string documentName, string annotationId, string outputName, string remoteFolder)
8+
{
9+
// Delete annotation from the PDF document.
10+
await helper.UploadFile(documentName);
11+
AsposeResponse response = await helper.pdfApi.DeleteAnnotationAsync(documentName, annotationId, folder: remoteFolder);
12+
if (response == null)
13+
Console.WriteLine("DeleteTextAnnotation(): Unexpected error!");
14+
else if (response.Code < 200 || response.Code > 299)
15+
Console.WriteLine("DeleteTextAnnotation(): Failed to delete annotation from the document.");
16+
else
17+
{
18+
await helper.DeletePopupAnnotationsAsync(documentName, annotationId, remoteFolder);
19+
Console.WriteLine("DeleteTextAnnotation(): annotations '{0}' deleted from the document '{1}.", annotationId, documentName);
20+
await helper.DownloadFile(documentName, outputName, "del_text_annotations_");
21+
}
22+
}
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class GetAnnotationById
6+
{
7+
public static async Task RequestTextAnnotationAsync(AnnotationsHelper helper, string documentName, string annotationId, string remoteFolder)
8+
{
9+
// Get annotation by Id in the PDF document.
10+
await helper.UploadFile(documentName);
11+
12+
string annotationResult = string.Empty;
13+
TextAnnotationResponse response = await helper.pdfApi.GetTextAnnotationAsync(documentName, annotationId, folder: remoteFolder);
14+
15+
if (response == null)
16+
Console.WriteLine("RequestTextAnnotationAsync(): Unexpected error!");
17+
else if (response.Code < 200 || response.Code > 299)
18+
Console.WriteLine("RequestTextAnnotationAsync(): Failed to request text annotation from the document.");
19+
else
20+
Console.WriteLine("RequestTextAnnotationAsync(): annotation '{0}' with '{1}' contents successfully found in the document '{2}.", response.Annotation.Id, response.Annotation.Contents, documentName);
21+
}
22+
}
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class GetAnnotations
6+
{
7+
public static async Task<string> RequestAnnotationsOnPageAsync(AnnotationsHelper helper, string documentName, int pageNumber, string remoteFolder)
8+
{
9+
// Get annotations from the page in the PDF document.
10+
await helper.UploadFile(documentName);
11+
12+
string annotationResult = string.Empty;
13+
AnnotationsInfoResponse response = await helper.pdfApi.GetPageAnnotationsAsync(documentName, pageNumber, folder: remoteFolder);
14+
15+
if (response == null)
16+
Console.WriteLine("RequestAnnotationsOnPageAsync(): Unexpected error!");
17+
else if (response.Code < 200 || response.Code > 299)
18+
Console.WriteLine("RequestAnnotationsOnPageAsync(): Failed to request annotations from the document.");
19+
else
20+
{
21+
foreach (AnnotationInfo annotation in response.Annotations.List)
22+
{
23+
Console.WriteLine("RequestAnnotationsOnPageAsync(): annotation '{0}' with '{1}' contents get from the '{2}' page of the document '{3}.", [annotation.Id, annotation.Contents, pageNumber, documentName]);
24+
if (string.IsNullOrEmpty(annotationResult) &&
25+
annotation.AnnotationType == AnnotationType.Text)
26+
{
27+
annotationResult = annotation.Id;
28+
}
29+
}
30+
}
31+
return annotationResult;
32+
}
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class NewFreetextAnnotation
6+
{
7+
public static async Task Append(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
8+
{
9+
await helper.UploadFile(documentName);
10+
11+
List<FreeTextAnnotation> annotations = new List<FreeTextAnnotation>
12+
{
13+
new FreeTextAnnotation(
14+
Name: "Freetext_NEW_Annotation",
15+
Rect: new Rectangle(100,350, 450,400),
16+
Flags: new List<AnnotationFlags>() { AnnotationFlags.Default },
17+
HorizontalAlignment: HorizontalAlignment.Left,
18+
Intent: FreeTextIntent.FreeTextTypeWriter,
19+
Justification: Justification.Center,
20+
RichText: helper.config.NEW_FT_ANNOTATION_TEXT,
21+
Subject: helper.config.NEW_FT_ANNOTATION_SUBJECT,
22+
Contents: helper.config.NEW_FT_ANNOTATION_CONTENTS,
23+
Title: helper.config.NEW_FT_ANNOTATION_DESCRIPTION,
24+
ZIndex: 1,
25+
TextStyle: new TextStyle(
26+
FontSize: 20,
27+
Font: "Arial",
28+
ForegroundColor: new Color( A: 0xFF, R: 0x00, G: 0xFF, B: 0x00),
29+
BackgroundColor: new Color( A: 0xFF, R: 0xFF, G: 0x00, B: 0x00)
30+
),
31+
Modified: "03/27/2025 00:00:00.000 AM"
32+
)
33+
};
34+
AsposeResponse response = await helper.pdfApi.PostPageFreeTextAnnotationsAsync(documentName, pageNumber, annotations, folder: remoteFolder);
35+
36+
if (response == null)
37+
Console.WriteLine("NewFreetextAnnotation(): Unexpected error!");
38+
else if (response.Code < 200 || response.Code > 299)
39+
Console.WriteLine("NewFreetextAnnotation(): Failed to append text annotation to the document.");
40+
else
41+
{
42+
Console.WriteLine("NewFreetextAnnotation(): annotations '{0}' added to the document '{1}.", helper.config.NEW_FT_ANNOTATION_TEXT, documentName);
43+
await helper.DownloadFile(documentName, outputName, "add_text_annotation_");
44+
}
45+
}
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class NewHighlightAnnotation
6+
{
7+
public static async Task Append(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
8+
{
9+
await helper.UploadFile(documentName);
10+
11+
List<HighlightAnnotation> annotations = new List<HighlightAnnotation>
12+
{
13+
new HighlightAnnotation(
14+
Name: "Highlight_NEW_Annotation",
15+
Rect: new Rectangle(100,350, 450,400),
16+
Flags: new List<AnnotationFlags>() { AnnotationFlags.Default },
17+
HorizontalAlignment: HorizontalAlignment.Left,
18+
VerticalAlignment: VerticalAlignment.Top,
19+
RichText: helper.config.NEW_HL_ANNOTATION_TEXT,
20+
Subject: helper.config.NEW_HL_ANNOTATION_SUBJECT,
21+
Contents: helper.config.NEW_HL_ANNOTATION_CONTENTS,
22+
Title: helper.config.NEW_HL_ANNOTATION_DESCRIPTION,
23+
ZIndex: 1,
24+
Color: new Color(A: 0xFF, R: 0x00, G: 0xFF, B: 0x00),
25+
QuadPoints: new List<Point>() {
26+
new Point(X: 10, Y: 10),
27+
new Point(X: 20, Y: 10),
28+
new Point(X: 10, Y: 20),
29+
new Point(X: 10, Y: 10)
30+
},
31+
Modified: "03/27/2025 00:00:00.000 AM"
32+
)
33+
};
34+
AsposeResponse response = await helper.pdfApi.PostPageHighlightAnnotationsAsync(documentName, pageNumber, annotations, folder: remoteFolder);
35+
36+
if (response == null)
37+
Console.WriteLine("NewHighlightAnnotation(): Unexpected error!");
38+
else if (response.Code < 200 || response.Code > 299)
39+
Console.WriteLine("NewHighlightAnnotation(): Failed to append highlight annotation to the document.");
40+
else
41+
{
42+
Console.WriteLine("NewHighlightAnnotation(): annotations '{0}' added to the document '{1}.", helper.config.NEW_HL_ANNOTATION_TEXT, documentName);
43+
await helper.DownloadFile(documentName, outputName, "add_highlight_annotation_");
44+
}
45+
}
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
namespace Annotations
4+
{
5+
public class NewStrikeoutAnnotation
6+
{
7+
public static async Task Append(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
8+
{
9+
await helper.UploadFile(documentName);
10+
11+
List<StrikeOutAnnotation> annotations = new List<StrikeOutAnnotation>
12+
{
13+
new StrikeOutAnnotation(
14+
Name: "Strikeout_NEW_Annotation",
15+
Rect: new Rectangle(100,350, 450,400),
16+
Flags: new List<AnnotationFlags>() { AnnotationFlags.Default },
17+
HorizontalAlignment: HorizontalAlignment.Left,
18+
VerticalAlignment: VerticalAlignment.Top,
19+
RichText: helper.config.NEW_SO_ANNOTATION_TEXT,
20+
Subject: helper.config.NEW_SO_ANNOTATION_SUBJECT,
21+
Contents: helper.config.NEW_SO_ANNOTATION_CONTENTS,
22+
Title: helper.config.NEW_SO_ANNOTATION_DESCRIPTION,
23+
ZIndex: 1,
24+
Color: new Color(A: 0xFF, R: 0x00, G: 0xFF, B: 0x00),
25+
QuadPoints: new List<Point>() {
26+
new Point(X: 10, Y: 10),
27+
new Point(X: 20, Y: 10),
28+
new Point(X: 10, Y: 20),
29+
new Point(X: 10, Y: 10)
30+
},
31+
Modified: "03/27/2025 00:00:00.000 AM"
32+
)
33+
};
34+
AsposeResponse response = await helper.pdfApi.PostPageStrikeOutAnnotationsAsync(documentName, pageNumber, annotations, folder: remoteFolder);
35+
36+
if (response == null)
37+
Console.WriteLine("NewStrikeoutAnnotation(): Unexpected error!");
38+
else if (response.Code < 200 || response.Code > 299)
39+
Console.WriteLine("NewStrikeoutAnnotation(): Failed to append strikeout annotation to the document.");
40+
else
41+
{
42+
Console.WriteLine("NewStrikeoutAnnotation(): annotations '{0}' added to the document '{1}.", helper.config.NEW_SO_ANNOTATION_TEXT, documentName);
43+
await helper.DownloadFile(documentName, outputName, "add_strikeout_annotation_");
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)