Code sample for txt/2/pdf API

C#


This C# sample demonstrates how to access the PDFTeam txt/2/pdf API endpoint. The code converts a two-page report in plain TXT format to a PDF and adds overlays with some text and a QR code. Also added will be a header that prints the document title on every page. The resulting PDF will be stored in AWS S3, the permalink is returned to the caller.

Parameters that will be POST'ed to the API are in a separate file. This C# sample expects the params.json-file to be in the same folder as the executable. Create a build action so that the file is copied to the output directory.

Here's a sample Visual Studio solution, the build action is highlighted:

Visual Studio setup for PDFTam API Convert Text to PDF

The params-file defines two overlays, one with the text "TOP SECRET" in red, the other one with a barcode. Note the sections near the end, where headers and footers are defined:

// Sample C# code for accessing the PDFTeam API, endpoint: txt/2/pdf
// Overlay #1: Text "TOP SECRET"
// Overlay #2: QR barcode
// Header: Document title
//
// File: params.json

{
  "job": "ACME ANALYSIS",
  "user": "YOUR_USERNAME",
  "key": "YOUR_APIKEY",
  "test": false,
  "inputOptions": {
    "source": "https://pdfteam.com/samples/coyote_report.txt"
  },
  "outputOptions": {
    "storage": true,
    "file": "acme_analysis.pdf"
  },
  "pdfOptions": {
    "format": "A4",
    "margin": {
      "left": "2cm",
      "top": "3cm",
      "right": "2cm",
      "bottom": "3cm",
    },
  },
  "overlays": [
    {
      "position": {
        "x": "170mm",
        "y": "270mm"
      },
      "type": "barcode",
      "config": {
        "type": 19,
        "data": "https://pdfteam.com"
      }
    },
    {
      "rotate": 45,
      "position": {
        "x": "center",
        "y": "middle"
      },
      "type": "text",
      "config": {
        "data": "TOP SECRET",
        "font": "Helvetica Bold",
        "size": 72,
        "color": "#ff0000",
        "opacity": 0.5
      }
    },
  ],
  "hfOptions": {
    "footer": " ",
    "header":
    "<div style=\"color:#0000ff;font-size: 12px;margin-left:auto;margin-right:auto;\">TITLE: <span class=\"title\"></span></div>",
  }
}


NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark.


Main C# program:

// Sample C# code for accessing the PDFTeam API, endpoint: txt/2/pdf
// Code assumes .NET 8 (LTS). For older versions, we recommend Newtonsoft.JSON package
// to parse the params file
// File: program.cs

using System.Text;

namespace PDFTeam
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var uri = new Uri("https://api.pdfteam.com/api/txt/2/pdf");

            using (var client = new HttpClient())
            {
                StreamReader sr = new StreamReader("params.json");
                string json = sr.ReadToEnd();
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = uri,
                    Content = content
                };

                using (var response = await client.SendAsync(request))
                {
                    response.EnsureSuccessStatusCode();
                    var body = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(body);
                }
            }
        }
    }
}


Sample response:

{"endpoint": "txt/2/pdf", "job": "ACME ANALYSIS", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../acme_analysis.pdf"}


Sample result:

PDF Report with QR code, watermark, header converted with PDFTEam API from TXT with C#