Create PDF Files On the Fly in C#

CK CK 3788 
Created at
Updated at  
1,003 0 0

Creation of PDF Document in 5 easy steps

Step 1: First create an instance of document object

Document myDocument= new Document(PageSize.A4.Rotate());


Step 2: Now create a writer that listens to this doucment and writes the document to desired Stream.

PdfWriter.GetInstance(myDocument,new FileStream("Salman.pdf",FileMode.Create));


Step 3: Open the document now using

myDocument.Open();


Step 4: Now add some contents to the document

myDocument.add( new Paragraph ( "First Pdf File made by Salman using iText"));


Step 5: Remember to close the documnet

myDocument.close();


Here's the actual code generating PDF on the fly.

// Code

using System;
using System.IO;
using System.Diagnostics;

using iTextSharp.text;
using iTextSharp.text.pdf;

public class iTextDemo 
{
 public static void Main() 
 {
  Console.WriteLine("iText Demo");
        
  // step 1: creation of a document-object
  Document myDocument = new Document(PageSize.A4.Rotate());
        
  try 
  {
            
   // step 2:
   // Now create a writer that listens to this doucment and writes the document to desired Stream.
            
   PdfWriter.GetInstance(myDocument,new FileStream("Salman.pdf",FileMode.Create));
            
   // step 3:  Open the document now using
   myDocument.Open();
            
   // step 4: Now add some contents to the document
   myDocument.Add(new Paragraph("First Pdf File made by Salman using iText"));
            
  }
  catch(DocumentException de) 
  {
   Console.Error.WriteLine(de.Message);
  }
  catch(IOException ioe) 
  {
   Console.Error.WriteLine(ioe.Message);
  }
        
  // step 5: Remember to close the documnet
  
  myDocument.Close();
 }
}

 

Tags Create PDF FileStream PDF PDF Creation PDF Generation PdfWriter WriteLine Facebook X
Comments 0
Similar posts
  1. Python code to convert PDF file to Excel Spreadsheet
    700
  1. Simplified .NET Printing In C#
    692
  2. How To Print Text In C#
    455
  3. C# Lists
    1,151
  4. Simple Example of IF/THEN using C#
    1,265
  5. Fast and Good Keyboard/Mouse Test without the message handler
    982
  6. Simple CGI Programming in C
    545
  7. C++ CGI Example working on Apache
    484
  8. Simple C# CGI working on Apache
    481
  9. How to send binary data through C# CGI app?
    994
  10. Interacting With TinyPic From C#
    1,303
  11. Creating A Watched Folder With Assigned Events
    1,658
  12. Calling Your Main Thread From A Worker Thread In C#
    546
  13. HashTable Tutorial In C#
    1,787
  14. Stack Tutorial In C#
    668
  15. Queue Tutorial In C#
    1,737
  16. How to call Visual C/C++ implemented DLL functions in C# - Simple DLLImport
    1,160
  17. Send Email Using C#
    1,377
  18. How To Read And Write A Cookie
    363
  19. Using C# With Cookies
    625
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,612
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    244
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    217
  30. Understanding Data Lineage
    260