public int TryParse(out PageRange[] pageRanges) { int result = Define.Success; pageRanges = null; try { if (PageInterval > -1) { int ranges = NumberOfPages / PageInterval; if (numberOfPages % PageInterval != 0) { ranges++; } pageRanges = new PageRange[ranges]; for (int rangeCnt = 0; rangeCnt < ranges; rangeCnt++) { pageRanges[rangeCnt] = new PageRange(rangeCnt * PageInterval + 1, (rangeCnt + 1) * PageInterval); } if (numberOfPages % PageInterval != 0) { pageRanges[ranges - 1].PageTo = NumberOfPages; } } else if (!string.IsNullOrEmpty(PageRanges)) { string[] ranges = PageRanges.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); pageRanges = new PageRange[ranges.Length]; for (int rangeCnt = 0; rangeCnt < ranges.Length; rangeCnt++) { if (ranges[rangeCnt].IndexOfAny(dash) > 0) { int[] pages = ranges[rangeCnt].Split(dash).Select(str => int.Parse(str)).ToArray(); pageRanges[rangeCnt] = new PageRange(pages[0], pages[1]); } else { int[] pages = ranges[rangeCnt].Split(comma).Select(str => int.Parse(str)).ToArray(); pageRanges[rangeCnt] = new PageRange(pages); } } } else { ErrorMsg = string.Format("Please enter a page range or interval."); result = Define.RangeAndIntervalAreNullOrEmpty; } } catch (Exception ex) { result = Define.Failure; ErrorMsg = "An error occurred while processing files" + ex.Message; } return(result); }
private int ValidatePageRange() { int result = Define.Failure; if (string.IsNullOrEmpty(PageRanges)) { result = Define.PageRangeIsNullOrEmpty; ErrorMsg = "Page range is required."; } else if (!Regex.IsMatch(PageRanges, pageRangePattern, RegexOptions.Singleline)) { result = Define.PageRangeSyntaxError; ErrorMsg = "Please enter a valid page range (see example for syntax)."; } else { char[] delimiters = new char[] { '-', ';', ',' }; int[] intArray = PageRanges.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Select(str => int.Parse(str)).ToArray(); if (intArray.Min() < 1 || intArray.Max() > NumberOfPages) { result = Define.SpecifiedPageRangeNotValid; ErrorMsg = string.Format("Please enter a valid page range between 1 and {0}.", NumberOfPages); } else { result = Define.Success; } } return(result); }
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. ExecutionContext executionContext = ExecutionContext.Create(credentials); // Create a new operation instance RotatePagesOperation rotatePagesOperation = RotatePagesOperation.CreateNew(); // Set operation input from a source file. FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"rotatePagesInput.pdf"); rotatePagesOperation.SetInput(sourceFileRef); // Sets angle by 90 degrees (in clockwise direction) for rotating the specified pages of // the input PDF file. PageRanges firstPageRange = GetFirstPageRangeForRotation(); rotatePagesOperation.SetAngleToRotatePagesBy(Angle._90, firstPageRange); // Sets angle by 180 degrees (in clockwise direction) for rotating the specified pages of // the input PDF file. PageRanges secondPageRange = GetSecondPageRangeForRotation(); rotatePagesOperation.SetAngleToRotatePagesBy(Angle._180, secondPageRange); // Execute the operation. FileRef result = rotatePagesOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/rotatePagesOutput.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. ExecutionContext executionContext = ExecutionContext.Create(credentials); // Create a new operation instance SplitPDFOperation splitPDFOperation = SplitPDFOperation.CreateNew(); // Set operation input from a source file. FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"splitPDFInput.pdf"); splitPDFOperation.SetInput(sourceFileRef); // Set the page ranges where each page range corresponds to a single output file. PageRanges pageRanges = GetPageRanges(); splitPDFOperation.SetPageRanges(pageRanges); // Execute the operation. List <FileRef> result = splitPDFOperation.Execute(executionContext); // Save the result to the specified location. int index = 0; foreach (FileRef fileRef in result) { fileRef.SaveAs(Directory.GetCurrentDirectory() + "/output/SplitPDFByPageRangesOutput_" + index + ".pdf"); index++; } } 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); } }
private static PageRanges GetPageRangeForSecondFile() { // Specify which pages of the second file are to be included in the combined file. PageRanges pageRangesForSecondFile = new PageRanges(); // Add all pages including and after page 5. pageRangesForSecondFile.AddAllFrom(5); return(pageRangesForSecondFile); }
private static PageRanges GetSecondPageRangeForRotation() { // Specify pages for rotation. PageRanges secondPageRange = new PageRanges(); // Add page 2. secondPageRange.AddSinglePage(2); return(secondPageRange); }
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); CombineFilesOperation combineFilesOperation = CombineFilesOperation.CreateNew(); // Create a FileRef instance from a local file. FileRef firstFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput1.pdf"); PageRanges pageRangesForFirstFile = GetPageRangeForFirstFile(); // Add the first file as input to the operation, along with its page range. combineFilesOperation.AddInput(firstFileToCombine, pageRangesForFirstFile); // Create a second FileRef instance using a local file. FileRef secondFileToCombine = FileRef.CreateFromLocalFile(@"combineFileWithPageRangeInput2.pdf"); PageRanges pageRangesForSecondFile = GetPageRangeForSecondFile(); // Add the second file as input to the operation, along with its page range. combineFilesOperation.AddInput(secondFileToCombine, pageRangesForSecondFile); // Execute the operation. FileRef result = combineFilesOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/combineFilesOutput.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); } }
private static PageRanges GetFirstPageRangeForRotation() { // Specify pages for rotation. PageRanges firstPageRange = new PageRanges(); // Add page 1. firstPageRange.AddSinglePage(1); // Add pages 3 to 4. firstPageRange.AddRange(3, 4); return(firstPageRange); }
private static PageRanges GetPageRangeForDeletion() { // Specify pages for deletion. PageRanges pageRangeForDeletion = new PageRanges(); // Add page 1. pageRangeForDeletion.AddSinglePage(1); // Add pages 3 to 4. pageRangeForDeletion.AddRange(3, 4); return(pageRangeForDeletion); }
private static PageRanges GetPageRanges() { // Specify page ranges. PageRanges pageRanges = new PageRanges(); // Add page 1. pageRanges.AddSinglePage(1); // Add pages 3 to 4. pageRanges.AddRange(3, 4); return(pageRanges); }
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. ExecutionContext executionContext = ExecutionContext.Create(credentials); // Create a new operation instance DeletePagesOperation deletePagesOperation = DeletePagesOperation.CreateNew(); // Set operation input from a source file. FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"deletePagesInput.pdf"); deletePagesOperation.SetInput(sourceFileRef); // Delete pages of the document (as specified by PageRanges). PageRanges pageRangeForDeletion = GetPageRangeForDeletion(); deletePagesOperation.SetPageRanges(pageRangeForDeletion); // Execute the operation. FileRef result = deletePagesOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/deletePagesOutput.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); } }
private static PageRanges GetPageRangeForFirstFile() { // Specify pages of the first file for replacing the page of base PDF file. PageRanges pageRanges = new PageRanges(); // Add pages 1 to 3. pageRanges.AddRange(1, 3); // Add page 4. pageRanges.AddSinglePage(4); return(pageRanges); }
private static PageRanges GetPageRangeForFirstFile() { // Specify which pages of the first file are to be included in the combined file. PageRanges pageRangesForFirstFile = new PageRanges(); // Add page 2. pageRangesForFirstFile.AddSinglePage(2); // Add page 3. pageRangesForFirstFile.AddSinglePage(3); // Add pages 5 to 7. pageRangesForFirstFile.AddRange(5, 7); return(pageRangesForFirstFile); }
private static PageRanges GetPageRangeForFirstFile() { // Specify which pages of the first file are to be inserted in the base file. PageRanges pageRanges = new PageRanges(); // Add pages 1 to 3. pageRanges.AddRange(1, 3); // Add page 4. pageRanges.AddSinglePage(4); return(pageRanges); }
private static PageRanges GetPageRangeForReorder() { // Specify order of the pages for an output document. PageRanges pageRanges = new PageRanges(); // Add pages 3 to 4. pageRanges.AddRange(3, 4); // Add page 1. pageRanges.AddSinglePage(1); return(pageRanges); }
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. ExecutionContext executionContext = ExecutionContext.Create(credentials); // Create a new operation instance ReorderPagesOperation reorderPagesOperation = ReorderPagesOperation.CreateNew(); // Set operation input from a source file, along with specifying the order of the pages for // rearranging the pages in a PDF file. FileRef sourceFileRef = FileRef.CreateFromLocalFile(@"reorderPagesInput.pdf"); reorderPagesOperation.SetInput(sourceFileRef); PageRanges pageRanges = GetPageRangeForReorder(); reorderPagesOperation.SetPagesOrder(pageRanges); // Execute the operation. FileRef result = reorderPagesOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/reorderPagesOutput.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. ExecutionContext executionContext = ExecutionContext.Create(credentials); // Create a new operation instance InsertPagesOperation insertPagesOperation = InsertPagesOperation.CreateNew(); // Set operation base input from a source file. FileRef baseSourceFile = FileRef.CreateFromLocalFile(@"baseInput.pdf"); insertPagesOperation.SetBaseInput(baseSourceFile); // Create a FileRef instance using a local file. FileRef firstFileToInsert = FileRef.CreateFromLocalFile(@"firstFileToInsertInput.pdf"); PageRanges pageRanges = GetPageRangeForFirstFile(); // Adds the pages (specified by the page ranges) of the input PDF file to be inserted at // the specified page of the base PDF file. insertPagesOperation.AddPagesToInsertAt(firstFileToInsert, pageRanges, 2); // Create a FileRef instance using a local file. FileRef secondFileToInsert = FileRef.CreateFromLocalFile(@"secondFileToInsertInput.pdf"); // Adds all the pages of the input PDF file to be inserted at the specified page of the // base PDF file. insertPagesOperation.AddPagesToInsertAt(secondFileToInsert, 3); // Execute the operation. FileRef result = insertPagesOperation.Execute(executionContext); // Save the result to the specified location. result.SaveAs(Directory.GetCurrentDirectory() + "/output/insertPagesOutput.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); } }