How to Create New PDFs
Creating a new PDF involves generating a fresh PDF document from scratch, typically by adding text, images, or other elements programmatically. This can be accomplished using libraries like IronPDF in C#, which allow developers to define the content, layout, and formatting before saving the document as a PDF file.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet
Copy the code
new IronPdf.PdfDocument { DefaultPageSize = new IronPdf.PageSize(270,270) }.SaveAs("blankPage.pdf");
Deploy to test on your live environment
How to Create New PDFs
- Download IronPDF from NuGet for creating new PDFs
- Use the PdfDocument constructor to create a blank PDF
- Import existing PDF from file, URL, byte array, or JSON
- Convert multiple formats to PDFs such as HTML, RTF, Markdown, images, and XML
- Export the PDF document
Start using IronPDF in your project today with a free trial.
Create a Blank PDF Example
There are multiple ways to create a PDF object. Let's start with the simplest method, which requires only the width and height of the PDF. This PdfDocument
constructor will create a new blank PDF, ready for customization and use.
Code Example
:path=/static-assets/pdf/content-code-examples/how-to/create-new-pdfs.cs
using IronPdf;
PdfDocument pdf = new PdfDocument(270, 270);
pdf.SaveAs("blankPage.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
Import PDF from File
Before a PDF can be modified or converted, it must first be imported as a PdfDocument
object. Use the static FromFile
method of the PdfDocument
class to accomplish this. Importing PDFs from JSON, byte arrays, streams, and URLs is also supported within the same class.
Code Example
:path=/static-assets/pdf/content-code-examples/how-to/create-new-pdfs-from-file.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
pdf.SaveAs("export.pdf");
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
pdf.SaveAs("export.pdf")
Convert from Other Formats
In addition to the previously mentioned methods for obtaining a PdfDocument
object, you can also convert various formats into PDF. The most sophisticated method is converting HTML to PDF, which renders the HTML along with all its assets—including JavaScript, images, links, and CSS styling using the Chrome engine. Besides HTML, you can also convert other formats into PDFs, such as images, RTF, Markdown, and XML.
The following code snippet is an example of converting HTML to PDF.
The HTML conversion will take place using IronPDF's rendering engine to maintain the integrity of the content with all styling applied.
Code Example
:path=/static-assets/pdf/content-code-examples/how-to/create-new-pdfs-from-html.cs
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.PaperFit.UseResponsiveCssRendering(1800);
renderer.RenderingOptions.WaitFor.RenderDelay(5000);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
// Create a PDF from a URL or local file path
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
Imports IronPdf
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
renderer.RenderingOptions.EnableJavaScript = True
renderer.RenderingOptions.PrintHtmlBackgrounds = True
renderer.RenderingOptions.PaperFit.UseResponsiveCssRendering(1800)
renderer.RenderingOptions.WaitFor.RenderDelay(5000)
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
' Create a PDF from a URL or local file path
Dim pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Export to a file or Stream
pdf.SaveAs("url.pdf")
The generated PDF can be viewed using an embedded PDF viewer, as shown here. The below iframe would typically display the output PDF.
Frequently Asked Questions
How can I create a blank PDF document in C#?
To create a blank PDF document in C#, you can use IronPDF's PdfDocument
constructor. Simply define the size of the PDF using the PageSize
property and save it as a file using the SaveAs
method.
How do I install IronPDF for PDF creation?
You can install IronPDF via the NuGet package manager. Search for IronPDF in the manager, download, and install it to start creating PDFs programmatically.
How can I import a PDF file for modification?
You can import a PDF file using IronPDF by using the PdfDocument.FromFile
method. This allows you to load and modify an existing PDF file before saving your changes.
What formats can I convert to PDF using IronPDF?
IronPDF supports converting multiple formats to PDF, such as HTML, RTF, Markdown, images, and XML. This allows for a wide range of content types to be easily converted into high-quality PDFs.
How can I convert HTML to a PDF document?
To convert HTML to a PDF document using IronPDF, you can use the PdfDocument.FromHtml
method. This method renders the HTML content along with its assets like CSS and JavaScript into a PDF file.
Why is the Chrome engine beneficial for HTML to PDF conversion?
The Chrome engine is beneficial for HTML to PDF conversion because it ensures the rendered PDF maintains the original HTML content's integrity, including styling, JavaScript, links, and images.
Can I add new elements to an imported PDF?
Yes, after importing a PDF using IronPDF, you can add new elements such as text, images, or pages to the document before you save it as a new PDF file.