/
Program.cs
107 lines (96 loc) · 4.04 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it. If you have received this file from a source other than Adobe,
* then your use, modification, or distribution of it requires the prior
* written permission of Adobe.
*/
using Adobe.PDFServicesSDK;
using Adobe.PDFServicesSDK.auth;
using Adobe.PDFServicesSDK.exception;
using Adobe.PDFServicesSDK.io;
using Adobe.PDFServicesSDK.options.exportpdf;
using Adobe.PDFServicesSDK.pdfops;
using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.IO;
using System.Reflection;
/// <summary>
/// This sample illustrates how to specify the region for the SDK. This enables the client to configure the SDK to
/// process the documents in the specified region.
/// <para>
/// Refer to README.md for instructions on how to run the samples.
/// </para>
/// </summary>
namespace ExportPDFWithSpecifiedRegion
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main()
{
//Configure the logging
ConfigureLogging();
try
{
// Initial setup, create credentials instance.
Credentials credentials = Credentials.ServicePrincipalCredentialsBuilder()
.WithClientId(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_ID"))
.WithClientSecret(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_SECRET"))
.Build();
// Create client config instance with the specified region.
ClientConfig clientConfig = ClientConfig.ConfigBuilder()
.SetRegion(Region.EU)
.Build();
// Create an ExecutionContext using credentials and create a new operation instance.
ExecutionContext executionContext = ExecutionContext.Create(credentials, clientConfig);
ExportPDFOperation exportPdfOperation = ExportPDFOperation.CreateNew(ExportPDFTargetFormat.DOCX);
// Set operation input from a source file.
FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"exportPdfInput.pdf");
exportPdfOperation.SetInput(sourceFileRef);
// Execute the operation.
FileRef result = exportPdfOperation.Execute(executionContext);
//Generating a file name
String outputFilePath = CreateOutputFilePath();
// Save the result to the specified location.
result.SaveAs(Directory.GetCurrentDirectory() + outputFilePath);
}
catch (ServiceUsageException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (ServiceApiException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (SDKException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (IOException ex)
{
log.Error("Exception encountered while executing operation", ex);
}
catch (Exception ex)
{
log.Error("Exception encountered while executing operation", ex);
}
}
static void ConfigureLogging()
{
ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
}
// Generates a string containing a directory structure and file name for the output file.
public static string CreateOutputFilePath()
{
String timeStamp = DateTime.Now.ToString("yyyy'-'MM'-'dd'T'HH'-'mm'-'ss");
return ("/output/export" + timeStamp + ".docx");
}
}
}