Replace Text on page in PDF via .NET SDK
Replacing Text in PDF Document Page using server-side .NET API
Get StartedHow to replace text on page in PDF document via Cloud .NET SDK
In order to replace text on page in PDF document via Cloud .NET SDK , we’ll use Aspose.PDF Cloud .NET SDK This Cloud SDK allows you to easily build cloud-based PDF creator, editor & converter apps in C#, ASP.NET, or other .NET languages for various cloud platforms. Open NuGet package manager, search for Aspose.PDF Cloud and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Pdf-Cloud
Steps to replace text on page in PDF document via .NET SDK
Aspose.PDF Cloud developers can easily load & replace text on page in PDF in just a few lines of code.
- Upload the File to Cloud Storage
- Initializes the PdfApi with the provided credentials (Application ID and Application Key).
- Reads the local PDF file and uploads it to the Aspose Cloud storage.
- Call API to Replace text on page.
- Downloads the updated file for local use.
This sample code shows replacing text on page in PDF documents
public static async Task PageTextReplace()
{
const string localImageFileName = @"C:\Samples\sample.pdf";
const string storageFileName = "sample.pdf";
const string localFolder = @"C:\Samples";
const string resultFileName = "output_page_replace_text.pdf";
const string storageTempFolder = "YourTempFolder";
const int pageNumber = 2;
// Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required).
var pdfApi = new PdfApi(AppSecret, AppSid);
using var file = File.OpenRead(localImageFileName);
var uploadResult = await pdfApi.UploadFileAsync(Path.Combine(storageTempFolder, storageFileName), file);
Console.WriteLine(uploadResult.Uploaded[0]);
var rect = new Rectangle(0, 0, 800, 500);
var textReplace = new TextReplace("Your-Document-Text-On-Page", "New-Document-Text-On-Page", false, Rect: rect);
var textReplaceList = new TextReplaceListRequest(new List<TextReplace> { textReplace },
StartIndex: 0, CountReplace: 0);
var response = await pdfApi.PostPageTextReplaceAsync(storageFileName, pageNumber, textReplaceList, folder: storageTempFolder);
using Stream downloadStream = await pdfApi.DownloadFileAsync(Path.Combine(storageTempFolder, storageFileName));
using FileStream localStream = File.Create(Path.Combine(localFolder, resultFileName));
downloadStream.CopyTo(localStream);
Console.WriteLine($"PageTextReplace(): {response.Matches} text occurencies successfully replaced on page in PDF document {resultFileName}.");
}
Work with Text in PDF
Text can be dynamically replaced in PDF document for a specific user, such as correcting mistakes in text or replacing text fragments. This is often used in marketing materials or for personalized reports. Replacing text in specific fonts, colors, and alignments can make certain sections more readable and visually appealing, creating a better user experience. Text replacements can help maintain consistent information across different PDF versions, avoiding manual edits and ensuring that key information remains in place. Replacement Texts in PDF documents with Aspose.PDF Cloud .NET SDK.
With our .NET library you can:
- Combine PDF documents.
- Split PDF Files.
- Convert PDF to other formats, and vice versa.
- Manipulate Annotations.
- Work with Images in PDF, etc.
- You can try out our free App to replace text in PDF files online and test the functionality.