.NET (1317)
Children categories
For PDF security, people are more likely to encrypt PDF file by setting up to two passwords: owner password and user password. The essential difference between these two passwords is that people can fully control the document by owner password, while only can open or have to subject to some restrictions by user password. This section will introduce a solution to easily encrypt your PDF by the two passwords via Spire.PDF for WPF.
Spire.PDF for WPF is a WPF PDF component, which can encrypt your PDF not only by owner password but also by restricting nine permissions when setting user password. In the solution, these two passwords are set by an object of PDFSecurity class which is contained in the namespace Spire.PDFDocument.Security.
Before you start, please feel free to download Spire.PDF for WPF first and encrypt your PDF file by below key steps after loading it.
Step1: Set PDF password size
In this step, three kinds of key size are applicable by the enum Spire.Pdf.Security.PdfEncryptionKeySize. They are: Key128Bit, Key256Bit and Key40Bit. You can choose any of the three according to your own situation.
doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit;
doc.Security.KeySize = PdfEncryptionKeySize.Key256Bit
Step 2: Secure PDF file by passwords
Owner password and user password are set to encrypt your PDF file. Please make sure that the password size should not be over the key size you set in last step.
doc.Security.OwnerPassword = "e-iceblue"; doc.Security.UserPassword = "pdf";
doc.Security.OwnerPassword = "e-iceblue" doc.Security.UserPassword = "pdf"
Step 3: Restrict certain permissions of user password
Nine access permissions of user password can be specified in the step, you can view them as below picture:

- AccessibilityCopyContent: copy accessibility content.
- AssembleDocument: assemble document permission. (Only for 128 bits key).
- CopyContent: copy content.
- Default: default value means users are authorized all permissions.
- EditAnnotations: add or modify text annotations, fill in interactive form fields.
- EditContent: edit content.
- FillFields: fill form fields. (Only for 128 bits key).
- FullQualityPrint: print document with full quality.
- Print: print document.
Here, three permissions are authorized: AccessibilityCopyContent, Print and EditAnnotations. Now, see following code:
doc.Security.Permissions = PdfPermissionsFlags.AccessibilityCopyContent | PdfPermissionsFlags.Print | PdfPermissionsFlags.EditAnnotations;
doc.Security.Permissions = PdfPermissionsFlags. AccessibilityCopyContent Or PdfPermissionsFlags. Print Or PdfPermissionsFlags.EditAnnotations
After you run the project, appears a dialog box in which you need to input password before opening the PDF document. You can look at the effective screenshot below:

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.
This section is designed to introduce a simple method to clearly convert text to PDF for WPF. During the text to PDF conversion task, font, font color, line space and other text format also can be quickly set according to your own need via a WPF PDF component Spire.PDF for WPF.
Spire.PDF for WPF will display a solution of two key steps to realize text to PDF task as well as customize text in PDF. Compared with some text to PDF converters, Spire.PDF for WPF not only has the function of convert HTML, text and image to PDF, but also owns the ability to read, write and manipulate PDF document without Adobe Acrobat or any third party component library.
Below screenshot gives you a clear view of the text to PDF conversion effect:

