-
Notifications
You must be signed in to change notification settings - Fork 1
2.17 PDF Document Output
The PdfFileWriter
creates PDF documents. The main class PdfDocument
constructor gives you two choices to save the document. The first choice is to save the PDF file to a disk file. In this case you provide the constuctor with a file name. At the end of file creation, you call PdfDocument.CreateFile
. This method writes the PDF to the file and closes the file.
// create main class
PdfDocument Document = new PdfDocument(PaperType.Letter, false, UnitOfMeasure.Inch, FileName);
// terminate
Document.CreateFile();
The second choice is a stream. You create a stream, either memory stream or a file stream, and you pass the stream as an argument to the PdfDocument
constructor. After CreateFile
method is executed, your stream contains the PDF document. Extract the document from the stream as appropriate to your application. You must close the stream in your application.
// create memory stream
MemoryStream PdfStream = new MemoryStream();
// create main class
PdfDocument Document = new PdfDocument(PaperType.Letter, false, UnitOfMeasure.Inch, PdfStream);
// terminate
Document.CreateFile();
// save the memory stream to a file
FileStream FS = new FileStream(FileName, FileMode.Create);
PdfStream.WriteTo(FS);
PdfStream.Close();
FS.Close();
This page is a copy from https://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library by Uzi Granot. The article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL). All rights to the texts and source code remain with Uzi Granot.