This short tutorial explains how to replace text in PDF with C# REST API. You will learn to replace words in PDF with C# RESTful Service using a .NET-based Cloud SDK. It will guide you in finding words by comparing strings or using regex and perform this task on a single page or the entire PDF file.
Prerequisite
- Create an account API credentials to replace text inside a PDF
- Download Aspose.PDF Cloud SDK for Dotnet to replace text in a PDF file
- Setup C# project with the above SDK for finding and replacing text
Steps to Replace Text in PDF Online with C# REST Interface
- Configure the PdfApi object using the key and application SID to replace the text
- Upload the source PDF file into the Cloud storage for finding and replacing text
- Create the list of TextReplace objects with a pair of old and new strings
- Create the text replace request using the TextReplaceListRequest class by providing the TextReplace list
- Call the PostDocumentTextReplace to replace all the old strings with new strings
- Download the output file on the successful response from the API
The above steps describe how to find and replace word in PDF with C# REST API. Create the PdfApi object containing the features to work with a PDF file, upload the source PDF file to online Cloud storage, create a list of pairs of words of old and new strings, and create a request object using this pair of lists. Call the PostDocumentTextReplace() method to replace the listed words and save the output file.
Code to Find and Replace Text in PDF with C# .NET-based API
using System; | |
using System.IO; | |
using Aspose.Pdf.Cloud.Sdk.Api; | |
using Aspose.Pdf.Cloud.Sdk.Model; | |
using System.Collections.Generic; | |
namespace Aspose.PDF.Cloud.Examples.Kb | |
{ | |
public class PdfTasks | |
{ | |
public static void ReplaceTextInPdf() | |
{ | |
PdfApi pdfApi = new PdfApi("APP_KEY", "APP_SID"); | |
String fileName = "TextAndImages.pdf"; | |
try | |
{ | |
// Upload source file to aspose cloud storage for replacing words | |
FilesUploadResult result = pdfApi.UploadFile(fileName, new MemoryStream(File.ReadAllBytes(fileName))); | |
if (result.Errors.Count == 0) | |
{ | |
List<TextReplace> textReplaces = new List<TextReplace>() | |
{ | |
new TextReplace("English", "English Subject", false), | |
new TextReplace("Math", "Math Subject", false), | |
new TextReplace("Science", "Science Subject", false), | |
}; | |
TextReplaceListRequest textReplaceListRequest = new TextReplaceListRequest(textReplaces); | |
TextReplaceResponse response = pdfApi.PostDocumentTextReplace(fileName, textReplaceListRequest); | |
if (response.Status == "OK") | |
{ | |
// Download created pdf file | |
Stream storageRes = pdfApi.DownloadFile(fileName); | |
FileStream fileStream = new FileStream("output.pdf", FileMode.Create, FileAccess.Write); | |
storageRes.CopyTo(fileStream); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
} | |
} |
This code demonstrates the process to replace text in PDF file with C# REST Interface. You may use the list of strings or Regex expression while searching the strings in the complete document. If you want to replace text in a single page of the uploaded PDF file, use the PostPageTextReplace() method with the target page number.
This article has taught us to search and replace text in a PDF file. If you want to add a new page to a PDF file, refer to the article on Add new page in PDF with C# REST API.