Skip to content

Commit 227d9c0

Browse files
authored
Merge pull request #130 from aspose-pdf-cloud/Dmitriy-Xawstov-patch-1
PDFCLOUD-5061: added snippets Headers/Footers
2 parents 701e07f + 5e8018c commit 227d9c0

7 files changed

+252
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using System.IO;
3+
4+
namespace HeadersFooters
5+
{
6+
public class HeadersFootersAddTextFooter
7+
{
8+
public static async Task Append(HeadersFootersHelper helper, string documentName, string outputName, string footerText, int startPage, int endPage, string remoteFolder)
9+
{
10+
await helper.UploadFile(documentName);
11+
12+
TextFooter footer = new TextFooter(
13+
Background: true,
14+
LeftMargin: 1,
15+
RightMargin: 2,
16+
HorizontalAlignment: HorizontalAlignment.Center,
17+
Opacity: 1,
18+
Rotate: Rotation.None,
19+
RotateAngle: 15,
20+
XIndent: 0,
21+
YIndent: 0,
22+
Zoom: 1,
23+
Value: footerText
24+
);
25+
26+
var response = await helper.pdfApi.PostDocumentTextFooterAsync(documentName, footer, startPage, endPage, folder: remoteFolder);
27+
28+
if (response == null)
29+
Console.WriteLine("HeadersFootersAddTextFooter(): Unexpected error!");
30+
else if (response.Code < 200 || response.Code > 299)
31+
Console.WriteLine("HeadersFootersAddTextFooter(): Failed to append text footer to the page of document.");
32+
else
33+
{
34+
Console.WriteLine("HeadersFootersAddTextFooter(): text '{0}' appended as footer to the document '{1}.", footerText, documentName);
35+
await helper.DownloadFile(documentName, outputName, "add_text_footer_");
36+
}
37+
}
38+
}
39+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using System.IO;
3+
4+
namespace HeadersFooters
5+
{
6+
public class HeadersFootersAddImageFooter
7+
{
8+
public static async Task Append(HeadersFootersHelper helper, string documentName, string outputName, string imageFileName, int startPage, int endPage, string remoteFolder)
9+
{
10+
await helper.UploadFile(documentName);
11+
await helper.UploadFile(imageFileName);
12+
13+
ImageFooter footer = new ImageFooter(
14+
Background: true,
15+
LeftMargin: 1,
16+
RightMargin: 2,
17+
HorizontalAlignment: HorizontalAlignment.Center,
18+
Opacity: 1,
19+
Rotate: Rotation.None,
20+
RotateAngle: 0,
21+
XIndent: 0,
22+
YIndent: 0,
23+
Zoom: 1,
24+
Width: 24,
25+
Height: 24,
26+
FileName: Path.Combine(remoteFolder, imageFileName)
27+
);
28+
29+
var response = await helper.pdfApi.PostDocumentImageFooterAsync(documentName, footer, startPage, endPage, folder: remoteFolder);
30+
31+
if (response == null)
32+
Console.WriteLine("HeadersFootersAddImageFooter(): Unexpected error!");
33+
else if (response.Code < 200 || response.Code > 299)
34+
Console.WriteLine("HeadersFootersAddImageFooter(): Failed to append image footer to the page of document.");
35+
else
36+
{
37+
Console.WriteLine("HeadersFootersAddImageFooter(): image '{0}' appended as footer to the document '{1}.", imageFileName, documentName);
38+
await helper.DownloadFile(documentName, outputName, "add_image_footer_");
39+
}
40+
}
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using System.IO;
3+
4+
namespace HeadersFooters
5+
{
6+
public class HeadersFootersAddImageHeader
7+
{
8+
public static async Task Append(HeadersFootersHelper helper, string documentName, string outputName, string imageFileName, int startPage, int endPage, string remoteFolder)
9+
{
10+
await helper.UploadFile(documentName);
11+
await helper.UploadFile(imageFileName);
12+
13+
ImageHeader header = new ImageHeader(
14+
Background: true,
15+
LeftMargin: 1,
16+
RightMargin: 2,
17+
HorizontalAlignment: HorizontalAlignment.Center,
18+
Opacity: 1,
19+
Rotate: Rotation.None,
20+
RotateAngle: 0,
21+
XIndent: 0,
22+
YIndent: 0,
23+
Zoom: 1,
24+
Width: 24,
25+
Height: 24,
26+
FileName: Path.Combine(remoteFolder, imageFileName)
27+
);
28+
29+
var response = await helper.pdfApi.PostDocumentImageHeaderAsync(documentName, header, startPage, endPage, folder: remoteFolder);
30+
31+
if (response == null)
32+
Console.WriteLine("HeadersFootersAddImageHeader(): Unexpected error!");
33+
else if (response.Code < 200 || response.Code > 299)
34+
Console.WriteLine("HeadersFootersAddImageHeader(): Failed to append image header to the page of document.");
35+
else
36+
{
37+
Console.WriteLine("HeadersFootersAddImageHeader(): image '{0}' appended as header to the document '{1}.", imageFileName, documentName);
38+
await helper.DownloadFile(documentName, outputName, "add_image_header_");
39+
}
40+
}
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
using System.IO;
3+
4+
namespace HeadersFooters
5+
{
6+
public class HeadersFootersAddTextHeader
7+
{
8+
public static async Task Append(HeadersFootersHelper helper, string documentName, string outputName, string footerText, int startPage, int endPage, string remoteFolder)
9+
{
10+
await helper.UploadFile(documentName);
11+
12+
TextHeader footer = new TextHeader(
13+
Background: true,
14+
LeftMargin: 1,
15+
RightMargin: 2,
16+
HorizontalAlignment: HorizontalAlignment.Center,
17+
Opacity: 1,
18+
Rotate: Rotation.None,
19+
RotateAngle: 15,
20+
XIndent: 0,
21+
YIndent: 0,
22+
Zoom: 1,
23+
Value: footerText
24+
);
25+
26+
var response = await helper.pdfApi.PostDocumentTextHeaderAsync(documentName, footer, startPage, endPage, folder: remoteFolder);
27+
28+
if (response == null)
29+
Console.WriteLine("HeadersFootersAddTextHeader(): Unexpected error!");
30+
else if (response.Code < 200 || response.Code > 299)
31+
Console.WriteLine("HeadersFootersAddTextHeader(): Failed to append text header to the page of document.");
32+
else
33+
{
34+
Console.WriteLine("HeadersFootersAddTextFooter(): text '{0}' appended as header to the document '{1}.", footerText, documentName);
35+
await helper.DownloadFile(documentName, outputName, "add_text_header_");
36+
}
37+
}
38+
}
39+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Aspose.Pdf.Cloud.Sdk.Api;
2+
using Aspose.Pdf.Cloud.Sdk.Model;
3+
using Newtonsoft.Json;
4+
5+
namespace HeadersFooters
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+
15+
public int PAGE_NUMBER { get; } = 2;
16+
17+
public string IMAGE_FILE { get; } = "sample.png";
18+
19+
public string TEXT_FOOTER { get; } = "NEW APPENDED TEXT FOOTER";
20+
public string TEXT_HEADER { get; } = "NEW APPENDED TEXT HEADER";
21+
}
22+
23+
public class Credentials
24+
{
25+
public string Id { get; set; }
26+
public string Key { get; set; }
27+
}
28+
29+
public class HeadersFootersHelper
30+
{
31+
public PdfApi pdfApi { get; private set; }
32+
public ConfigParams config { get; private set; }
33+
34+
public HeadersFootersHelper()
35+
{
36+
config = new ConfigParams();
37+
string jsCredText = File.ReadAllText(config.CrdentialPath);
38+
Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText);
39+
pdfApi = new PdfApi(cred.Key, cred.Id);
40+
}
41+
42+
public async Task UploadFile(string fileName)
43+
{
44+
using (var file = File.OpenRead(Path.Combine(config.LOCAL_FOLDER, fileName)))
45+
{
46+
FilesUploadResult response = await pdfApi.UploadFileAsync(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName), file);
47+
if (response == null)
48+
Console.WriteLine("UploadFile(): Unexpected error - no response!");
49+
else if (response.Errors != null && response.Errors.Count > 0)
50+
foreach (var error in response.Errors)
51+
Console.WriteLine("UploadFile(): {0} -> {1}", [error.Code, error.Message]);
52+
else
53+
Console.WriteLine("UploadFile(): File '{0}' successfully uploaded.", fileName);
54+
}
55+
}
56+
57+
public async Task DownloadFile(string fileName, string outputName, string outputPrefix)
58+
{
59+
Stream stream = pdfApi.DownloadFile(Path.Combine(config.REMOTE_TEMP_FOLDER, fileName));
60+
using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, outputPrefix + outputName));
61+
stream.Position = 0;
62+
await stream.CopyToAsync(fileStream);
63+
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", outputPrefix + outputName);
64+
}
65+
}
66+
}

