I always use Spire.PDF for .NET to realize a PDF function in .NET, Now since Spire.PDF for WPF has published long ago. It is time for me to use Spire.PDF for WPF to begin my WPF articles.
You can Freely Download Spire.PDF for WPF
How to create PDF file in WPF with C#, VB.NET
Spire.PDF for WPF is a WPF PDF component which enables your WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat. Please look at the below procedure.
Step1. Create a new project.
- Create a new project in WPF Application.
- Set the target Framework in Properties of the project to be .NET Framework 4.
- Add a button in MainWindow.
- Add Spire.Pdf.Wpf.dll and System. Drawing as references.
- Add namespaces to the top of the method.
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
VB.NET Code:
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Step2. Create a PDF document.
- Create a new PDF file and add a blank page in it.
//create a new pdf document
PdfDocument pdfDoc = new PdfDocument();
//add one blank page
PdfPageBase page = pdfDoc.Pages.Add();
VB.NET Code:
'create a new pdf document
Dim pdfDoc As New PdfDocument()
'add one blank page
Dim page As PdfPageBase = pdfDoc.Pages.Add()
2. Draw text in PDF file.
//draw a string on the blank page
PdfFont font = new PdfFont(PdfFontFamily.Courier, 14f);
page.Canvas.DrawString("This sample demonstrates how to write \"Hello, World!\" \n by using Spire.Pdf for WPF!", font, PdfBrushes.Green, new System.Drawing.PointF(0, 20f));
VB.NET Code:
'draw a string on the blank page
Dim font As New PdfFont(PdfFontFamily.Courier, 14F)
page.Canvas.DrawString("This sample demonstrates how to write ""Hello, World!"" " & vbLf & " by using Spire.Pdf for WPF!", font, PdfBrushes.Green, New System.Drawing.PointF(0, 20F))
Step3. Save and launch the file.
C# Code:
//save the pdf document
pdfDoc.SaveToFile(@"sample.pdf");
//launch the pdf document
System.Diagnostics.Process.Start(@"sample.pdf");
VB.NET Code:
'save the pdf document
pdfDoc.SaveToFile("sample.pdf")
'launch the pdf document
System.Diagnostics.Process.Start("sample.pdf")
Preview