In this tutorial, you will learn how you can use PDF (Generator) SDK to create and modify PDF files using C# source code.
PDF (Generator) SDK is a complete toolkit of enhanced PDF engines working in C# and VB.NET that can create a completely new PDF file or add content to the existing PDF).
In this short guide, we will demonstrate the following features of PDF (Generator) SDK in C#:
The source code snippet can be used to easily merge 2 PDF documents using PDF SDK in C#.
Just copy-paste the following C# source code to see the program in action.
using System.Diagnostics; using Bytescout.PDF; namespace CopyPagesFromOneDocumentToAnother { class Program { static void Main(string[] args) { // Open first document Document document1 = new Document("document1.pdf"); document1.RegistrationName = "demo"; document1.RegistrationKey = "demo"; // Open second document Document document2 = new Document("document2.pdf"); document2.RegistrationName = "demo"; document2.RegistrationKey = "demo"; // Add pages from document2 to document1 for (int i = 0; i < document2.Pages.Count; ++i) { document1.Pages.Add(document2.Pages[i]); } // Save merged document document1.Save("MergedDocument.pdf"); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("MergedDocument.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } } }
You can use this source code sample to quickly insert a page to PDF using PDF SDK in C#.
You can see the program in action and add a PDF page using the following C# source code.
using System.Diagnostics; using Bytescout.PDF; namespace InsertPageToPDF { class Program { static void Main(string[] args) { // Open first document Document document1 = new Document("document1.pdf"); document1.RegistrationName = "demo"; document1.RegistrationKey = "demo"; // Open second document Document document2 = new Document("document2.pdf"); document2.RegistrationName = "demo"; document2.RegistrationKey = "demo"; // Page index to insert document int pageIndexToAdd = 1; // Add pages from document2 to document1 for (int i = 0; i < document2.Pages.Count; ++i) { document1.Pages.Insert((pageIndexToAdd++), document2.Pages[i]); } // Save merged document document1.Save("MergedDocument.pdf"); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("MergedDocument.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } } }
The following source code sample is used for PDF form filling with the help of PDF SDK in C#.
In order to see the program how to fill a PDF form, just copy-paste the C# snippet below.
using System; using System.Diagnostics; using Bytescout.PDF; namespace FillFormExample { /// <summary> /// This example demonstrates how to fill PDF form programmatically. /// </summary> class Program { static void Main() { // Load PDF form Document pdfDocument = new Document(@"form.pdf"); pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; Page page = pdfDocument.Pages[0]; // Get widget by its name and change value ((EditBox) page.Annotations["editBox1"]).Text = "Test 123"; ((EditBox) page.Annotations["editBox2"]).Text = "Test 456"; ((CheckBox) page.Annotations["checkBox1"]).Checked = true; // Save modified document pdfDocument.Save("result.pdf"); // Cleanup pdfDocument.Dispose(); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } } }
This C# source code sample would be used to quickly create and use a template in PDF using PDF SDK in C#.
Just copy-paste the following C# source code to see the program in action.
using System.Diagnostics; using System.Drawing; using Bytescout.PDF; using Font = Bytescout.PDF.Font; using SolidBrush = Bytescout.PDF.SolidBrush; namespace Templates { /// <summary> /// This example demonstrates how to use templates. /// </summary> class Program { static void Main() { // Create new document Document pdfDocument = new Document(); pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; // Prepare simple template (for example, a logo) and draw it on every page // Create template of specified size GraphicsTemplate template = new GraphicsTemplate(250, 50); // Draw the logo template.DrawRectangle(new SolidBrush(new ColorRGB(192, 192, 255)), 0, 0, 250, 50); template.DrawString("My Company Logo", new Font(StandardFonts.TimesBoldItalic, 24), new SolidBrush(), 30, 10); // Add few pages and draw the prepared template on each one for (int i = 0; i < 3; i++) { Page page = new Page(PaperFormat.A4); page.Canvas.DrawTemplate(template, 10, 10); pdfDocument.Pages.Add(page); } // Save document to file pdfDocument.Save("result.pdf"); // Cleanup pdfDocument.Dispose(); // Open result document in default associated application (for demo purpose) ProcessStartInfo processStartInfo = new ProcessStartInfo("result.pdf"); processStartInfo.UseShellExecute = true; Process.Start(processStartInfo); } } }
This C# source code snippet might be used for Reg Ex search and highlights in PDF – see this tutorial using PDF SDK in C#.
Just use the C# source code snippet below to see the program in action.
using System.Diagnostics; using System.Drawing; using Bytescout.PDFExtractor; using Bytescout.PDF; using Pen = Bytescout.PDF.Pen; using SolidBrush = Bytescout.PDF.SolidBrush; namespace SearchAndHighlightExample { class Program { static void Main(string[] args) { const string inputFile = @"sample.pdf"; const int pageIndex = 0; const string searchPattern = "\\d+\\.\\d+"; // Prepare TextExtractor using (TextExtractor textExtractor = new TextExtractor("demo", "demo")) { textExtractor.RegexSearch = true; textExtractor.LoadDocumentFromFile(inputFile); // Load document with PDF SDK using (Document pdfDocument = new Document(inputFile)) { pdfDocument.RegistrationName = "demo"; pdfDocument.RegistrationKey = "demo"; Page pdfDocumentPage = pdfDocument.Pages[pageIndex]; Canvas canvas = pdfDocumentPage.Canvas; SolidBrush fillBrush = new SolidBrush(new ColorRGB(255, 0, 0)); fillBrush.Opacity = 50; // make the brush transparent // Search for pattern and highlight found pieces if (textExtractor.Find(pageIndex, searchPattern, caseSensitive: false)) { do { foreach (var foundPiece in textExtractor.FoundText.Elements) { // Inflate the rectangle a bit RectangleF rect = RectangleF.Inflate(foundPiece.Bounds, 1, 2); // Draw rectangle over the PDF page canvas.DrawRectangle(fillBrush, rect); } } while (textExtractor.FindNext()); } // Save as new PDF document pdfDocument.Save("result.pdf"); // Open result document in default associated application (for demo purposes) Process.Start("result.pdf"); } } } } }
The following video guide would be also useful:
The above code snippets demonstrate just a few of the multiple functionalities PDF (Generator) SDK can offer. Check the documentation for more.