Skip to content

PDFCLOUD-5050: added snippets for Annotations #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Uses-Cases/Annotations/Annotations.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Aspose.Pdf.Cloud.Sdk\Aspose.Pdf.Cloud.Sdk.csproj" />
</ItemGroup>

</Project>
111 changes: 111 additions & 0 deletions Uses-Cases/Annotations/AnnotationsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using Aspose.Pdf.Cloud.Sdk.Api;
using Aspose.Pdf.Cloud.Sdk.Model;
using Newtonsoft.Json;

namespace Annotations
{
public class ConfigParams
{
public string CrdentialPath { get; } = "..\\credentials.json ";
public string LOCAL_FOLDER { get; } = "C:\\Samples";
public string REMOTE_TEMP_FOLDER { get; } = "TempPdfCloud";
public string PDF_DOCUMENT { get; } = "sample.pdf";
public string PDF_OUTPUT { get; } = "output_sample.pdf";
public int PAGE_NUMBER { get; } = 1;

public string ANNOTATION_ID { get; } = "GE5TAOZTHA2CYMRZGUWDIMBZFQZTEMA";

public string NEW_HL_ANNOTATION_TEXT { get; } = "NEW HIGHLIGHT TEXT ANNOTATION";
public string NEW_HL_ANNOTATION_DESCRIPTION { get; } = "This is a sample highlight annotation";
public string NEW_HL_ANNOTATION_SUBJECT { get; } = "Highlight Text Box Subject";
public string NEW_HL_ANNOTATION_CONTENTS { get; } = "Highlight annotation sample contents";

public string NEW_SO_ANNOTATION_TEXT { get; } = "NEW STRIKEOUT TEXT ANNOTATION";
public string NEW_SO_ANNOTATION_DESCRIPTION { get; } = "This is a sample strikeout annotation";
public string NEW_SO_ANNOTATION_SUBJECT { get; } = "Strikeout Text Box Subject";
public string NEW_SO_ANNOTATION_CONTENTS { get; } = "Strikeout annotation sample contents";

public string NEW_UL_ANNOTATION_TEXT { get; } = "NEW UNDERLINE TEXT ANNOTATION";
public string NEW_UL_ANNOTATION_DESCRIPTION { get; } = "This is a sample underline annotation";
public string NEW_UL_ANNOTATION_SUBJECT { get; } = "Underline Text Box Subject";
public string NEW_UL_ANNOTATION_CONTENTS { get; } = "Underline annotation sample contents";

public string NEW_FT_ANNOTATION_TEXT { get; } = "NEW FREE TEXT ANNOTATION";
public string NEW_FT_ANNOTATION_DESCRIPTION { get; } = "This is a sample annotation";
public string NEW_FT_ANNOTATION_SUBJECT { get; } = "Free Text Box Subject";
public string NEW_FT_ANNOTATION_CONTENTS { get; } = "Free Text annotation sample contents";

public string REPLACED_CONTENT { get; } = "This is a replaced sample annotation";
}

public class Credentials
{
public string Id { get; set; }
public string Key { get; set; }
}

public class AnnotationsHelper
{
public PdfApi pdfApi { get; private set; }
public ConfigParams config { get; private set; }

public AnnotationsHelper()
{
config = new ConfigParams();
string jsCredText = File.ReadAllText(config.CrdentialPath);
Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText);
pdfApi = new PdfApi(cred.Key, cred.Id);
}

public async Task UploadFile(string fileName)
{
using (var file = File.OpenRead(Path.Combine(config.LOCAL_FOLDER, fileName)))
{
FilesUploadResult response = await pdfApi.UploadFileAsync(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName), file);
if (response == null)
Console.WriteLine("UploadFile(): Unexpected error - no response!");
else if (response.Errors != null && response.Errors.Count > 0)
foreach (var error in response.Errors)
Console.WriteLine("UploadFile(): {0} -> {1}", [error.Code, error.Message]);
else
Console.WriteLine("UploadFile(): File '{0}' successfully uploaded.", fileName);
}
}