Uses-Cases/HeadersFooters/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using HeadersFooters;
2+
3+
HeadersFootersHelper helper = new HeadersFootersHelper();
4+
ConfigParams config = helper.config;
5+
6+
await HeadersFootersAddImageFooter.Append(helper, config.PDF_DOCUMENT, config.PDF_OUTPUT, config.IMAGE_FILE, config.PAGE_NUMBER, config.PAGE_NUMBER + 1, config.REMOTE_TEMP_FOLDER);
7+
await HeadersFootersAddTextFooter.Append(helper, config.PDF_DOCUMENT, config.PDF_OUTPUT, config.TEXT_FOOTER, config.PAGE_NUMBER, config.PAGE_NUMBER + 1, config.REMOTE_TEMP_FOLDER);
8+
9+
await HeadersFootersAddImageHeader.Append(helper, config.PDF_DOCUMENT, config.PDF_OUTPUT, config.IMAGE_FILE, config.PAGE_NUMBER, config.PAGE_NUMBER + 1, config.REMOTE_TEMP_FOLDER);
10+
await HeadersFootersAddTextHeader.Append(helper, config.PDF_DOCUMENT, config.PDF_OUTPUT, config.TEXT_FOOTER, config.PAGE_NUMBER, config.PAGE_NUMBER + 1, config.REMOTE_TEMP_FOLDER);

0 commit comments

Comments
 (0)