iTextSharp is a powerful and flexible library for generating PDF files in C#. It is an open-source library that allows developers to easily create, manipulate, and modify PDF documents. The iTextSharp.text.Document.Close method is used to close an instance of the Document class. It finalizes the document, flushing all content to the output stream and releasing all resources used by the document. Here are some code examples using the Document.Close method:
Example 1: using iTextSharp.text; using iTextSharp.text.pdf; using System.IO;
// Creating a new PDF document Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("example.pdf", FileMode.Create));
// Opening the document document.Open();
// Adding content to the document document.Add(new Paragraph("Hello World!"));
// Closing the document document.Close();
In this example, we create a new PDF document, add some content to it, and then close it. The resulting PDF file will be saved in the "example.pdf" file.
Example 2: using iTextSharp.text; using iTextSharp.text.pdf; using System.IO;
// Creating a new PDF document Document document = new Document();
try { // Creating a PdfWriter PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("example.pdf", FileMode.Create));
// Opening the document document.Open();
// Adding content to the document document.Add(new Paragraph("Hello World!"));
// Closing the document document.Close(); } catch(Exception ex) { // Handle any exceptions that occurred } finally { // Dispose of the document document.Dispose(); }
In this example, we create a new PDF document inside a try-catch-finally block. If an exception occurs, we handle it in the catch block. In the finally block, we dispose of the document to release any resources it was using.
These examples use the iTextSharp NuGet package/library.
C# (CSharp) iTextSharp.text Document.Close - 60 examples found. These are the top rated real world C# (CSharp) examples of iTextSharp.text.Document.Close extracted from open source projects. You can rate examples to help us improve the quality of examples.