Portable Document Format (PDF) usage continues to grow since its inception in 1993.

The PDF format is an ISO-3200 standard which means it can work on any device and OS. The software is now open-source meaning you can create PDFs from your own codebase. That includes using the popular C# programming language PDF tools to generate files on the fly.

This article offers five C# PDF programming tips and tricks to make your projects a joy to code.

Read on to learn how to generate a PDF from an HTML page. See how to include security features like encryption. Discover how to print a PDF using .NET and edit your files.

Does C# Work With PDFs?

C# and the .NET platform offer a huge range of useful functions and features. However, they don’t offer PDF manipulation out of the box.

All of the following tips and examples use a third-party library. And like all of the best tools, the library takes the hassle out of working with PDFs.

1. C# Generate PDF From URL and HTML

Is it possible to create PDF files by simply passing a web page URL to a pre-defined function? What about HTML strings and files?

Firstly, download and install this excellent C# edit PDF library using NuGet.

Next, include the library at the top of your code and instantiate the object:

using IronPdf;

IronPdf.ChromePdfRenderer chromeRenderer = new IronPdf.ChromePdfRenderer();
var pdfFile = chromeRenderer.RenderUrlAsPdf(“https://www.yoursite.com/”);

pdfFile.SaveAs(“homepage.pdf”);

Four lines of code and you’re done! You can use the RenderHTMLFileAsPdf() method to generate a PDF file from an HTML file. Or RenderHtmlAsPdf() for HTML strings.

2. C# PDF Security

PDFs come with their own encryption and user restrictions. For example, you can:

  • Make the PDF read-only
  • Refuse user annotations and bookmarks
  • Disallow printing
  • Prevent copy/paste

Using the library above, work with the Pdf.SecuritySettings options to add and remove security features.

3. Print PDFs Using .NET

C# allows you to print PDFs directly to a printer if the computer has Adobe Acrobat installed. But what if it doesn’t?

The library offers a range of simple C# print PDF options that fit onto one line of code:

pdfFile.Print(300, false);

This will print the PdfDocument object at 300dpi but won’t open the print dialog. Use the GetPrintDocument() method for enhanced printing preferences.

4. Watermarking PDFs With C#

You can edit PDF files to protect them from unauthorized usage by adding a watermark in the background.

The library makes this easy:

pdfFile.WatermarkAllPages(“<h3 style=’color:gray;’>CONFIDENTIAL</h3>”,
IronPdf.Editing.WaterMarkLocation.MiddleCenter, 55, -40, “https://www.yoursite.com”);

Use the first parameter to enter your formatted text. The others let you place the watermark where you wish and add a link.

5. Merge PDF Files

Merging documents involves rendering one or more PDFs using the Renderer object then merging them:

var mergedFiles = IronPdf.PdfDocument.Merge(pdfFile1, pdfFile2);

Save the new PDF using the SaveAs() method. Now, you have one document from two!

More C# Programming Language PDF Tips

C# PDF creation and manipulation is a daunting task without third-party help.

By installing an established PDF library you can use C# to generate PDFs and add metadata. You can modify their contents and even create bookmarks and annotations.

Trying to accomplish these tasks from scratch will set your projects back by months if not years. Be smart. Use a third-party library.

Read more C# tips on how to create PDFs and edit PDFs on our blog.

Leave a Reply

Your email address will not be published. Required fields are marked *