static void Main() { //Configure the logging ConfigureLogging(); try { /* * Initial setup, create credentials instance. * Replace the values of CLIENT_ID, CLIENT_SECRET, ORGANIZATION_ID and ACCOUNT_ID with their corresponding values * present in the pdfservices-api-credentials.json file and PRIVATE_KEY_FILE_CONTENTS with contents of private.key file * within the zip file which must have been downloaded at the end of Getting the Credentials workflow. */ Credentials credentials = new ServiceAccountCredentials.Builder() .WithClientId("CLIENT_ID") .WithClientSecret("CLIENT_SECRET") .WithPrivateKey("PRIVATE_KEY_FILE_CONTENTS") .WithOrganizationId("ORGANIZATION_ID") .WithAccountId("ACCOUNT_ID") .Build(); //Create an ExecutionContext using credentials and create a new operation instance. ExecutionContext executionContext = ExecutionContext.Create(credentials); CreatePDFOperation createPdfOperation = CreatePDFOperation.CreateNew(); // Set operation input from a source file. FileRef source = FileRef.CreateFromLocalFile(@"createPdfInput.docx"); createPdfOperation.SetInput(source); // Execute the operation. FileRef result = createPdfOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPdfOutput.pdf"); } 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); } }
/// <summary> /// Converts zip-file or page located by provided URL to PDF. /// </summary> public void ConvertFileToPdf() { try { // Initial setup, create credentials instance. Credentials credentials = Credentials.ServiceAccountCredentialsBuilder() .FromFile(Directory.GetCurrentDirectory() + "/dc-services-sdk-credentials.json") .Build(); // Create an ExecutionContext using credentials and create a new operation instance. Adobe.DocumentCloud.Services.ExecutionContext executionContext = Adobe.DocumentCloud.Services.ExecutionContext.Create(credentials); CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.CreateNew(); FileRef source = GetSource(); // Set operation input from a source file. htmlToPDFOperation.SetInput(source); // Provide any custom configuration options for the operation. SetCustomOptions(htmlToPDFOperation); // Execute the operation. FileRef result = htmlToPDFOperation.Execute(executionContext); // Save the result to the specified location. File.Delete(outputFileName); result.SaveAs(outputFileName); } catch (ServiceUsageException ex) { log.Error("Exception encountered while executing operation", ex); throw; } catch (ServiceApiException ex) { log.Error("Exception encountered while executing operation", ex); throw; } catch (SDKException ex) { log.Error("Exception encountered while executing operation", ex); throw; } catch (IOException ex) { log.Error("Exception encountered while executing operation", ex); throw; } catch (Exception ex) { log.Error("Exception encountered while executing operation", ex); throw; } }
static void Main() { //Configure the logging ConfigureLogging(); Stream inputStream = File.OpenRead(@"createPdfInput.docx"); try { // Initial setup, create credentials instance. Credentials credentials = Credentials.ServiceAccountCredentialsBuilder() .FromFile(Directory.GetCurrentDirectory() + "/pdftools-api-credentials.json") .Build(); //Create an ExecutionContext using credentials and create a new operation instance. ExecutionContext executionContext = ExecutionContext.Create(credentials); CreatePDFOperation createPdfOperation = CreatePDFOperation.CreateNew(); // Set operation input from the source stream by specifying the stream media type. FileRef source = FileRef.CreateFromStream(inputStream, CreatePDFOperation.SupportedSourceFormat.DOCX.GetMediaType()); createPdfOperation.SetInput(source); // Execute the operation. FileRef result = createPdfOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPDFFromDOCXStream.pdf"); } 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); } finally { inputStream.Close(); } }
/// <summary> /// Sets any custom options for the operation. /// </summary> /// <param name="createPdfOperation">operation instance for which the options are provided.</param> private static void SetCustomOptions(CreatePDFOperation createPdfOperation) { // Select the documentLanguage for input file. SupportedDocumentLanguage documentLanguage = SupportedDocumentLanguage.EN_US; // Set the desired Word-to-PDF conversion options. CreatePDFOptions createPDFOptions = CreatePDFOptions.WordOptionsBuilder() .WithDocumentLanguage(documentLanguage) .Build(); createPdfOperation.SetOptions(createPDFOptions); }
static void Main() { //Configure the logging ConfigureLogging(); try { // Initial setup, create credentials instance. Credentials credentials = Credentials.ServiceAccountCredentialsBuilder() .FromFile(Directory.GetCurrentDirectory() + "/pdftools-api-credentials.json") .Build(); // Create client config instance with custom time-outs. ClientConfig clientConfig = ClientConfig.ConfigBuilder() .WithTimeout(40000) .WithReadWriteTimeout(10000) .Build(); //Create an ExecutionContext using credentials and create a new operation instance. ExecutionContext executionContext = ExecutionContext.Create(credentials, clientConfig); CreatePDFOperation createPdfOperation = CreatePDFOperation.CreateNew(); // Set operation input from a source file. FileRef source = FileRef.CreateFromLocalFile(@"createPdfInput.docx"); createPdfOperation.SetInput(source); // Execute the operation. FileRef result = createPdfOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPdfOutput.pdf"); } 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 Main() { //Configure the logging ConfigureLogging(); try { // Initial setup, create credentials instance. Credentials credentials = Credentials.ServiceAccountCredentialsBuilder() .FromFile(Directory.GetCurrentDirectory() + "/pdftools-api-credentials.json") .Build(); //Create an ExecutionContext using credentials and create a new operation instance. ExecutionContext executionContext = ExecutionContext.Create(credentials); CreatePDFOperation createPdfOperation = CreatePDFOperation.CreateNew(); // Set operation input from a source file. FileRef source = FileRef.CreateFromLocalFile(@"createPdfInput.docx"); createPdfOperation.SetInput(source); // Execute the operation. FileRef result = createPdfOperation.Execute(executionContext); // Create an OutputStream and save the result to the stream. using (Stream outputStream = PrepareOutputStream()) { result.SaveAs(outputStream); log.Info("Moving this temporary file to output stream."); } } 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 Main() { //Configure the logging ConfigureLogging(); try { // Initial setup, create credentials instance. Credentials credentials = Credentials.ServiceAccountCredentialsBuilder() .FromFile(Directory.GetCurrentDirectory() + "/pdfservices-api-credentials.json") .Build(); //Create an ExecutionContext using credentials and create a new operation instance. ExecutionContext executionContext = ExecutionContext.Create(credentials); CreatePDFOperation htmlToPDFOperation = CreatePDFOperation.CreateNew(); // Set operation input from a source file. FileRef source = FileRef.CreateFromLocalFile(@"createPDFFromStaticHtmlInput.zip"); htmlToPDFOperation.SetInput(source); // Provide any custom configuration options for the operation. SetCustomOptions(htmlToPDFOperation); // Execute the operation. FileRef result = htmlToPDFOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/createPdfFromStaticHtmlOutput.pdf"); } 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); } }
/// <summary> /// Sets any custom options for the operation. /// </summary> /// <param name="htmlToPDFOperation">operation instance for which the options are provided.</param> private static void SetCustomOptions(CreatePDFOperation htmlToPDFOperation) { // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation). PageLayout pageLayout = new PageLayout(); pageLayout.SetPageSize(8, 11.5); // Set the desired HTML-to-PDF conversion options. CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.HtmlOptionsBuilder() .IncludeHeaderFooter(true) .WithPageLayout(pageLayout) .Build(); htmlToPDFOperation.SetOptions(htmlToPdfOptions); }
/// <summary> /// Sets any custom options for the operation. /// </summary> /// <param name="htmlToPDFOperation">operation instance for which the options are provided.</param> private static void SetCustomOptions(CreatePDFOperation htmlToPDFOperation) { // Define the page layout, in this case an 8 x 11.5 inch page (effectively portrait orientation). PageLayout pageLayout = new PageLayout(); pageLayout.SetPageSize(8, 11.5); //Set the dataToMerge field that needs to be populated in the HTML before its conversion JObject dataToMerge = new JObject { { "title", "Create, Convert PDFs and More!" }, { "sub_title", "Easily integrate PDF actions within your document workflows." } }; // Set the desired HTML-to-PDF conversion options. CreatePDFOptions htmlToPdfOptions = CreatePDFOptions.HtmlOptionsBuilder() .IncludeHeaderFooter(true) .WithPageLayout(pageLayout) .WithDataToMerge(dataToMerge) .Build(); htmlToPDFOperation.SetOptions(htmlToPdfOptions); }
/// <summary> /// Fills selected template with selected datasource. /// </summary> /// <param name="selectedTemplate"></param> /// <returns></returns> //Todo: Add data-source as indata aswell. public string FillTemplate(DocumentTemplate selectedTemplate) { string sourceFile = selectedTemplate.FilePath; string destinationFile = $@"C:\Users\Gustav\source\repos\Doctrim\Doctrim.API\FilledTemplates\DatasourceName_{selectedTemplate.TemplateName}_{DateTime.Now.ToString("yyMMddhhmmssffff")}.docx"; try { //Makes a copy of template, that gets filled. File.Copy(sourceFile, destinationFile, true); using (WordprocessingDocument doc = WordprocessingDocument.Open(destinationFile, true)) { var body = doc.MainDocumentPart.Document.Body; foreach (var text in body.Descendants <Text>()) { if (text.Text.Contains("<#name>")) { text.Text = text.Text.Replace("<#name>", "Gustav"); } if (text.Text.Contains("<#lastname>")) { text.Text = text.Text.Replace("<#lastname>", "Falk"); } if (text.Text.Contains("<#food>")) { text.Text = text.Text.Replace("<#food>", "Tacos"); } if (text.Text.Contains("<#animal>")) { text.Text = text.Text.Replace("<#animal>", "Dog"); } } } Credentials credentials = Credentials.ServiceAccountCredentialsBuilder() .FromFile("Insert path to pdf-apitools-credentials.json inside of CreatePdfFromDocx") //TODO: Fill in credential path here .Build(); //Create an ExecutionContext using credentials and create a new operation instance. ExecutionContext executionContext = ExecutionContext.Create(credentials); CreatePDFOperation createPdfOperation = CreatePDFOperation.CreateNew(); // Set operation input from a source file. FileRef source = FileRef.CreateFromLocalFile(destinationFile); createPdfOperation.SetInput(source); // Execute the operation. FileRef result = createPdfOperation.Execute(executionContext); string savedPdf = $"{_env.ContentRootPath}\\FilledTemplates\\{Path.GetFileNameWithoutExtension(destinationFile)}.pdf"; // Save the result to the specified location. result.SaveAs(savedPdf); return(savedPdf); } catch { return(null); } }