### Install PdfPig via NuGet Source: https://uglytoad.github.io/PdfPig Use the Package Manager Console to install the PdfPig library. ```powershell > Install-Package PdfPig ``` -------------------------------- ### Accessing PDF Document Metadata and Forms Source: https://uglytoad.github.io/PdfPig Demonstrates opening a PDF file and retrieving information such as page count, form fields, XMP metadata, bookmarks, and embedded files. ```csharp using System; using System.Collections.Generic; using System.Xml.Linq; using UglyToad.PdfPig; using UglyToad.PdfPig.AcroForms; using UglyToad.PdfPig.AcroForms.Fields; using UglyToad.PdfPig.Content; using UglyToad.PdfPig.Outline; public static class Program { public static void Main() { using (PdfDocument document = PdfDocument.Open(@"C:\temp\file.pdf")) { Console.WriteLine($"Document has {document.NumberOfPages} pages."); if (document.TryGetForm(out AcroForm form)) { foreach (AcroFieldBase field in form.GetFieldsForPage(1)) { switch (field) { case AcroCheckboxField cb: if (cb.IsChecked) { Console.WriteLine($"Checkbox was checked: {cb.Information.MappingName}."); } break; } } } if (document.TryGetXmpMetadata(out XmpMetadata metadata)) { XDocument xmp = metadata.GetXDocument(); } if (document.TryGetBookmarks(out Bookmarks bookmarks)) { Console.WriteLine($"Document contained bookmarks with {bookmarks.Roots.Count} root nodes."); } Console.WriteLine($"Document uses version {document.Version} of the PDF specification."); if (document.Advanced.TryGetEmbeddedFiles(out IReadOnlyList embeddedFiles)) { Console.WriteLine($"Document contains {embeddedFiles.Count} embedded files."); } } } } ``` -------------------------------- ### Create a PDF document with standard fonts Source: https://uglytoad.github.io/PdfPig Initializes a new PDF document, adds standard Helvetica fonts, writes text to an A4 page, and saves the result to a file. ```csharp using System.IO; using UglyToad.PdfPig.Content; using UglyToad.PdfPig.Core; using UglyToad.PdfPig.Fonts.Standard14Fonts; using UglyToad.PdfPig.Writer; public static class Program { public static void Main() { PdfDocumentBuilder builder = new PdfDocumentBuilder(); PdfDocumentBuilder.AddedFont helvetica = builder.AddStandard14Font(Standard14Font.Helvetica); PdfDocumentBuilder.AddedFont helveticaBold = builder.AddStandard14Font(Standard14Font.HelveticaBold); PdfPageBuilder page = builder.AddPage(PageSize.A4); PdfPoint closeToTop = new PdfPoint(15, page.PageSize.Top - 25); page.AddText("My first PDF document!", 12, closeToTop, helvetica); page.AddText("Hello World!", 10, closeToTop.Translate(0, -15), helveticaBold); File.WriteAllBytes(@"C:\temp\file.pdf", builder.Build()); } } ``` -------------------------------- ### Open Password Protected PDF Source: https://uglytoad.github.io/PdfPig Provide a list of passwords via ParsingOptions to open encrypted PDF files and access document metadata. ```csharp ParsingOptions parsingOptions = new ParsingOptions { Passwords = new List {"a password", "password123"} }; using (PdfDocument document = PdfDocument.Open(@"C:\path\to\pdffile\file.pdf", parsingOptions)) { // Get the title from the document metadata. Console.WriteLine(document.Information.Title); foreach (Page page in document.GetPages()) { IReadOnlyList letters = page.Letters; Console.WriteLine(letters.Count); } } ``` -------------------------------- ### Read PDF Content Source: https://uglytoad.github.io/PdfPig Open a PDF document to access its pages, letters, words, and images. ```csharp using System.Collections.Generic; using System.Linq; using UglyToad.PdfPig; using UglyToad.PdfPig.Content; public static class Program { public static void Main() { using (PdfDocument document = PdfDocument.Open(@"C:\path\to\pdffile\file.pdf")) { foreach (Page page in document.GetPages()) { IReadOnlyList letters = page.Letters; string example = string.Join(string.Empty, letters.Select(x => x.Value)); IEnumerable words = page.GetWords(); IEnumerable images = page.GetImages(); } } } } ``` -------------------------------- ### Register a TrueType font Source: https://uglytoad.github.io/PdfPig Registers a custom TrueType font from a file path to support non-ASCII text in the PDF document. ```csharp PdfDocumentBuilder builder = new PdfDocumentBuilder(); // You must provide a valid path to a .ttf file. byte[] robotoBytes = File.ReadAllBytes(@"C:\fonts\roboto.ttf"); PdfDocumentBuilder.AddedFont roboto = builder.AddTrueTypeFont(robotoBytes); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.