Skip to content

Commit 9dd239c

Browse files
PDFCLOUD-4997: added snippets Create documents
1 parent cd6d815 commit 9dd239c

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Aspose.Pdf.Cloud.Sdk.Api;
2+
using Aspose.Pdf.Cloud.Sdk.Model;
3+
using RestSharp;
4+
5+
namespace CreateDocument
6+
{
7+
public class ClreatePdfDocumentSimple
8+
{
9+
public ClreatePdfDocumentSimple()
10+
{
11+
string LOCAL_FOLDER = "c:\\Samples";
12+
string PDF_DOCUMENT = "output_created_simple.pdf";
13+
string REMOTE_FOLDER = "TempPdfCloud";
14+
15+
string AppSID = "*************";
16+
string AppKey = "*************";
17+
18+
PdfApi pdfApi = new PdfApi(AppKey, AppSID);
19+
20+
DocumentResponse response = pdfApi.PutCreateDocument(PDF_DOCUMENT, folder: REMOTE_FOLDER);
21+
if (response.Code != 200)
22+
Console.WriteLine("ClreatePdfDocumentSimple(): Unexpected error: {0}", response.Messages[0]);
23+
else
24+
{
25+
Console.WriteLine("ClreatePdfDocumentSimple():Document '{0}' successfully created.", PDF_DOCUMENT);
26+
Stream stream = pdfApi.DownloadFile(Path.Combine(REMOTE_FOLDER, PDF_DOCUMENT));
27+
using var fileStream = File.Create(Path.Combine(LOCAL_FOLDER, PDF_DOCUMENT));
28+
stream.Position = 0;
29+
stream.CopyTo(fileStream);
30+
Console.WriteLine("ClreatePdfDocumentSimple():Document '{0}' successfully downloaded.", PDF_DOCUMENT);
31+
}
32+
}
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Aspose.Pdf.Cloud.Sdk.Api;
2+
using Newtonsoft.Json;
3+
4+
namespace CreateDocument
5+
{
6+
public class ConfigParams
7+
{
8+
public string CrdentialPath { get; } = ".\\credentials.json";
9+
public string LOCAL_FOLDER { get; } = "C:\\Samples";
10+
public string TEMP_FOLDER { get; } = "TempPdfCloud";
11+
public string LOCAL_RESULT_DOCUMENT_NAME { get; } = "output_sample.pdf";
12+
public int PAGE_WIDTH { get; } = 590;
13+
public int PAGE_HEIGHT { get; } = 894;
14+
public int PAGES_COUNT { get; } = 5;
15+
}
16+
17+
public class Credentials
18+
{
19+
public string Id { get; set; }
20+
public string Key { get; set; }
21+
}
22+
23+
public class CrateDocumentHelper
24+
{
25+
public PdfApi pdfApi { get; private set; }
26+
public ConfigParams config { get; private set; }
27+
28+
public CrateDocumentHelper()
29+
{
30+
config = new ConfigParams();
31+
string jsCredText = File.ReadAllText(config.CrdentialPath);
32+
Credentials cred = JsonConvert.DeserializeObject<Credentials>(jsCredText);
33+
pdfApi = new PdfApi(cred.Key, cred.Id);
34+
}
35+
36+
public async void DownloadFile(string filename)
37+
{
38+
Stream stream = pdfApi.DownloadFile(Path.Combine(config.TEMP_FOLDER, filename));
39+
using var fileStream = File.Create(Path.Combine(config.LOCAL_FOLDER, config.LOCAL_RESULT_DOCUMENT_NAME));
40+
stream.Position = 0;
41+
await stream.CopyToAsync(fileStream);
42+
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", filename);
43+
}
44+
}
45+
}
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Aspose.Pdf.Cloud.Sdk.Model;
2+
3+
4+
namespace CreateDocument
5+
{
6+
public class CreatePdfDocument
7+
{
8+
public CreatePdfDocument(CrateDocumentHelper helper)
9+
{
10+
DocumentProperties docProps = new DocumentProperties(
11+
List: new List<DocumentProperty>() {
12+
new DocumentProperty(Name: "prop1", Value: "Value1", BuiltIn: false)
13+
}
14+
);
15+
16+
DisplayProperties dispProps = new DisplayProperties()
17+
{
18+
CenterWindow = true,
19+
HideMenuBar = true,
20+
Direction = Direction.L2R,
21+
DisplayDocTitle = true,
22+
HideToolBar = true,
23+
HideWindowUI = true,
24+
NonFullScreenPageMode = PageMode.UseThumbs,
25+
PageLayout = PageLayout.TwoPageLeft,
26+
PageMode = PageMode.UseThumbs
27+
};
28+
29+
DefaultPageConfig pageConfig = new DefaultPageConfig(helper.config.PAGE_HEIGHT, helper.config.PAGE_WIDTH);
30+
31+
DocumentConfig document_config = new DocumentConfig(
32+
DocumentProperties: docProps,
33+
DisplayProperties: dispProps,
34+
DefaultPageConfig: pageConfig,
35+
PagesCount: helper.config.PAGES_COUNT
36+
);
37+
38+
DocumentResponse response = helper.pdfApi.PostCreateDocument(helper.config.LOCAL_RESULT_DOCUMENT_NAME, document_config, folder: helper.config.TEMP_FOLDER);
39+
40+
if (response != null && response.Code == 200)
41+
Console.WriteLine("Document #{0} created.", helper.config.LOCAL_RESULT_DOCUMENT_NAME);
42+
else
43+
Console.WriteLine("Unexpected error!!!");
44+
}
45+
}
46+
}

Uses-Cases/CreateDocument/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using CreateDocument;
2+
3+
CrateDocumentHelper helper = new CrateDocumentHelper ();
4+
CreatePdfDocument pdfCreatror = new CreatePdfDocument(helper);
5+
6+
helper.DownloadFile(helper.config.LOCAL_RESULT_DOCUMENT_NAME);
7+
8+
Console.WriteLine(helper.config.LOCAL_RESULT_DOCUMENT_NAME);
9+
10+
ClreatePdfDocumentSimple simpleDoc = new ClreatePdfDocumentSimple();

0 commit comments

Comments
 (0)