public async Task DownloadFile(string fileName, string outputName, string outputPrefix)
{
Stream stream = pdfApi.DownloadFile(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName));
using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, outputPrefix + outputName));
stream.Position = 0;
await stream.CopyToAsync(fileStream);
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + outputName);
}

public async Task DeletePopupAnnotationsAsync(string documentName, string parentAnnotation, string remoteFolder)
{
// Delete popup annotations for typed parent annotation in the page in the PDF document.
PopupAnnotationsResponse response = await pdfApi.GetDocumentPopupAnnotationsAsync(documentName, folder: config.REMOTE_TEMP_FOLDER);
if (response == null)
Console.WriteLine("DeletePopupAnnotations(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("DeletePopupAnnotations(): Unexpected error '{0}'!", response.Status);
else
{
foreach (PopupAnnotationWithParent annotation in response.Annotations.List)
{
if (annotation.Parent.Id == parentAnnotation)
{
string popupContents = annotation.Contents;
AsposeResponse response2 = await pdfApi.DeleteAnnotationAsync(documentName, annotation.Id, folder: remoteFolder);
if (response2 == null)
Console.WriteLine("DeletePopupAnnotations(): Unexpected error!");
else if (response2.Code < 200 || response2.Code > 299)
Console.WriteLine("DeletePopupAnnotations(): Failed to delete popup annotation '{0}' from the document.", popupContents);
else
Console.WriteLine("DeletePopupAnnotations(): popup annotations '{0}' with '{1}' contents deleted from the document '{1}.", annotation.Id, popupContents, documentName);
}
}
}
}
}
}
22 changes: 22 additions & 0 deletions Uses-Cases/Annotations/DeletePageAnnotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class DeletePageAnnotations
{
public async static Task MakeDeleteAsync(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
{
// Delete annotation from the PDF document.
await helper.UploadFile(documentName);
AsposeResponse response = await helper.pdfApi.DeletePageAnnotationsAsync(documentName, pageNumber, folder: remoteFolder);
if (response == null)
Console.WriteLine("DeletePageAnnotations(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("DeletePageAnnotations(): Failed to delete annotation from the document.");
else {
Console.WriteLine("DeletePageAnnotations(): annotations on page '{0}' deleted from the document '{1}.", pageNumber, documentName);
await helper.DownloadFile(documentName, outputName, "del_page_annotations_");
}
}
}
}
24 changes: 24 additions & 0 deletions Uses-Cases/Annotations/DeleteTextAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class DeleteTextAnnotation
{
public async static Task MakeDeleteAsync(AnnotationsHelper helper, string documentName, string annotationId, string outputName, string remoteFolder)
{
// Delete annotation from the PDF document.
await helper.UploadFile(documentName);
AsposeResponse response = await helper.pdfApi.DeleteAnnotationAsync(documentName, annotationId, folder: remoteFolder);
if (response == null)
Console.WriteLine("DeleteTextAnnotation(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("DeleteTextAnnotation(): Failed to delete annotation from the document.");
else
{
await helper.DeletePopupAnnotationsAsync(documentName, annotationId, remoteFolder);
Console.WriteLine("DeleteTextAnnotation(): annotations '{0}' deleted from the document '{1}.", annotationId, documentName);
await helper.DownloadFile(documentName, outputName, "del_text_annotations_");
}
}
}
}
23 changes: 23 additions & 0 deletions Uses-Cases/Annotations/GetAnnotationById.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class GetAnnotationById
{
public static async Task RequestTextAnnotationAsync(AnnotationsHelper helper, string documentName, string annotationId, string remoteFolder)
{
// Get annotation by Id in the PDF document.
await helper.UploadFile(documentName);

string annotationResult = string.Empty;
TextAnnotationResponse response = await helper.pdfApi.GetTextAnnotationAsync(documentName, annotationId, folder: remoteFolder);

if (response == null)
Console.WriteLine("RequestTextAnnotationAsync(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("RequestTextAnnotationAsync(): Failed to request text annotation from the document.");
else
Console.WriteLine("RequestTextAnnotationAsync(): annotation '{0}' with '{1}' contents successfully found in the document '{2}.", response.Annotation.Id, response.Annotation.Contents, documentName);
}
}
}
34 changes: 34 additions & 0 deletions Uses-Cases/Annotations/GetAnnotations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class GetAnnotations
{
public static async Task<string> RequestAnnotationsOnPageAsync(AnnotationsHelper helper, string documentName, int pageNumber, string remoteFolder)
{
// Get annotations from the page in the PDF document.
await helper.UploadFile(documentName);

string annotationResult = string.Empty;
AnnotationsInfoResponse response = await helper.pdfApi.GetPageAnnotationsAsync(documentName, pageNumber, folder: remoteFolder);

if (response == null)
Console.WriteLine("RequestAnnotationsOnPageAsync(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("RequestAnnotationsOnPageAsync(): Failed to request annotations from the document.");
else
{
foreach (AnnotationInfo annotation in response.Annotations.List)
{
Console.WriteLine("RequestAnnotationsOnPageAsync(): annotation '{0}' with '{1}' contents get from the '{2}' page of the document '{3}.", [annotation.Id, annotation.Contents, pageNumber, documentName]);
if (string.IsNullOrEmpty(annotationResult) &&
annotation.AnnotationType == AnnotationType.Text)
{
annotationResult = annotation.Id;
}
}
}
return annotationResult;
}
}
}
47 changes: 47 additions & 0 deletions Uses-Cases/Annotations/NewFreetextAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class NewFreetextAnnotation
{
public static async Task Append(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
{
await helper.UploadFile(documentName);

List<FreeTextAnnotation> annotations = new List<FreeTextAnnotation>
{
new FreeTextAnnotation(
Name: "Freetext_NEW_Annotation",
Rect: new Rectangle(100,350, 450,400),
Flags: new List<AnnotationFlags>() { AnnotationFlags.Default },
HorizontalAlignment: HorizontalAlignment.Left,
Intent: FreeTextIntent.FreeTextTypeWriter,
Justification: Justification.Center,
RichText: helper.config.NEW_FT_ANNOTATION_TEXT,
Subject: helper.config.NEW_FT_ANNOTATION_SUBJECT,
Contents: helper.config.NEW_FT_ANNOTATION_CONTENTS,
Title: helper.config.NEW_FT_ANNOTATION_DESCRIPTION,
ZIndex: 1,
TextStyle: new TextStyle(
FontSize: 20,
Font: "Arial",
ForegroundColor: new Color( A: 0xFF, R: 0x00, G: 0xFF, B: 0x00),
BackgroundColor: new Color( A: 0xFF, R: 0xFF, G: 0x00, B: 0x00)
),
Modified: "03/27/2025 00:00:00.000 AM"
)
};
AsposeResponse response = await helper.pdfApi.PostPageFreeTextAnnotationsAsync(documentName, pageNumber, annotations, folder: remoteFolder);

if (response == null)
Console.WriteLine("NewFreetextAnnotation(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("NewFreetextAnnotation(): Failed to append text annotation to the document.");
else
{
Console.WriteLine("NewFreetextAnnotation(): annotations '{0}' added to the document '{1}.", helper.config.NEW_FT_ANNOTATION_TEXT, documentName);
await helper.DownloadFile(documentName, outputName, "add_text_annotation_");
}
}
}
}
47 changes: 47 additions & 0 deletions Uses-Cases/Annotations/NewHighlightAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class NewHighlightAnnotation
{
public static async Task Append(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
{
await helper.UploadFile(documentName);

List<HighlightAnnotation> annotations = new List<HighlightAnnotation>
{
new HighlightAnnotation(
Name: "Highlight_NEW_Annotation",
Rect: new Rectangle(100,350, 450,400),
Flags: new List<AnnotationFlags>() { AnnotationFlags.Default },
HorizontalAlignment: HorizontalAlignment.Left,
VerticalAlignment: VerticalAlignment.Top,
RichText: helper.config.NEW_HL_ANNOTATION_TEXT,
Subject: helper.config.NEW_HL_ANNOTATION_SUBJECT,
Contents: helper.config.NEW_HL_ANNOTATION_CONTENTS,
Title: helper.config.NEW_HL_ANNOTATION_DESCRIPTION,
ZIndex: 1,
Color: new Color(A: 0xFF, R: 0x00, G: 0xFF, B: 0x00),
QuadPoints: new List<Point>() {
new Point(X: 10, Y: 10),
new Point(X: 20, Y: 10),
new Point(X: 10, Y: 20),
new Point(X: 10, Y: 10)
},
Modified: "03/27/2025 00:00:00.000 AM"
)
};
AsposeResponse response = await helper.pdfApi.PostPageHighlightAnnotationsAsync(documentName, pageNumber, annotations, folder: remoteFolder);

if (response == null)
Console.WriteLine("NewHighlightAnnotation(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("NewHighlightAnnotation(): Failed to append highlight annotation to the document.");
else
{
Console.WriteLine("NewHighlightAnnotation(): annotations '{0}' added to the document '{1}.", helper.config.NEW_HL_ANNOTATION_TEXT, documentName);
await helper.DownloadFile(documentName, outputName, "add_highlight_annotation_");
}
}
}
}
47 changes: 47 additions & 0 deletions Uses-Cases/Annotations/NewStrikeoutAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Aspose.Pdf.Cloud.Sdk.Model;

namespace Annotations
{
public class NewStrikeoutAnnotation
{
public static async Task Append(AnnotationsHelper helper, string documentName, int pageNumber, string outputName, string remoteFolder)
{
await helper.UploadFile(documentName);

List<StrikeOutAnnotation> annotations = new List<StrikeOutAnnotation>
{
new StrikeOutAnnotation(
Name: "Strikeout_NEW_Annotation",
Rect: new Rectangle(100,350, 450,400),
Flags: new List<AnnotationFlags>() { AnnotationFlags.Default },
HorizontalAlignment: HorizontalAlignment.Left,
VerticalAlignment: VerticalAlignment.Top,
RichText: helper.config.NEW_SO_ANNOTATION_TEXT,
Subject: helper.config.NEW_SO_ANNOTATION_SUBJECT,
Contents: helper.config.NEW_SO_ANNOTATION_CONTENTS,
Title: helper.config.NEW_SO_ANNOTATION_DESCRIPTION,
ZIndex: 1,
Color: new Color(A: 0xFF, R: 0x00, G: 0xFF, B: 0x00),
QuadPoints: new List<Point>() {
new Point(X: 10, Y: 10),
new Point(X: 20, Y: 10),
new Point(X: 10, Y: 20),
new Point(X: 10, Y: 10)
},
Modified: "03/27/2025 00:00:00.000 AM"
)
};
AsposeResponse response = await helper.pdfApi.PostPageStrikeOutAnnotationsAsync(documentName, pageNumber, annotations, folder: remoteFolder);

if (response == null)
Console.WriteLine("NewStrikeoutAnnotation(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("NewStrikeoutAnnotation(): Failed to append strikeout annotation to the document.");
else
{
Console.WriteLine("NewStrikeoutAnnotation(): annotations '{0}' added to the document '{1}.", helper.config.NEW_SO_ANNOTATION_TEXT, documentName);
await helper.DownloadFile(documentName, outputName, "add_strikeout_annotation_");
}
}
}
}
Loading