Please download Spire.PDF for WPF on system. Then, perform your text to PDF by below key steps.
Step1: Read all text information to s string
In this step, you need to read all the text from a text file to a string.
string text = File.ReadAllText(@"..\texttopdf.txt");
Dim text As String = File.ReadAllText("..\texttopdf.txt")
Step2: Draw string to PDF document with custom layout.
This step allows you to set the text font, font color, text format and position in PDF document at your own will by below code:
PdfPageBase page = section.Pages.Add();
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 20f;
float pageWidth = page.Canvas.ClientSize.Width;
PdfBrush brush = PdfBrushes.DarkBlue;
float y = 30;
PdfStringLayouter textLayouter = new PdfStringLayouter();
PdfStringLayoutResult result
= textLayouter.Layout(text, font, format, new SizeF(pageWidth, 0));
foreach (LineInfo line in result.Lines)
{
page.Canvas.DrawString(line.Text, font, brush, 0, y, format);
y = y + result.LineHeight;
}
Dim page As PdfPageBase = section.Pages.Add() Dim font As New PdfFont(PdfFontFamily.Helvetica, 11) Dim format As New PdfStringFormat() format.LineSpacing = 20F Dim pageWidth As Single = page.Canvas.ClientSize.Width Dim brush As PdfBrush = PdfBrushes.DarkBlue Dim y As Single = 30 Dim textLayouter As New PdfStringLayouter() Dim result As PdfStringLayoutResult = textLayouter.Layout(text, font, format, New SizeF(pageWidth, 0)) For Each line As LineInfo In result.Lines page.Canvas.DrawString(line.Text, font, brush, 0, y, format) y = y + result.LineHeight Next
Now that is all the core codes for draw text in PDF. Apart from converting text to PDF, Spire.PDF for WPF also can extract PDF text.
Word watermark can be text or image, which appears behind document contents and will not influence integrity of document body. It often adds interest or identifies document status, for example, marking document as private or important. This is the guide you are looking for about how to insert text watermark in Word with WPF.
Spire.Doc for WPF, a professional WPF component on manipulating Word document, enables users to insert text watermark in Word document and set format for this watermark. Comparing with “Insert Watermark” Command in Microsoft Word, Spire.Doc for WPF provides a Spire.Doc.TextWatermark class. Firstly initialize a new instance of Textwatermark with TextWatermark TXTWatermark = new TextWatermark() and then set text watermark as document watermark type by code:document.Watermark = TXTWatermark.
Besides, TextWatermark class includes several parameters for watermark formatting setting, including text, font style, layout etc. The following screenshot is the document without watermark. After the code below, you will find a result screenshot with text watermark being adding at the bottom of the page.

Download Spire.Doc for WPF and install it on your system. After adding button in MainWindow, double click this button to use the following code to insert text watermark in Word with WPF.
using System.Windows;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
namespace Doc_x_Watermark
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\work\Documents\New Zealand.docx");
//Insert Text Watermark and Set Format
TextWatermark TXTWatermark = new TextWatermark();
TXTWatermark.Text = "NZ Brief Introduction ";
TXTWatermark.FontSize = 45;
TXTWatermark.FontName = "Broadway BT";
TXTWatermark.Layout = WatermarkLayout.Diagonal;
TXTWatermark.Color = Color.Purple;
document.Watermark = TXTWatermark;
//Save and Launch
document.SaveToFile("TXTWatermark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("TXTWatermark.docx");
}
}
}
Imports System.Windows
Imports System.Drawing
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 document As New Document()
document.LoadFromFile("E:\work\Documents\New Zealand.docx")
'Insert Text Watermark and Set Format
Dim TXTWatermark As New TextWatermark()
TXTWatermark.Text = "NZ Brief Introduction"
TXTWatermark.FontSize = 45
TXTWatermark.FontName = "Broadway BT"
TXTWatermark.Layout = WatermarkLayout.Diagonal
TXTWatermark.Color = Color.Purple
document.Watermark = TXTWatermark
'Save and Launch
document.SaveToFile("TXTWatermark.docx", FileFormat.Docx)
System.Diagnostics.Process.Start("TXTWatermark.docx")
End Sub
End Class
After running, you can get the result as following:

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.
In Excel files, headers and footers provide rich content at the top and bottom of worksheet pages. The content can include a logo, company name, page number, date, and time, etc. This article shows you how to add text, images, as well as fields (like page numbers) to Excel headers or footers using Spire.XLS for .NET.
- Add an Image and Formatted Text to Excel Header
- Add the Current Date and Page Numbers to Excel Footer
Spire.XLS for .NET provides the PageSetup class to work with the page setup in Excel including headers and footers. Specifically, it contains LeftHeader property, CenterHeader property, RightHeader property, LeftFooter property, etc. to represent the left section, center section and right section of a header or footer. To add fields to headers or footers, or to apply formatting to text, you’ll need to use the scripts listed in the following table.
| Script | Description |
| &P | The current page numbers. |
| &N | The total number of pages. |
| &D | The current data. |
| &T | The current time. |
| &G | A picture. |
| &A | The worksheet name. |
| &F | The file name. |
| &B | Make text bold. |
| &I | Italicize text. |
| &U | Underline text. |
| &"font name" | Represents a font name, for example, &"Arial". |
| & + Integer | Represents font size, for example, &12. |
| &K + Hex color code | Represents font color, for example, &KFF0000. |
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Add an Image and Formatted Text to Excel Header
The following are the steps to add an image and formatted text to the header of an existing Excel document using Spire.XLS for .NET.
- Create a Workbook object, and load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through the Workbook.Worksheets[index] property.
- Load an image, scale it and set it as the image source of the left header through the PageSetup.LeftHeaderImage property.
- Display image in the left header section by setting the PageSetup.LeftHeader property to “&G”.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
using System.Drawing;
namespace AddImageAndTextToHeader
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook wb = new Workbook();
//Load an existing Excel file
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.Worksheets[0];
//Load an image
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\your-logo.png");
//Scale the image
Bitmap bitmap = new Bitmap(image, new Size(image.Width / 2, image.Height / 2));
//Add image to header’s left section
sheet.PageSetup.LeftHeaderImage = bitmap;
sheet.PageSetup.LeftHeader = "&G";
//Add formatted text to header’s right section
sheet.PageSetup.RightHeader = "&\"Calibri\"&B&10&K4253E2Your Company Name \n Goes Here";
//Save the file
wb.SaveToFile("Header.xlsx", ExcelVersion.Version2016);
}
}
}

