I’m trying to figure out how to add text to a PDF document. I’ve created a fresh C# dotnet core console application and copied one of the samples from the Aspose.PDF documentation. The sample I copied is below …
using System;
using Aspose.Pdf;
using Aspose.Pdf.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Create new document object
Document pdfDocument = new Document();
// Get particular page
Page pdfPage = (Page)pdfDocument.Pages.Add();
// 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.LightGray;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
// Set StrokingColor property for drawing border (stroking) around text rectangle
textFragment.TextState.StrokingColor = Aspose.Pdf.Color.DarkRed;
// Set DrawTextRectangleBorder property value to true
textFragment.TextState.DrawTextRectangleBorder = true;
TextBuilder tb = new TextBuilder(pdfPage);
tb.AppendText(textFragment);
// Save the document
pdfDocument.Save(@"c:\users\ryan\desktop\test.pdf");
}
}
}
This produces a blank PDF and I see no text anywhere. I attached the PDF it created to this post, as well.
I’m using Aspose.PDF version 19.2.0. I’m running this on Windows 10.
Why isn’t the text appearing? Does anybody see something I’m doing incorrectly?
test.pdf (64.8 KB)