Skip to content

Commit

Permalink
= regular update
Browse files Browse the repository at this point in the history
  • Loading branch information
estarkov committed Jan 27, 2023
1 parent 73a53ae commit f277b5b
Show file tree
Hide file tree
Showing 19 changed files with 866 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyTrademarkAttribute>false</GenerateAssemblyTrademarkAttribute>
<GenerateAssemblyCultureAttribute>false</GenerateAssemblyCultureAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Remove="Properties\**" />
<None Remove="Properties\**" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="sample.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.32126.317
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ByteScoutWebApiExample.NETCore", "ByteScoutWebApiExample.NETCore.csproj", "{6C67B480-C7E2-4AF7-AE7B-0E12A5799D9C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6C67B480-C7E2-4AF7-AE7B-0E12A5799D9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C67B480-C7E2-4AF7-AE7B-0E12A5799D9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C67B480-C7E2-4AF7-AE7B-0E12A5799D9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C67B480-C7E2-4AF7-AE7B-0E12A5799D9C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBAE862B-1737-4E3F-96CF-5B8772A7AC09}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1E1C2C34-017E-4605-AE2B-55EA3313BE51}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ByteScoutWebApiExample</RootNamespace>
<AssemblyName>ByteScoutWebApiExample</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="sample.pdf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.40629.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ByteScoutWebApiExample", "ByteScoutWebApiExample.csproj", "{1E1C2C34-017E-4605-AE2B-55EA3313BE51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1E1C2C34-017E-4605-AE2B-55EA3313BE51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E1C2C34-017E-4605-AE2B-55EA3313BE51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E1C2C34-017E-4605-AE2B-55EA3313BE51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E1C2C34-017E-4605-AE2B-55EA3313BE51}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
229 changes: 229 additions & 0 deletions Delete Text From PDF/C#/Remove Credit Card Data From PDF/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
//*******************************************************************************************//
// //
// Download Free Evaluation Version From: https://bytescout.com/download/web-installer //
// //
// Also available as Web API! Get Your Free API Key: https://app.pdf.co/signup //
// //
// Copyright © 2017-2020 ByteScout, Inc. All rights reserved. //
// https://www.bytescout.com //
// https://pdf.co //
// //
//*******************************************************************************************//


using System;
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Collections.Generic;
using Newtonsoft.Json;

// Cloud API asynchronous "Delete Text from PDF" job example.

namespace ByteScoutWebApiExample
{
class Program
{
// The authentication key (API Key).
// Get your own by registering at https://app.pdf.co
const String API_KEY = "YOUR_PDFco_API_KEY_HERE";

// Source PDF file
const string SourceFile = @".\sample.pdf";

// PDF document password. Leave empty for unprotected documents.
const string Password = "";

// Regex Enabled Search
const bool RegexEnabled = true;

// Destination PDF file name
const string DestinationFile = @".\result.pdf";


static void Main(string[] args)
{
// Upload file to the cloud
string uploadedFileUrl = UploadFile(SourceFile);

if (string.IsNullOrEmpty(uploadedFileUrl))
{
Console.WriteLine("File upload error.");
return;
}

// Regular Expression for Credit Card
string regex_creditCard = @"[0-9]{4} [0-9]{4} [0-9]{4} [0-9]{4}";

// Regular expression CVV number
string regex_cvv = @"\b[0-9]{3}\b";

// Regular expression mm/year
string regex_month_year = @"[0-9]{2}(\/|-)[0-9]{4}";

// Create standard .NET web client instance
WebClient webClient = new WebClient();

// Set API Key
webClient.Headers.Add("x-api-key", API_KEY);

// Prepare requests params as JSON
// See documentation: https://apidocs.pdf.co/#pdf-search-and-delete-text-from-pdf
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("name", Path.GetFileName(DestinationFile));
parameters.Add("password", Password);
parameters.Add("url", uploadedFileUrl);
parameters.Add("searchStrings", new string[] { regex_creditCard, regex_cvv, regex_month_year });
parameters.Add("regex", RegexEnabled);
parameters.Add("async", true); // ! Making Async request

// Convert dictionary of params to JSON
string jsonPayload = JsonConvert.SerializeObject(parameters);

// URL of `Delete Text from PDF` API call
string url = "https://api.pdf.co/v1/pdf/edit/delete-text";

try
{
// Execute POST request with JSON payload
string response = webClient.UploadString(url, jsonPayload);

// Parse JSON response
JObject json = JObject.Parse(response);

if (json["error"].ToObject<bool>() == false)
{
// Asynchronous job ID
string jobId = json["jobId"].ToString();
// URL of generated PDF file that will available after the job completion
string resultFileUrl = json["url"].ToString();

// Check the job status in a loop.
// If you don't want to pause the main thread you can rework the code
// to use a separate thread for the status checking and completion.
do
{
string status = CheckJobStatus(jobId); // Possible statuses: "working", "failed", "aborted", "success".

// Display timestamp and status (for demo purposes)
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": " + status);

if (status == "success")
{
// Download PDF file
webClient.DownloadFile(resultFileUrl, DestinationFile);

Console.WriteLine("Generated PDF file saved as \"{0}\" file.", DestinationFile);
break;
}
else if (status == "working")
{
// Pause for a few seconds
Thread.Sleep(3000);
}
else
{
Console.WriteLine(status);
break;
}
}
while (true);
}
else
{
Console.WriteLine(json["message"].ToString());
}
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}

webClient.Dispose();


Console.WriteLine();
Console.WriteLine("Press any key...");
Console.ReadKey();
}

/// <summary>
/// Uploads file to the cloud and return URL of uploaded file to use in further API calls.
/// </summary>
/// <param name="file">Source file name (path).</param>
/// <returns>URL of uploaded file</returns>
static string UploadFile(string file)
{
// Create standard .NET web client instance
WebClient webClient = new WebClient();

// Set API Key
webClient.Headers.Add("x-api-key", API_KEY);

try
{
// 1. RETRIEVE THE PRESIGNED URL TO UPLOAD THE FILE.
// * If you already have a direct file URL, skip to the step 3.

// Prepare URL for `Get Presigned URL` API call
string query = Uri.EscapeUriString(string.Format(
"https://api.pdf.co/v1/file/upload/get-presigned-url?contenttype=application/octet-stream&name={0}",
Path.GetFileName(file)));

// Execute request
string response = webClient.DownloadString(query);

// Parse JSON response
JObject json = JObject.Parse(response);

if (json["error"].ToObject<bool>() == false)
{
// Get URL to use for the file upload
string uploadUrl = json["presignedUrl"].ToString();
// Get URL of uploaded file to use with later API calls
string uploadedFileUrl = json["url"].ToString();

// 2. UPLOAD THE FILE TO CLOUD.

webClient.Headers.Add("content-type", "application/octet-stream");
webClient.UploadFile(uploadUrl, "PUT", file); // You can use UploadData() instead if your file is in byte[] or Stream

return uploadedFileUrl;
}
else
{
// Display service reported error
Console.WriteLine(json["message"].ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
webClient.Dispose();
}

return null;
}

static string CheckJobStatus(string jobId)
{
using (WebClient webClient = new WebClient())
{
// Set API Key
webClient.Headers.Add("x-api-key", API_KEY);

string url = "https://api.pdf.co/v1/job/check?jobid=" + jobId;

string response = webClient.DownloadString(url);
JObject json = JObject.Parse(response);

return Convert.ToString(json["status"]);
}
}
}
}
Loading

0 comments on commit f277b5b

Please sign in to comment.