Add the Current Date and Page Numbers to Excel Footer
The following are the steps to add the current date and page numbers to the footer of an existing Excel document using Spire.XLS for .NET.
- Create a Workbook object, and load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through the Workbook.Worksheets[index] property.
- Add page numbers with formatting to the left footer section by setting the PageSetup.LeftFooter property to “&\"Calibri\"&B&10&K4253E2Page &P”. You can customize the page numbers’ formatting according to your preference.
- Add the current date to the right footer section by setting the PageSetup.RightFooter property to “&\"Calibri\"&B&10&K4253E2&D”. Likewise, you can change the appearance of the date string as desired.
- Save the workbook to another Excel file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace AddDateAndPageNumberToFooter
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook object
Workbook wb = new Workbook();
//Load an exsting Excel file
wb.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx");
//Get the first worksheet
Worksheet sheet = wb.Worksheets[0];
//Add page number to footer's left section
sheet.PageSetup.LeftFooter = "&\"Calibri\"&B&10&K4253E2Page &P";
//Add current date to footer's right section
sheet.PageSetup.RightFooter = "&\"Calibri\"&B&10&K4253E2&D";
//Save the file
wb.SaveToFile("Footer.xlsx", ExcelVersion.Version2016);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
During software development, developers may need to convert Word to HTML. Different from many available ways on Internet, in this example, the solution for converting Word to HTML enables WPF developers load a predefined Doc, Docx, or RTF document, fill it with data, convert the document to HTML.
Spire.Doc for WPF is a professional component specializing in manipulating Word document for WPF. To convert a Word to HTML simply in WPF, developers need to invoke document.LoadFromFile(), which is used to load document and document.SaveToFile(), which is used to convert document.
The following screenshot shows parts of contents of original Word document. The target HTML is at the bottom after this coding. It is shown in Firefox, same as the original document.

Download and install Spire.Doc for WPF on your system. Then, add a button in MainWindow and double click it to use the following code to convert Word to HTML in WPF
using Spire.Doc;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\work\Documents\Humor Them.docx");
//Convert to HTML
document.SaveToFile("ToHTML.html", FileFormat.Html);
//Launch Document
System.Diagnostics.Process.Start("ToHTML.html");
}
}
}
Imports Spire.Doc
Imports System.Windows
Namespace WpfApplication1
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\work\Documents\Humor Them.docx")
'Convert to HTML
document.SaveToFile("ToHTML.html", FileFormat.Html)
'Launch Document
System.Diagnostics.Process.Start("ToHTML.html")
End Sub
End Class
End Namespace
After running, you can get the result as following:

