Created
May 6, 2024 11:48
How to Edit PDF Document using C#. For further details: https://kb.conholdate.com/total/net/how-to-edit-pdf-document-using-csharp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Aspose.Pdf; | |
using Aspose.Pdf.Facades; | |
using Aspose.Pdf.Text; | |
class Program | |
{ | |
static void Main(string[] args) // Edit PDF in C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Create PdfFileEditor object | |
PdfContentEditor editor = new PdfContentEditor(); | |
editor.BindPdf("Input.pdf"); | |
// Replace some text in the entire file | |
while (true) | |
if (editor.ReplaceText("scenario", "situation") == false) | |
break; | |
// Replace some text and change its font and color | |
TextState textState = new TextState(); | |
textState.ForegroundColor = Color.Red; | |
textState.FontSize = 14; | |
while (true) | |
if (editor.ReplaceText("attack", "fight", textState) == false) | |
break; | |
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); | |
editor.Save(memoryStream); | |
// Add text to an existing page | |
PdfFileMend mendor = new PdfFileMend(); | |
memoryStream.Position = 0; | |
mendor.BindPdf(memoryStream); | |
FormattedText message = new FormattedText("Test message on the page"); | |
mendor.AddText(message, 2, 60, 300); | |
mendor.Save(memoryStream); | |
// Add a paragraph with some text on a new page | |
memoryStream.Position = 0; | |
Document document = new Document(memoryStream); | |
Page page = document.Pages.Add(); | |
page.Paragraphs.Add(new TextFragment("New paragraph is added")); | |
// Save the output | |
document.Save("output.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment