PM > Install-Package XDoc.PDF

How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Report Generator Library
How to create a new pdf file using C# in ASP.NET, Windows app


C# sample code to Create PDF Document from Word in C# Program with .NET XDoc.PDF Component





In this C# tutorial, you will learn how to create a new PDF report file, and start inserting text, image contents on it using C# in ASP.NET MVC Web, Windows applications.

  • Quick to create a new PDF file
  • Insert new page with page size applied
  • Change page margins
  • Insert text content on PDF page

How to create a new PDF report file using C#

  1. Download XDoc.PDF Report Generator C# library
  2. Install C# library to create PDF document
  3. Step by Step Tutorial














Create a "Hello World" file


Below are the steps and C# sample source code to create a new PDF file and print a text message on it using C#.

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Open the document object, start editing the PDF content
  3. Add a new paragraph with text "Hello World" to the PDF document
  4. Save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample1.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  add content to the document
document.Add(new Paragraph("Hello World"));

//  close document and save to the output file
document.Close();




Add a new page to the PDF document in C#


The following C# sample code will add new PDF pages with text inserted.

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Open the document object, start editing the PDF content
  3. Add a new PDF page with a new text paragraph "This is the 1st page."
  4. Add another new PDF page with a text paragraph "This is the 2nd page."
  5. Save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample2.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

document.Add(new Paragraph("This is the 1st page."));
//  add a new page and move current position to the next page
document.NewPage();
document.Add(new Paragraph("This is the 2nd page."));
//  add a new page and move current position to the next page
document.NewPage();

//  close document and save to the output file
document.Close();




Create a PDF file with paragraphs using C#


String outputFilePath = Program.RootPath + "\\" + "Sample3.pdf";

String[] contents = new String[] {
    "Charles Babbage, an English mechanical engineer and polymath, originated the concept of a programmable computer. Considered the \"father of the computer\", he conceptualized and invented the first mechanical computer in the early 19th century. After working on his revolutionary difference engine, designed to aid in navigational calculations, in 1833 he realized that a much more general design, an Analytical Engine, was possible. The input of programs and data was to be provided to the machine via punched cards, a method being used at the time to direct mechanical looms such as the Jacquard loom. For output, the machine would have a printer, a curve plotter and a bell. The machine would also be able to punch numbers onto cards to be read in later. The Engine incorporated an arithmetic logic unit, control flow in the form of conditional branching and loops, and integrated memory, making it the first design for a general-purpose computer that could be described in modern terms as Turing-complete.",
    "The machine was about a century ahead of its time. All the parts for his machine had to be made by hand —this was a major problem for a device with thousands of parts. Eventually, the project was dissolved with the decision of the British Government to cease funding. Babbage's failure to complete the analytical engine can be chiefly attributed to difficulties not only of politics and financing, but also to his desire to develop an increasingly sophisticated computer and to move ahead faster than anyone else could follow. Nevertheless, his son, Henry Babbage, completed a simplified version of the analytical engine's computing unit (the mill) in 1888. He gave a successful demonstration of its use in computing tables in 1906.",
    "The principle of the modern computer was proposed by Alan Turing, in his seminal 1936 paper. Turing proposed a simple device that he called \"Universal Computing machine\" that is later known as a Universal Turing machine. He proved that such machine is capable of computing anything that is computable by executing instructions (program) stored on tape, allowing the machine to be programmable. The fundamental concept of Turing's design is stored program, where all instruction for computing is stored in the memory. Von Neumann acknowledged that the central concept of the modern computer was due to this paper. Turing machines are to this day a central object of study in theory of computation. Except for the limitations imposed by their finite memory stores, modern computers are said to be Turing-complete, which is to say, they have algorithm execution capability equivalent to a universal Turing machine.",
    "Early computing machines had fixed programs. Changing its function required the re-wiring and re-structuring of the machine. With the proposal of the stored-program computer this changed. A stored-program computer includes by design an instruction set and can store in memory a set of instructions (a program) that details the computation. The theoretical basis for the stored-program computer was laid by Alan Turing in his 1936 paper. In 1945 Turing joined the National Physical Laboratory and began work on developing an electronic stored-program digital computer. His 1945 report ‘Proposed Electronic Calculator’was the first specification for such a device. John von Neumann at the University of Pennsylvania also circulated his First Draft of a Report on the EDVAC in 1945.",
    "The Manchester Small-Scale Experimental Machine, nicknamed Baby, was the world's first stored-program computer. It was built at the Victoria University of Manchester by Frederic C. Williams, Tom Kilburn and Geoff Tootill, and ran its first program on 21 June 1948. It was designed as a testbed for the Williams tube the first random-access digital storage device. Although the computer was considered \"small and primitive\" by the standards of its time, it was the first working machine to contain all of the elements essential to a modern electronic computer. As soon as the SSEM had demonstrated the feasibility of its design, a project was initiated at the university to develop it into a more usable computer, the Manchester Mark 1."
};

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

//  add content to the document
foreach (String content in contents)
{
    document.Add(new Paragraph(content));
}

//  close document and save to the output file
document.Close();






Set page size for a PDF document in C#


Below are the steps and C# sample source code to create a new PDF file with standard "A4" page size (8.27 in. x 11.69 in.).

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Call method SetPageSize() to set page size to "A4"
  3. Open the document object
  4. Insert a text paragraph "Page Size: A4" in the new page.
  5. Add a new Page with size PaperSize.Letter, and insert a text "Page Size: Letter" in the new page.
  6. Add a new Page with size SetPageSize(4F, 3F), and insert a text "Page Size: Width = 4 inches; Height = 3 inches" in the new page
  7. Save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample4.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

document.SetPageSize(PaperSize.A4);

//  open document
document.Open();

document.Add(new Paragraph("Page Size: A4"));

document.SetPageSize(PaperSize.Letter);
document.NewPage();
document.Add(new Paragraph("Page Size: Letter"));

document.SetPageSize(4F, 3F);
document.NewPage();
document.Add(new Paragraph("Page Size: Width = 4 inches; Height = 3 inches"));

//  close document and save to the output file
document.Close();




Set page margins for a PDF document in C#


Below are the steps and C# sample source code to create a new PDF file page margins applied.

  1. Use PDFBuildHandler.Create() to create a new PDF document
  2. Call method SetMargins() to set page margins
  3. Open the document object
  4. Insert a text paragraph "Left Margin = 0; Right Margin = 0; Top Margin = 0; Bottom Margin = 0" in the new page.
  5. Add a new Page with new page margins applied
  6. Insert a text paragraph in the new page
  7. Save the PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample5.pdf";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

float leftMargin = 0F;
float rightMargin = 0F;
float topMargin = 0F;
float bottomMargin = 0F;
document.SetMargins(leftMargin, rightMargin, topMargin, bottomMargin);

//  open document
document.Open();

//  add content to the document
document.Add(new Paragraph("Left Margin = 0; Right Margin = 0; Top Margin = 0; Bottom Margin = 0"));

leftMargin = 144F;
rightMargin = 144F;
topMargin = 144F;
bottomMargin = 144F;
document.SetMargins(leftMargin, rightMargin, topMargin, bottomMargin);
document.NewPage();

//  add content to the document
document.Add(new Paragraph("Left Margin = 144 pt. (2 inches); Right Margin = 144 pt. (2 inches); Top Margin = 144 pt. (2 inches); Bottom Margin = 144 pt. (2 inches)"));

//  close document and save to the output file
document.Close();