Spire.Doc is an 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.
Software developers are often asked to find a way to convert Microsoft Excel into PDF as PDF files are widely used for exchanging documents between organizations, government sectors and individuals. This solution demonstrates a way of converting Excel to PDF for WPF developers and maintains high visual fidelity in the conversion.
Spire.XLS for WPF is a WPF Excel component which enables your WPF applications to fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. It also fully supports converting files from Excel to PDF, Excel to HTML, Excel to CSV, Excel to Text, Excel to Image and Excel to XML. All the conversion is independent of any other software.
Any kind of trial and evaluation is always welcomed. Please feel free to download Spire.XLS for WPF to have a trial and convert your Excel to PDF for personal use. Below is a screenshot of the source Excel file we load in this demo. At the end, a screenshot of PDF will be demonstrated for comparison with the source Excel file.

Now this is the key procedure for converting Excel to PDF in WPF.
Step 1: Load Excel file or Create Excel from scratch.
In this step, instantiate an object of the Workbook class by calling its empty constructor and then you may open/load an existing template file or skip this step if you are creating the workbook from scratch.
// load Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\test.xlsx");
[VB.NET]
' load Excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")
Step 2: Set PDF template.
In this step, we will set PDF template which will be used below.
// Set PDF template PdfDocument pdfDocument = new PdfDocument(); pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape; pdfDocument.PageSettings.Width = 970; pdfDocument.PageSettings.Height = 850;
' Set PDF template Dim pdfDocument As New PdfDocument() pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape pdfDocument.PageSettings.Width = 970 pdfDocument.PageSettings.Height = 850
Step 3: Convert Excel to PDF in WPF.
Now we will use the template which we have already set above to convert Excel to PDF in WPF.
//Convert Excel to PDF using the template above PdfConverter pdfConverter = new PdfConverter(workbook); PdfConverterSettings settings = new PdfConverterSettings(); settings.TemplateDocument = pdfDocument; pdfDocument = pdfConverter.Convert(settings);
'Convert Excel to PDF using the template above Dim pdfConverter As New PdfConverter(workbook) Dim settings As New PdfConverterSettings() settings.TemplateDocument = pdfDocument pdfDocument = pdfConverter.Convert(settings)
Step 4: Save and preview PDF.
Please use the codes below to save and preview PDF.
// Save and preview PDF
pdfDocument.SaveToFile("sample.pdf");
System.Diagnostics.Process.Start("sample.pdf");
' Save and preview PDF
pdfDocument.SaveToFile("sample.pdf")
System.Diagnostics.Process.Start("sample.pdf")
The picture below is a screenshot of the PDF, Please see:


doc.LoadFromFile(@"..\Sample.pdf"); System.Drawing.Image image = System.Drawing.Image.FromFile(@"..\MS.jpg");
doc.LoadFromFile("..\Sample.pdf")
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("..\MS.jpg")
//adjust image size int width = image.Width; int height = image.Height; float schale = 0.7f; System.Drawing.Size size = new System.Drawing.Size((int)(width * schale), (int)(height * schale)); Bitmap schaleImage = new Bitmap(image, size); //draw image into the first PDF page at specific position PdfImage pdfImage = PdfImage.FromImage(schaleImage); PdfPageBase page0 = doc.Pages[0]; PointF position = new PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10); page0.Canvas.DrawImage(pdfImage, position);
'adjust image size Dim width As Integer = image.Width Dim height As Integer = image.Height Dim schale As Single = 0.7F Dim size As New System.Drawing.Size(CInt(Math.Truncate(width * schale)), CInt(Math.Truncate(height * schale))) Dim schaleImage As New Bitmap(image, size) 'draw image into the first PDF page at specific position Dim pdfImage__1 As PdfImage = PdfImage.FromImage(schaleImage) Dim page0 As PdfPageBase = doc.Pages(0) Dim position As New PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10) page0.Canvas.DrawImage(pdfImage__1, position)