How to add text and images to PDF in C# using PDF.co Web API

How to add text and images to PDF in C#

The coding tutorials are designed to help you test the features without need to write your own code. PDF.co Web API can add text and images to PDF. It can be used from C#. PDF.co Web API is the Rest API that provides set of data extraction functions, tools for documents manipulation, splitting and merging of pdf files. Includes built-in OCR, images recognition, can generate and read barcodes from images, scans and pdf.

The SDK samples like this one below explain how to quickly make your application do add text and images to PDF in C# with the help of PDF.co Web API. Follow the instructions from the scratch to work and copy the C# code. Implementing C# application typically includes multiple stages of the software development so even if the functionality works please test it with your data and the production environment.

Free trial version of PDF.co Web API is available for download from our website. Get it to try other source code samples for C#.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
using System; using System.IO; using System.Net; using Newtonsoft.Json.Linq; namespace ByteScoutWebApiExample { class Program { // The authentication key (API Key). // Get your own by registering at https://app.pdf.co/documentation/api const String API_KEY = "*****************************************"; // Direct URL of source PDF file. const string SourceFileUrl = "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-edit/sample.pdf"; // Comma-separated list of page indices (or ranges) to process. Leave empty for all pages. Example: '0,2-5,7-'. const string Pages = ""; // PDF document password. Leave empty for unprotected documents. const string Password = ""; // Destination PDF file name const string DestinationFile = @".\result.pdf"; // Image params private const string Type1 = "image"; private const string ImageUrl = "https://bytescout-com.s3.amazonaws.com/files/demo-files/cloud-api/pdf-edit/logo.png"; static void Main(string[] args) { // Create standard .NET web client instance WebClient webClient = new WebClient(); // Set API Key webClient.Headers.Add("x-api-key", API_KEY); // Find Text coordinates to add image var oCoordinates = FindCoordinates(API_KEY, SourceFileUrl, "Your Company Name"); var Y1 = oCoordinates.y; var X1 = 450; var Width1 = 119; var Height1 = 32; // * Add image * // Prepare URL for `PDF Edit` API call string query = Uri.EscapeUriString(string.Format( "https://api.pdf.co/v1/pdf/edit/add?name={0}&password={1}&pages={2}&url={3}&type={4}&x={5}&y={6}&width={7}&height={8}&urlimage={9}", Path.GetFileName(DestinationFile), Password, Pages, SourceFileUrl, Type1, X1, Y1, Width1, Height1, ImageUrl)); try { // Execute request string response = webClient.DownloadString(query); // Parse JSON response JObject json = JObject.Parse(response); if (json["error"].ToObject<bool>() == false) { // Get URL of generated PDF file string resultFileUrl = json["url"].ToString(); // Download PDF file webClient.DownloadFile(resultFileUrl, DestinationFile); Console.WriteLine("Generated PDF file saved as \"{0}\" file.", DestinationFile); } 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> /// Find result coordinates /// </summary> public static ResultCoOrdinates FindCoordinates(string API_KEY, string SourceFileUrl, string SearchString) { ResultCoOrdinates oResult = null; // Create standard .NET web client instance WebClient webClient = new WebClient(); // Set API Key webClient.Headers.Add("x-api-key", API_KEY); // Prepare URL for PDF text search API call. // See documentation: https://app.pdf.co/documentation/api/1.0/pdf/find.html string query = Uri.EscapeUriString(string.Format( "https://api.pdf.co/v1/pdf/find?url={0}&searchString={1}", SourceFileUrl, SearchString)); try { // Execute request string response = webClient.DownloadString(query); // Parse JSON response JObject json = JObject.Parse(response); if (json["status"].ToString() != "error") { JToken item = json["body"][0]; oResult = new ResultCoOrdinates { x = Convert.ToInt32(item["left"]), y = Convert.ToInt32(item["top"]), width = Convert.ToInt32(item["width"]), height = Convert.ToInt32(item["height"]) }; } } catch (WebException e) { Console.WriteLine(e.ToString()); } webClient.Dispose(); return oResult; } } public class ResultCoOrdinates { public int x { get; set; } public int y { get; set; } public int width { get; set; } public int height { get; set; } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

packages.config
      
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net40" /> </packages>

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Related Samples: