Read the complete article on adding text to the PDF documents using C#: https://blog.aspose.com/2021/08/26/add-text-to-a-pdf-using-csharp/
Last active
August 27, 2021 05:14
-
-
Save aspose-com-gists/9ffb1c9daa703e0eb7e1d03bf7aeb882 to your computer and use it in GitHub Desktop.
Add Text to PDF in C#
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
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
string fontFile = "font.ttf"; | |
// Load input PDF file | |
Document doc = new Document("input.pdf"); | |
// Create text builder object for first page of document | |
TextBuilder textBuilder = new TextBuilder(doc.Pages[1]); | |
// Create text fragment with sample string | |
TextFragment textFragment = new TextFragment("Hello world"); | |
if (fontFile != "") | |
{ | |
// Load the TrueType font into stream object | |
using (FileStream fontStream = File.OpenRead(fontFile)) | |
{ | |
// Set the font name for text string | |
textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF); | |
// Specify the position for Text Fragment | |
textFragment.Position = new Position(10, 10); | |
// Add the text to TextBuilder so that it can be placed over the PDF file | |
textBuilder.AppendText(textFragment); | |
} | |
// Save resulting PDF document | |
doc.Save("output.pdf"); | |
} |
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
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// Open document | |
Document pdfDocument = new Document("input.pdf"); | |
// Get particular page | |
Page pdfPage = (Page)pdfDocument.Pages[1]; | |
// Create text fragment | |
TextFragment textFragment = new TextFragment("main text"); | |
textFragment.Position = new Position(100, 600); | |
// Set text properties | |
textFragment.TextState.FontSize = 12; | |
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray); | |
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red); | |
// Create TextBuilder object | |
TextBuilder textBuilder = new TextBuilder(pdfPage); | |
// Append the text fragment to the PDF page | |
textBuilder.AppendText(textFragment); | |
// Save resulting PDF document. | |
pdfDocument.Save("output.pdf"); |
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
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// Create Document instance | |
Document doc = new Document("input.pdf"); | |
// Create page to pages collection of PDF file | |
Aspose.Pdf.Page page = doc.Pages.Add(); | |
// Create Graph object | |
Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400); | |
// Create rectangle instance with certain dimensions | |
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 400, 400); | |
// Create color object from Alpha color channel | |
rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183))); | |
// Add rectanlge to shapes collection of Graph object | |
canvas.Shapes.Add(rect); | |
// Add graph object to paragraphs collection of page object | |
page.Paragraphs.Add(canvas); | |
// Set value to not change position for graph object | |
canvas.IsChangePosition = false; | |
// Create TextFragment instance with sample value | |
TextFragment text = new TextFragment("transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text "); | |
// Create color object from Alpha channel | |
Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(30, 0, 255, 0); | |
// Set color information for text instance | |
text.TextState.ForegroundColor = color; | |
// Add text to paragraphs collection of page instance | |
page.Paragraphs.Add(text); | |
// Save the updated PDF file | |
doc.Save("output.pdf"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment