Knowledgebase (2311)
Children categories
This section will introduce an easy solution to convert image to PDF in WPF. In my method, image to PDF conversion is just a piece of cake since various image formats such as jpg, png and bmp, even images of gif, tif and ico can be converted to PDF through two key steps for Spire.PDF users.
Spire.PDF for WPF,a professional WPF PDF component, enables your WPF applications to read, write and manipulate PDF document without Adobe Acrobat or any third party component library. As for image to PDF conversion, apart from clearly converting images of different formats to PDF, Spire.PDF for WPF also allows you to directly load your images to PDF files from stream. Please feel free to Download Spire.PDF and give it a try following our programming guide below. The following is a picture which we will convert to PDF. At the bottom of this article, a target PDF will be presented.

Now it's time for the key procedure of image to PDF conversion. To realize the key procedure, we need below two steps.
Step 1: Load an image file from system
An image file is required in this step. The image format can be any format among jpg, png, bmp, gif, tif and ico. Here a jpg image is loaded.
//Load a jpg image from system PdfImage image = PdfImage.FromFile(@"..\sky.jpg");
'Load a jpg image from system
Dim image As PdfImage = PdfImage.FromFile("..\sky.jpg")
Step 2: Set the image location and size to fit PDF page
Spire.PDF for WPF contains a namespace “Spire.Pdf.Graphics” in which image size and location are set through four parameters: Width/Height for image size and fitWidth/fitHeight for image location.
//Set image display location and size in PDF float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width; float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height; float fitRate = Math.Max(widthFitRate, heightFitRate); float fitWidth = image.PhysicalDimension.Width / fitRate; float fitHeight = image.PhysicalDimension.Height / fitRate; page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight);
'Set image display location and size in PDF Dim widthFitRate As Single = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width Dim heightFitRate As Single = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate) Dim fitWidth As Single = image.PhysicalDimension.Width / fitRate Dim fitHeight As Single = image.PhysicalDimension.Height / fitRate page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight)
After this coding, run your application, you can find a target PDF as below.

HTML to PDF solution can be quite a few when people search on google. However, most solutions are not proper in use since what you view on webpage is not always completely same with what you get on your PDF. Some information such as dynamic image and anchor text in HTML are easily lost. This section will introduce a simple method to clearly convert HTML to PDF without any cut on WPF platform.
Spire.PDF for WPF, as a professional PDF component, allows you to clearly convert HTML to PDF only by three lines of key code on WPF platform. In the solution, you only need to load HTML file with four parameters:
- URL, the url of the webpage which will be converted to PDF file.
- Enable JavaScrpit, indicates whether enables the JavaScript of the webpage.
- Enable hyperlink, indicates whether enables the hyperlink of the webpage.
- Auto detect page break, indicates whether splits auto the web page in the result PDF file.
Then, save your html as PDF to file by calling the method PdfDocument.LoadFromHTM. Please preview the result screenshot of the whole project first as prove:

Before demonstrating the code, please feel free to download Spire.PDF and install it on system. Now, you can view the full code below:
using Spire.Pdf;
namespace WPFhtmltopdf
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[STAThread]
private void button1_Click(object sender, RoutedEventArgs e)
{
//Create a new pdf document.
PdfDocument doc = new PdfDocument();
//load the webpage
String url = "http://msdn.microsoft.com/";
doc.LoadFromHTML(url, false, true, true);
//Save html webpage as pdf file.
doc.SaveToFile("sample.pdf");
doc.Close();
}
}
}
Imports Spire.Pdf
Namespace WPFhtmltopdf
'''
''' Interaction logic for MainWindow.xaml
'''
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
_
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
'Create a new pdf document.
Dim doc As New PdfDocument()
'load the webpage
Dim url As = "http://msdn.microsoft.com/"
doc.LoadFromHTML(url, False, True, True)
'Save html webpage as pdf file.
doc.SaveToFile("sample.pdf")
doc.Close()
End Sub
End Class
End Namespace
Obviously, using this WPF PDF component, the webpage is easily converted to PDF. Both text and images are clearly shown in PDF file. Besides, Spire.PDF for WPF is a 100% secure PDF component software. No Malware, No Spyware and No Virus.
Word Page Break is used to start with contents in a new page, which can be inserted anywhere in Word document. Generally speaking, page break can be generated automatically when one page is filled with contents, or users can specify where Microsoft Word positions automatic page break. Also, users can insert manual page breaks in document to keep some paragraphs together in a single page.
Spire.Doc for WPF, a professional WPF component on manipulating Word document, enables users to insert page break in Word with WPF. Users can invoke paragraph.Append(BreakType.PageBreak) method directly to insert page break in Word.
Spire.Doc presents an easy way to insert a page break. Just follow the simple steps below to insert a page break.
The following screenshot presents document before inserting page break.

Download and install Spire.Doc for WPF. Add button in MainWindow and double click this button to use the following code to insert page break with WPF in Word.
using System.Windows;
using Spire.Doc;
using Spire.Doc.Documents;
namespace Doc_x_PageBreak
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Load Document
Document doc = new Document();
doc.LoadFromFile(@"E:\work\documents\Blues Introduction.docx");
//Get Paragraph Position
Section section = doc.Sections[0];
Paragraph paragraph = section.Paragraphs[1];
//Insert Page Break
paragraph.AppendBreak(BreakType.PageBreak);
//Save and Launch
doc.SaveToFile("PageBreak.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("PageBreak.docx");
}
}
}
Imports System.Windows
Imports Spire.Doc
Imports Spire.Doc.Documents
Class MainWindow
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
'Load Document
Dim doc As New Document()
doc.LoadFromFile("E:\work\documents\Blues Introduction.docx")
'Get Paragraph Position
Dim section As Section = doc.Sections(0)
Dim paragraph As Paragraph = section.Paragraphs(1)
'Insert Page Break
paragraph.AppendBreak(BreakType.PageBreak)
'Save and Launch
doc.SaveToFile("PageBreak.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("PageBreak.docx")
End Sub
End Class
Effective Screenshot:

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.