Spire.PDF (77)
Spire.PDF enables your .NET application to convert image to PDF. If you have a lot of images, photos or pictures with any format such as jpg, PNG, bmp, tiff, etc. you can use Spire.PDF to convert all of them to PDF format. With special features, Spire.PDF can help you encrypt them and watermark them when you convert.
Download Spire.PDF (or Spire.Office) with .NET framework 2.0 (or above) together. Create a project and add Spire.PDF dll as reference. Copy the C#/VB.NET code below into the project to convert files from Image to PDF.
Note: Please make sure Spire.PDF and Visual Studio are correctly installed on system.
Convert Image to PDF with C# Code:
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
PdfSection section = doc.Sections.Add();
PdfPageBase page = doc.Pages.Add();
PdfImage image = PdfImage.FromFile("test.png");
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, 0, 0, fitWidth, fitHeight);
doc.SaveToFile("test.pdf");
doc.Close();
}
}
}
Convert Image to PDF with VB.NET Code:
Imports System.Text
Imports Spire.Pdf
Imports System.Drawing.Imaging
Imports Spire.Pdf.Graphics
Namespace ConsoleTest
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim doc As New PdfDocument()
Dim section As PdfSection = doc.Sections.Add()
Dim page As PdfPageBase = doc.Pages.Add()
Dim image As PdfImage = PdfImage.FromFile("test.png")
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, 0, 0, fitWidth, fitHeight)
doc.SaveToFile("test.pdf")
doc.Close()
End Sub
End Class
End Namespace
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. Click to learn more...

Spire.Doc, as a powerful .NET Word component, besides convert files from popular office format between Word doc/docx, it can also convert RTF to PDF. Totally programmatically converting RTF documents to PDF without Microsoft Office installed on your system! Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight.
Download Spire.Doc (or Spire.Office) with .NET framework together and use the C#/VB.NET code below to convert RTF to PDF.
Convert RTF to PDF with C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
namespace RTF2Pdf
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("sample.rtf", FileFormat.RTF);
doc.SaveToFile("test.pdf", FileFormat.PDF);
}
}
}
Convert RTF to PDF with VB.NET Code:
Imports System.Text
Imports Spire.Doc
Namespace RTF2Pdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim doc As New Document()
doc.LoadFromFile("sample.rtf", FileFormat.RTF)
doc.SaveToFile("test.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace

How to Use Spire.PDF for .NET Split PDF Documents
using System;
using Spire.Pdf;
namespace SplitDocument
{
class Program
{
static void Main(string[] args)
{
//open pdf document
PdfDocument doc = new PdfDocument(@"..\..\..\..\..\..\Data\Sample3.pdf");
String pattern = "SplitDocument-{0}.pdf";
doc.Split(pattern);
String lastPageFileName
= String.Format(pattern, doc.Pages.Count - 1);
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start(lastPageFileName);
}
}
}
Imports Spire.Pdf
Namespace SplitDocument
Friend Class Program
Shared Sub Main(ByVal args() As String)
'open pdf document
Dim doc As New PdfDocument("..\..\..\..\..\..\Data\Sample3.pdf")
Dim pattern As String = "SplitDocument-{0}.pdf"
doc.Split(pattern)
Dim lastPageFileName As String = String.Format(pattern, doc.Pages.Count - 1)
doc.Close()
'Launching the Pdf file.
Process.Start(lastPageFileName)
End Sub
End Class
End Namespace
How to Create a PDF Document via Spire.PDF
Step 1 Prepare
Step 2 Create a PDF Document
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Create one page
PdfPageBase page = doc.Pages.Add();
Step 3 Write Text Content
page.Canvas.DrawString("Hello, World!",
new PdfFont(PdfFontFamily.Helvetica, 30f),
new PdfSolidBrush(Color.Black),
10, 10);
Step 4 Save and Preview
//Save pdf file.
doc.SaveToFile("Sample.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Sample.pdf");
How to Insert an Image into PDF Document via Spire.PDF
Step 1, 2, 3 are the same as above.
Step 4 Insert Image

//Draw the image
PdfImage image = PdfImage.FromFile(@"..\..\Sample.jpg");
float width = image.Width * 0.75f;
float height = image.Height * 0.75f;
float x = (page.Canvas.ClientSize.Width - width) / 2;
Step 5 Save and Preview

static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Create one page
PdfPageBase page = doc.Pages.Add();
RotateText(page);
//Save doc file.
doc.SaveToFile("DrawText.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("DrawText.pdf");
}
Draw Transform Text in PDF
private static void TransformText(PdfPageBase page)
{
//save graphics state
PdfGraphicsState state = page.Canvas.Save();
//Draw the text - transform
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f);
PdfSolidBrush brush1 = new PdfSolidBrush(Color.DeepSkyBlue);
PdfSolidBrush brush2 = new PdfSolidBrush(Color.CadetBlue);
page.Canvas.TranslateTransform(20, 200);
page.Canvas.ScaleTransform(1f, 0.6f);
page.Canvas.SkewTransform(-10, 0);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush1, 0, 0);
page.Canvas.SkewTransform(10, 0);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, 0);
page.Canvas.ScaleTransform(1f, -1f);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18);
//restor graphics
page.Canvas.Restore(state);
}

Draw Alignment Text in PDF
private static void AlignText(PdfPageBase page)
{
//Draw the text - alignment
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
PdfStringFormat leftAlignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Left!", font, brush, 0, 20, leftAlignment);
page.Canvas.DrawString("Left!", font, brush, 0, 50, leftAlignment);
PdfStringFormat rightAlignment = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 30, rightAlignment);
page.Canvas.DrawString("Right!", font, brush, page.Canvas.ClientSize.Width, 60, rightAlignment);
PdfStringFormat centerAlignment
= new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!",
font, brush, page.Canvas.ClientSize.Width / 2, 40, centerAlignment);
}

Draw alignment Text in a Rectangle
private static void AlignTextInRectangle(PdfPageBase page)
{
//Draw the text - align in rectangle
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
RectangleF rctg1 = new RectangleF(0, 70, page.Canvas.ClientSize.Width / 2, 100);
RectangleF rctg2
= new RectangleF(page.Canvas.ClientSize.Width / 2, 70, page.Canvas.ClientSize.Width / 2, 100);
page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightBlue), rctg1);
page.Canvas.DrawRectangle(new PdfSolidBrush(Color.LightSkyBlue), rctg2);
PdfStringFormat leftAlignment
= new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);
page.Canvas.DrawString("Left! Left!", font, brush, rctg1, leftAlignment);
page.Canvas.DrawString("Left! Left!", font, brush, rctg2, leftAlignment);
PdfStringFormat rightAlignment
= new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
page.Canvas.DrawString("Right! Right!", font, brush, rctg1, rightAlignment);
page.Canvas.DrawString("Right! Right!", font, brush, rctg2, rightAlignment);
PdfStringFormat centerAlignment
= new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Bottom);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg1, centerAlignment);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, rctg2, centerAlignment);
}

Draw Rotate Text in PDF
private static void RotateText(PdfPageBase page)
{
//save graphics state
PdfGraphicsState state = page.Canvas.Save();
//Draw the text - transform
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
PdfStringFormat centerAlignment
= new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
float x = page.Canvas.ClientSize.Width / 2;
float y = 380;
page.Canvas.TranslateTransform(x, y);
for (int i = 0; i < 12; i++)
{
page.Canvas.RotateTransform(30);
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, 20, 0, centerAlignment);
}
//restor graphics
page.Canvas.Restore(state);
}

