This simple code sample shows you how to create a PDF using C# and print “Hello World” to it
// create a new document with a single section
Document document = new Document();
Section section = document.Sections.Add();
document.Sections.Add( section );
// add a new textparagraph to the section
TextParagraph textParagraph = new TextParagraph();
section.Paragraphs.Add( textParagraph );
// create a fragment, set some text and add it to the paragraph
Fragment fragment = new Fragment();
fragment.Text = "Hello world!";
textParagraph.Fragments.Add( fragment );
Here is the created PDF:
You might have noticed that you have to add the text into Fragment then add the Fragment into TextParagraph instead of simply add the text into TextParagraph. By doing so, a TextParagraph is allowed to have texts with different fonts and styles. This design increases the flexibility of TextParagraph.
Here is the sample of saving document:
using( FileStream file = new FileStream( @"..\..\HelloWorld.pdf", FileMode.Create, FileAccess.Write ) )
{
document.Write( file );
}
We have sent an email with a download link. Alternatively, you may want to use the NuGet package manager to install our library.
Use the NugetID and start right away, or download the package and install it handmatically