Make PDF or Word documents in C#

Create a new document in almost any format programmatically using our high fidelity C# library

Using our programming API, .NET developers can easily make a document in PDF, DOC, DOCX, HTML, EPUB, and many other formats with just a few lines of code.

View code snippet

Make a document using C#

With the given powerful API, C# developers can create documents in almost any format. To do this, you need to follow a few steps using our .NET library:

  1. Add file name
  2. Start creating a document using C#
  3. Save the created document in the selected format

It is worth noting that a blank document is technically supposed to contain one paragraph, so when you programmatically create a document, you will get exactly that basic document structure.

Note that you can instantly add content to a newly created document. Thus, you will get not just an empty document, but a document containing the necessary content. For more information on how to edit a document, see the Editing page.

Create a document in C# programmatically

The given .NET library allows you to programmatically create a document in any supported format – PDF, DOCX, DOC, RTF, ODT, EPUB, HTML and other.

Try our powerful functionality and see how to create a document in some formats using the following example:

Make a new document using C#
Select the target format from the list
Run code
using Aspose.Words;

var doc = new Document();
var builder = new DocumentBuilder(doc);

var font = builder.Font;
font.Name = "Courier New";            
font.Color = Color.Blue;
font.Size = 36;
font.HighlightColor = Color.Yellow;

builder.Write("Morbi enim nunc faucibus a.");

doc.Save("Output.docx");
using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); doc.Save("Output.docx"); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); var saveOptions = new PdfSaveOptions(); saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1); doc.Save("Output.docx", saveOptions);
using Aspose.Words;

var doc = new Document();

var run = new Run(doc, "Proin eros metus, sagittis sed.");
var para = doc.FirstSection.Body.FirstParagraph;
para.AppendChild(run);

var comment = new Comment(doc)
{
    Author = "John Doe",
    Initial = "JD",
    DateTime = DateTime.Now
};
comment.SetText("Quisque fringilla leo.");            

var commentRangeStart = new CommentRangeStart(doc, comment.Id);
var commentRangeEnd = new CommentRangeEnd(doc, comment.Id);

run.ParentNode.InsertBefore(commentRangeStart, run);
run.ParentNode.InsertAfter(commentRangeEnd, run);
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd);

comment.AddReply("Jane Doe", "JD", DateTime.Now, "Pellentesque vel sapien justo.");

doc.Save("Output.docx");
using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); doc.Save("Output.docx"); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); var saveOptions = new PdfSaveOptions(); saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1); doc.Save("Output.docx", saveOptions);
using Aspose.Words;

var doc = new Document();
var builder = new DocumentBuilder(doc);

var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);

builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// If NextSibling is null, then most likely this is the last Run in the Paragraph.
if (secondRun.NextSibling != null)            
    builder.MoveTo(secondRun.NextSibling);            
else            
    builder.MoveTo(secondRun.ParentParagraph);            
builder.EndBookmark("Aspose bookmark");

doc.Save("Output.docx");
using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); doc.Save("Output.docx"); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); var saveOptions = new PdfSaveOptions(); saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1); doc.Save("Output.docx", saveOptions);
using Aspose.Words;

var doc = new Document();
var builder = new DocumentBuilder(doc);

Shape shape = builder.InsertChart(ChartType.Pie, 432, 252);
Chart chart = shape.Chart;
chart.Title.Text = "Demo Chart";

chart.Series.Clear();
chart.Series.Add("Series 1",
    new string[] { "Category1", "Category2", "Category3" },
    new double[] { 2.7, 3.2, 0.8 });

doc.Save("Output.docx");
using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); doc.Save("Output.docx"); using Aspose.Words; var doc = new Document(); var builder = new DocumentBuilder(doc); var firstRun = new Run(doc, "Proin eros metus, sagittis sed. "); var secondRun = new Run(doc, "Morbi enim nunc faucibus a."); doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun); doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun); builder.MoveTo(secondRun); builder.StartBookmark("Aspose bookmark"); // If NextSibling is null, then most likely this is the last Run in the Paragraph. if (secondRun.NextSibling != null) builder.MoveTo(secondRun.NextSibling); else builder.MoveTo(secondRun.ParentParagraph); builder.EndBookmark("Aspose bookmark"); var saveOptions = new PdfSaveOptions(); saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1); doc.Save("Output.docx", saveOptions);
Run code

How to to make a document in C#

  1. Install Aspose.Words for .NET
  2. Add a library reference (import the library) to your C# project
  3. Create a new document
  4. Call the Save() method, passing the file name
  5. Get the result as a separate file

C# library to create documents

There are three alternative options to install "Aspose.Words for .NET" onto your system. Please choose one that resembles your needs and follow the step-by-step instructions:

System Requirements

Our product is fully cross-platform and supports all major .NET implementations:

  • .NET ≥ 5.0
  • .NET Core ≥ 2.0
  • .NET Standard ≥ 2.0
  • .NET Framework ≥ 3.5
  • MonoMac
  • MonoAndroid
  • Xamarin

As far as .NET code doesn't depend on the underlying hardware or operating system, but only on a Virtual Machine, you are free to develop any kind of software for Windows, macOS, Android, iOS and Linux. Just make sure you have installed the corresponding version of .NET Framework, .NET Core, Windows Azure, Mono or Xamarin.

We recommend using Microsoft Visual Studio, Xamarin, and MonoDevelop integrated development environments to create C#, F#, VB.NET applications.

Fore more details please refer to Product Documentation.

Most popular file formats

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.