Spire.PDF (68)
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Create one page
PdfPageBase page =doc.Pages.Add();
//Draw the text
page.Canvas.DrawString("Hello, World!",
new PdfFont(PdfFontFamily.Helvetica, 30f),
new PdfSolidBrush(Color.Black),
10, 10);
//Save pdf file.
doc.SaveToFile("HelloWorld.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("HelloWorld.pdf");
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace HelloWorld
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
' Create one page
Dim page As PdfPageBase = doc.Pages.Add()
'Draw the text
page.Canvas.DrawString("Hello, World!", _
New PdfFont(PdfFontFamily.Helvetica, 30.0F), _
New PdfSolidBrush(Color.Black), 10, 10)
'Save pdf file.
doc.SaveToFile("HelloWorld.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("HelloWorld.pdf")
End Sub
End Class
End Namespace
Published in
Quick guide
Tagged under
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace DrawText
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Create one page
PdfPageBase page = doc.Pages.Add();
DrawText(page);
AlignText(page);
AlignTextInRectangle(page);
TransformText(page);
RotateText(page);
//Save doc file.
doc.SaveToFile("DrawText.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("DrawText.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);
}
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);
}
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);
}
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);
}
private static void DrawText(PdfPageBase page)
{
//save graphics state
PdfGraphicsState state = page.Canvas.Save();
//Draw text - brush
String text = "Go! Turn Around! Go! Go! Go!";
PdfPen pen = PdfPens.DeepSkyBlue;
PdfSolidBrush brush = new PdfSolidBrush(Color.White);
PdfStringFormat format = new PdfStringFormat();
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 18f, PdfFontStyle.Italic);
SizeF size = font.MeasureString(text, format);
RectangleF rctg
= new RectangleF(page.Canvas.ClientSize.Width / 2 + 10, 180,
size.Width / 3 * 2, size.Height * 2);
page.Canvas.DrawString(text, font, pen, brush, rctg, format);
//restor graphics
page.Canvas.Restore(state);
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace DrawText
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
' Create one page
Dim page As PdfPageBase = doc.Pages.Add()
DrawText(page)
AlignText(page)
AlignTextInRectangle(page)
TransformText(page)
RotateText(page)
'Save doc file.
doc.SaveToFile("DrawText.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("DrawText.pdf")
End Sub
Private Shared Sub RotateText(ByVal page As PdfPageBase)
'save graphics state
Dim state As PdfGraphicsState = page.Canvas.Save()
'Draw the text - transform
Dim font As New PdfFont(PdfFontFamily.Helvetica, 10.0F)
Dim brush As New PdfSolidBrush(Color.Blue)
Dim centerAlignment As New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle)
Dim x As Single = page.Canvas.ClientSize.Width \ 2
Dim y As Single = 380
page.Canvas.TranslateTransform(x, y)
For i As Integer = 0 To 11
page.Canvas.RotateTransform(30)
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush, 20, 0, centerAlignment)
Next i
'restor graphics
page.Canvas.Restore(state)
End Sub
Private Shared Sub TransformText(ByVal page As PdfPageBase)
'save graphics state
Dim state As PdfGraphicsState = page.Canvas.Save()
'Draw the text - transform
Dim font As New PdfFont(PdfFontFamily.Helvetica, 18.0F)
Dim brush1 As New PdfSolidBrush(Color.DeepSkyBlue)
Dim brush2 As New PdfSolidBrush(Color.CadetBlue)
page.Canvas.TranslateTransform(20, 200)
page.Canvas.ScaleTransform(1.0F, 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(1.0F, -1.0F)
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", font, brush2, 0, -2 * 18)
'restor graphics
page.Canvas.Restore(state)
End Sub
Private Shared Sub AlignTextInRectangle(ByVal page As PdfPageBase)
'Draw the text - align in rectangle
Dim font As New PdfFont(PdfFontFamily.Helvetica, 10.0F)
Dim brush As New PdfSolidBrush(Color.Blue)
Dim rctg1 As New RectangleF(0, 70, page.Canvas.ClientSize.Width \ 2, 100)
Dim rctg2 As 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)
Dim leftAlignment As New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top)
page.Canvas.DrawString("Left! Left!", font, brush, rctg1, leftAlignment)
page.Canvas.DrawString("Left! Left!", font, brush, rctg2, leftAlignment)
Dim rightAlignment As New PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle)
page.Canvas.DrawString("Right! Right!", font, brush, rctg1, rightAlignment)
page.Canvas.DrawString("Right! Right!", font, brush, rctg2, rightAlignment)
Dim centerAlignment As 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)
End Sub
Private Shared Sub AlignText(ByVal page As PdfPageBase)
'Draw the text - alignment
Dim font As New PdfFont(PdfFontFamily.Helvetica, 20.0F)
Dim brush As New PdfSolidBrush(Color.Blue)
Dim leftAlignment As New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle)
page.Canvas.DrawString("Left!", font, brush, 0, 20, leftAlignment)
page.Canvas.DrawString("Left!", font, brush, 0, 50, leftAlignment)
Dim rightAlignment As 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)
Dim centerAlignment As New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)
page.Canvas.DrawString("Go! Turn Around! Go! Go! Go!", _
font, brush, page.Canvas.ClientSize.Width \ 2, 40, centerAlignment)
End Sub
Private Shared Sub DrawText(ByVal page As PdfPageBase)
'save graphics state
Dim state As PdfGraphicsState = page.Canvas.Save()
'Draw text - brush
Dim text As String = "Go! Turn Around! Go! Go! Go!"
Dim pen As PdfPen = PdfPens.DeepSkyBlue
Dim brush As New PdfSolidBrush(Color.White)
Dim format As New PdfStringFormat()
Dim font As New PdfFont(PdfFontFamily.Helvetica, 18.0F, PdfFontStyle.Italic)
Dim size As SizeF = font.MeasureString(text, format)
Dim rctg As New RectangleF(page.Canvas.ClientSize.Width \ 2 + 10, 180, size.Width \ 3 * 2, size.Height * 2)
page.Canvas.DrawString(text, font, pen, brush, rctg, format)
'restor graphics
page.Canvas.Restore(state)
End Sub
End Class
End Namespace
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace Font
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
// Create one page
PdfPageBase page = doc.Pages.Add();
//Draw the text
float l = page.Canvas.ClientSize.Width / 2;
PointF center = new PointF(l, l);
float r = (float)Math.Sqrt(2 * l * l);
PdfRadialGradientBrush brush
= new PdfRadialGradientBrush(center, 0f, center, r, Color.Blue, Color.Red);
PdfFontFamily[] fontFamilies
= (PdfFontFamily[])Enum.GetValues(typeof(PdfFontFamily));
float y = 10;
for (int i = 0; i < fontFamilies.Length; i++)
{
String text = String.Format("Font Family: {0}", fontFamilies[i]);
float x1 = 0;
y = 10 + i * 16;
PdfFont font1 = new PdfFont(PdfFontFamily.Courier, 14f);
PdfFont font2 = new PdfFont(fontFamilies[i], 14f);
float x2 = x1 + 10 + font1.MeasureString(text).Width;
page.Canvas.DrawString(text, font1, brush, x1, y);
page.Canvas.DrawString(text, font2, brush, x2, y);
}
//true type font - embedded.
System.Drawing.Font font = new System.Drawing.Font("Arial", 14f, FontStyle.Bold);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
page.Canvas.DrawString("Font Family: Arial - Embedded", trueTypeFont, brush, 0, (y = y + 16f));
//right to left
String arabicText
= "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020"
+ "\u0628\u062F\u0648\u0631\u0647\u0020\u062D\u0648\u0644\u0647\u0627\u0021\u0020"
+ "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020"
+ "\u0627\u0644\u0630\u0647\u0627\u0628\u0021\u0020"
+ "\u0627\u0644\u0630\u0647\u0627\u0628\u0021";
trueTypeFont = new PdfTrueTypeFont(font, true);
RectangleF rctg = new RectangleF(new PointF(0, (y = y + 16f)), page.Canvas.ClientSize);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
format.RightToLeft = true;
page.Canvas.DrawString(arabicText, trueTypeFont, brush, rctg, format);
//true type font - not embedded
font = new System.Drawing.Font("Batang", 14f, FontStyle.Italic);
trueTypeFont = new PdfTrueTypeFont(font);
page.Canvas.DrawString("Font Family: Batang - Not Embedded", trueTypeFont, brush, 0, (y = y + 16f));
//font file
String fontFileName = "Hawaii_Killer.ttf";
trueTypeFont = new PdfTrueTypeFont(fontFileName, 20f);
page.Canvas.DrawString("Hawaii Killer Font", trueTypeFont, brush, 0, (y = y + 16f));
page.Canvas.DrawString("Hawaii Killer Font, from http://www.1001freefonts.com",
new PdfFont(PdfFontFamily.Helvetica, 8f), brush, 10, (y = y + 20f));
//cjk font
PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14f);
page.Canvas.DrawString("How to say 'Font' in Chinese? \u5B57\u4F53",
cjkFont, brush, 0, (y = y + 16f));
cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14f);
page.Canvas.DrawString("How to say 'Font' in Japanese? \u30D5\u30A9\u30F3\u30C8",
cjkFont, brush, 0, (y = y + 16f));
cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14f);
page.Canvas.DrawString("How to say 'Font' in Korean? \uAE00\uAF34",
cjkFont, brush, 0, (y = y + 16f));
//Save pdf file.
doc.SaveToFile("Font.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Font.pdf");
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace FontFormat
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
' Create one page
Dim page As PdfPageBase = doc.Pages.Add()
'Draw the text
Dim l As Single = page.Canvas.ClientSize.Width / 2
Dim center As New PointF(l, l)
Dim r As Single = CSng(Math.Sqrt(2 * l * l))
Dim brush As New PdfRadialGradientBrush(center, 0.0F, center, r, Color.Blue, Color.Red)
Dim fontFamilies() As PdfFontFamily _
= CType(System.Enum.GetValues(GetType(PdfFontFamily)), PdfFontFamily())
Dim y As Single = 10
For i As Integer = 0 To fontFamilies.Length - 1
Dim text As String = String.Format("Font Family: {0}", fontFamilies(i))
Dim x1 As Single = 0
y = 10 + i * 16
Dim font1 As New PdfFont(PdfFontFamily.Courier, 14.0F)
Dim font2 As New PdfFont(fontFamilies(i), 14.0F)
Dim x2 As Single = x1 + 10 + font1.MeasureString(text).Width
page.Canvas.DrawString(text, font1, brush, x1, y)
page.Canvas.DrawString(text, font2, brush, x2, y)
Next i
'true type font - embedded.
Dim font As New System.Drawing.Font("Arial", 14.0F, FontStyle.Bold)
Dim trueTypeFont As New PdfTrueTypeFont(font)
y = y + 16.0F
page.Canvas.DrawString("Font Family: Arial - Embedded", trueTypeFont, brush, 0, y)
'right to left
Dim arabicText As String _
= ChrW(&H627).ToString() & ChrW(&H644).ToString() & ChrW(&H630).ToString() _
& ChrW(&H647).ToString() & ChrW(&H627).ToString() & ChrW(&H628).ToString() _
& ChrW(&H21).ToString() & ChrW(&H20).ToString() & ChrW(&H628).ToString() _
& ChrW(&H62F).ToString() & ChrW(&H648).ToString() & ChrW(&H631).ToString() _
& ChrW(&H647).ToString() & ChrW(&H20).ToString() & ChrW(&H62D).ToString() _
& ChrW(&H648).ToString() & ChrW(&H644).ToString() & ChrW(&H647).ToString() _
& ChrW(&H627).ToString() & ChrW(&H21).ToString() & ChrW(&H20).ToString() _
& ChrW(&H627).ToString() & ChrW(&H644).ToString() & ChrW(&H630).ToString() _
& ChrW(&H647).ToString() & ChrW(&H627).ToString() & ChrW(&H628).ToString() _
& ChrW(&H21).ToString() & ChrW(&H20).ToString() & ChrW(&H627).ToString() _
& ChrW(&H644).ToString() & ChrW(&H630).ToString() & ChrW(&H647).ToString() _
& ChrW(&H627).ToString() & ChrW(&H628).ToString() & ChrW(&H21).ToString() _
& ChrW(&H20).ToString() & ChrW(&H627).ToString() & ChrW(&H644).ToString() _
& ChrW(&H630).ToString() & ChrW(&H647).ToString() & ChrW(&H627).ToString() _
& ChrW(&H628).ToString() & ChrW(&H21).ToString()
trueTypeFont = New PdfTrueTypeFont(font, True)
y = y + 16.0F
Dim rctg As New RectangleF(New PointF(0, y), page.Canvas.ClientSize)
Dim format As New PdfStringFormat(PdfTextAlignment.Right)
format.RightToLeft = True
page.Canvas.DrawString(arabicText, trueTypeFont, brush, rctg, format)
'true type font - not embedded
font = New System.Drawing.Font("Batang", 14.0F, FontStyle.Italic)
trueTypeFont = New PdfTrueTypeFont(font)
y = y + 16.0F
page.Canvas.DrawString("Font Family: Batang - Not Embedded", trueTypeFont, brush, 0, y)
'font file
Dim fontFileName As String = "Hawaii_Killer.ttf"
trueTypeFont = New PdfTrueTypeFont(fontFileName, 20.0F)
y = y + 16.0F
page.Canvas.DrawString("Hawaii Killer Font", trueTypeFont, brush, 0, y)
y = y + 20.0F
page.Canvas.DrawString("Hawaii Killer Font, from http://www.1001freefonts.com", _
New PdfFont(PdfFontFamily.Helvetica, 8.0F), brush, 10, y)
'cjk font
Dim cjkFont As New PdfCjkStandardFont(PdfCjkFontFamily.MonotypeHeiMedium, 14.0F)
y = y + 16.0F
page.Canvas.DrawString("How to say 'Font' in Chinese? " _
& ChrW(&H5B57).ToString() & ChrW(&H4F53).ToString(), cjkFont, brush, 0, y)
cjkFont = New PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsGothicMedium, 14.0F)
y = y + 16.0F
page.Canvas.DrawString("How to say 'Font' in Japanese? " _
& ChrW(&H30D5).ToString() & ChrW(&H30A9).ToString() _
& ChrW(&H30F3).ToString() & ChrW(&H30C8).ToString(), cjkFont, brush, 0, y)
cjkFont = New PdfCjkStandardFont(PdfCjkFontFamily.HanyangSystemsShinMyeongJoMedium, 14.0F)
y = y + 16.0F
page.Canvas.DrawString("How to say 'Font' in Korean? " _
& ChrW(&HAE00).ToString() & ChrW(&HAF34).ToString(), cjkFont, brush, 0, y)
'Save pdf file.
doc.SaveToFile("Font.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Font.pdf")
End Sub
End Class
End Namespace
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
namespace PageSetup
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
page.BackgroundColor = Color.Chocolate;
//Draw page
DrawPage(page);
page = doc.Pages.Add(PdfPageSize.A4, margin);
page.BackgroundColor = Color.Coral;
//Draw page
DrawPage(page);
page = doc.Pages.Add(PdfPageSize.A3, margin, PdfPageRotateAngle.RotateAngle180, PdfPageOrientation.Landscape);
page.BackgroundColor = Color.LightPink;
//Draw page
DrawPage(page);
//create section
PdfSection section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = margin;
page = section.Pages.Add();
//Draw page
DrawPage(page);
//set background color
page = section.Pages.Add();
page.BackgroundColor = Color.LightSkyBlue;
DrawPage(page);
//Landscape
section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = margin;
section.PageSettings.Orientation = PdfPageOrientation.Landscape;
page = section.Pages.Add();
DrawPage(page);
//Rotate 90
section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = margin;
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90;
page = section.Pages.Add();
DrawPage(page);
//Rotate 180
section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Margins = margin;
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;
page = section.Pages.Add();
DrawPage(page);
//Save pdf file.
doc.SaveToFile("PageSetup.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("PageSetup.pdf");
}
private static void DrawPage(PdfPageBase page)
{
float pageWidth = page.Canvas.ClientSize.Width;
float y = 0;
//title
y = y + 5;
PdfBrush brush2 = new PdfSolidBrush(Color.Black);
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center);
format2.CharacterSpacing = 1f;
String text = "Summary of Science";
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
SizeF size = font2.MeasureString(text, format2);
y = y + size.Height + 6;
//icon
PdfImage image = PdfImage.FromFile("Wikipedia_Science.png");
page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y));
float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2;
float imageBottom = image.PhysicalDimension.Height + y;
//refenrence content
PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f));
PdfStringFormat format3 = new PdfStringFormat();
format3.ParagraphIndent = font3.Size * 2;
format3.MeasureTrailingSpaces = true;
format3.LineSpacing = font3.Size * 1.5f;
String text1 = "(All text and picture from ";
String text2 = "Wikipedia";
String text3 = ", the free encyclopedia)";
page.Canvas.DrawString(text1, font3, brush2, 0, y, format3);
size = font3.MeasureString(text1, format3);
float x1 = size.Width;
format3.ParagraphIndent = 0;
PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline));
PdfBrush brush3 = PdfBrushes.Blue;
page.Canvas.DrawString(text2, font4, brush3, x1, y, format3);
size = font4.MeasureString(text2, format3);
x1 = x1 + size.Width;
page.Canvas.DrawString(text3, font3, brush2, x1, y, format3);
y = y + size.Height;
//content
PdfStringFormat format4 = new PdfStringFormat();
text = System.IO.File.ReadAllText("Summary_of_Science.txt");
PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f));
format4.LineSpacing = font5.Size * 1.5f;
PdfStringLayouter textLayouter = new PdfStringLayouter();
float imageLeftBlockHeight = imageBottom - y;
PdfStringLayoutResult result
= textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));
if (result.ActualSize.Height < imageBottom - y)
{
imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight;
result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));
}
foreach (LineInfo line in result.Lines)
{
page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4);
y = y + result.LineHeight;
}
PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2);
PdfTextLayout textLayout = new PdfTextLayout();
textLayout.Break = PdfLayoutBreakType.FitPage;
textLayout.Layout = PdfLayoutType.Paginate;
RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);
textWidget.StringFormat = format4;
textWidget.Draw(page, bounds, textLayout);
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.AutomaticFields
Imports Spire.Pdf.Graphics
Namespace PageSetup
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
'margin
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
' Create one page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
page.BackgroundColor = Color.Chocolate
'Draw page
DrawPage(page)
page = doc.Pages.Add(PdfPageSize.A4, margin)
page.BackgroundColor = Color.Coral
'Draw page
DrawPage(page)
page = doc.Pages.Add(PdfPageSize.A3, margin, PdfPageRotateAngle.RotateAngle180, _
PdfPageOrientation.Landscape)
page.BackgroundColor = Color.LightPink
'Draw page
DrawPage(page)
'create section
Dim section As PdfSection = doc.Sections.Add()
section.PageSettings.Size = PdfPageSize.A4
section.PageSettings.Margins = margin
page = section.Pages.Add()
'Draw page
DrawPage(page)
'set background color
page = section.Pages.Add()
page.BackgroundColor = Color.LightSkyBlue
DrawPage(page)
'Landscape
section = doc.Sections.Add()
section.PageSettings.Size = PdfPageSize.A4
section.PageSettings.Margins = margin
section.PageSettings.Orientation = PdfPageOrientation.Landscape
page = section.Pages.Add()
DrawPage(page)
'Rotate 90
section = doc.Sections.Add()
section.PageSettings.Size = PdfPageSize.A4
section.PageSettings.Margins = margin
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90
page = section.Pages.Add()
DrawPage(page)
'Rotate 180
section = doc.Sections.Add()
section.PageSettings.Size = PdfPageSize.A4
section.PageSettings.Margins = margin
section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180
page = section.Pages.Add()
DrawPage(page)
'Save pdf file.
doc.SaveToFile("PageSetup.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("PageSetup.pdf")
End Sub
Private Shared Sub DrawPage(ByVal page As PdfPageBase)
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim y As Single = 0
'title
y = y + 5
Dim brush2 As PdfBrush = New PdfSolidBrush(Color.Black)
Dim font2 As New PdfTrueTypeFont(New Font("Arial", 16.0F, FontStyle.Bold))
Dim format2 As New PdfStringFormat(PdfTextAlignment.Center)
format2.CharacterSpacing = 1.0F
Dim text As String = "Summary of Science"
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2)
Dim size As SizeF = font2.MeasureString(text, format2)
y = y + size.Height + 6
'icon
Dim image As PdfImage = PdfImage.FromFile("Wikipedia_Science.png")
page.Canvas.DrawImage(image, New PointF(pageWidth - image.PhysicalDimension.Width, y))
Dim imageLeftSpace As Single = pageWidth - image.PhysicalDimension.Width - 2
Dim imageBottom As Single = image.PhysicalDimension.Height + y
'refenrence content
Dim font3 As New PdfTrueTypeFont(New Font("Arial", 9.0F))
Dim format3 As New PdfStringFormat()
format3.ParagraphIndent = font3.Size * 2
format3.MeasureTrailingSpaces = True
format3.LineSpacing = font3.Size * 1.5F
Dim text1 As String = "(All text and picture from "
Dim text2 As String = "Wikipedia"
Dim text3 As String = ", the free encyclopedia)"
page.Canvas.DrawString(text1, font3, brush2, 0, y, format3)
size = font3.MeasureString(text1, format3)
Dim x1 As Single = size.Width
format3.ParagraphIndent = 0
Dim font4 As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Underline))
Dim brush3 As PdfBrush = PdfBrushes.Blue
page.Canvas.DrawString(text2, font4, brush3, x1, y, format3)
size = font4.MeasureString(text2, format3)
x1 = x1 + size.Width
page.Canvas.DrawString(text3, font3, brush2, x1, y, format3)
y = y + size.Height
'content
Dim format4 As New PdfStringFormat()
text = System.IO.File.ReadAllText("Summary_of_Science.txt")
Dim font5 As New PdfTrueTypeFont(New Font("Arial", 10.0F))
format4.LineSpacing = font5.Size * 1.5F
Dim textLayouter As New PdfStringLayouter()
Dim imageLeftBlockHeight As Single = imageBottom - y
Dim result As PdfStringLayoutResult _
= textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight))
If result.ActualSize.Height < imageBottom - y Then
imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight
result = textLayouter.Layout(text, font5, format4, New SizeF(imageLeftSpace, imageLeftBlockHeight))
End If
For Each line As LineInfo In result.Lines
page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4)
y = y + result.LineHeight
Next line
Dim textWidget As New PdfTextWidget(result.Remainder, font5, brush2)
Dim textLayout As New PdfTextLayout()
textLayout.Break = PdfLayoutBreakType.FitPage
textLayout.Layout = PdfLayoutType.Paginate
Dim bounds As New RectangleF(New PointF(0, y), page.Canvas.ClientSize)
textWidget.StringFormat = format4
textWidget.Draw(page, bounds, textLayout)
End Sub
End Class
End Namespace
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
namespace SimpleTable
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
float y = 10;
//title
PdfBrush brush1 = PdfBrushes.Black;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
y = y + font1.MeasureString("Country List", format1).Height;
y = y + 5;
String[] data
= {
"Name;Capital;Continent;Area;Population",
"Argentina;Buenos Aires;South America;2777815;32300003",
"Bolivia;La Paz;South America;1098575;7300000",
"Brazil;Brasilia;South America;8511196;150400000",
"Canada;Ottawa;North America;9976147;26500000",
"Chile;Santiago;South America;756943;13200000",
"Colombia;Bagota;South America;1138907;33000000",
"Cuba;Havana;North America;114524;10600000",
"Ecuador;Quito;South America;455502;10600000",
"El Salvador;San Salvador;North America;20865;5300000",
"Guyana;Georgetown;South America;214969;800000",
"Jamaica;Kingston;North America;11424;2500000",
"Mexico;Mexico City;North America;1967180;88600000",
"Nicaragua;Managua;North America;139000;3900000",
"Paraguay;Asuncion;South America;406576;4660000",
"Peru;Lima;South America;1285215;21600000",
"United States of America;Washington;North America;9363130;249200000",
"Uruguay;Montevideo;South America;176140;3002000",
"Venezuela;Caracas;South America;912047;19700000"
};
String[][] dataSource
= new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
dataSource[i] = data[i].Split(';');
}
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.DataSource = dataSource;
PdfLayoutResult result = table.Draw(page, new PointF(0, y));
y = y + result.Bounds.Height + 5;
PdfBrush brush2 = PdfBrushes.Gray;
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
page.Canvas.DrawString(String.Format("* {0} countries in the list.", data.Length - 1),
font2, brush2, 5, y);
//Save pdf file.
doc.SaveToFile("SimpleTable.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("SimpleTable.pdf");
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Tables
Namespace SimpleTable
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
'margin
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
' Create one page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)
Dim y As Single = 10
'title
Dim brush1 As PdfBrush = PdfBrushes.Black
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 16.0F, FontStyle.Bold))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Center)
page.Canvas.DrawString("Country List", font1, brush1, page.Canvas.ClientSize.Width \ 2, y, format1)
y = y + font1.MeasureString("Country List", format1).Height
y = y + 5
Dim data() As String _
= {"Name;Capital;Continent;Area;Population", _
"Argentina;Buenos Aires;South America;2777815;32300003", _
"Bolivia;La Paz;South America;1098575;7300000", _
"Brazil;Brasilia;South America;8511196;150400000", _
"Canada;Ottawa;North America;9976147;26500000", _
"Chile;Santiago;South America;756943;13200000", _
"Colombia;Bagota;South America;1138907;33000000", _
"Cuba;Havana;North America;114524;10600000", _
"Ecuador;Quito;South America;455502;10600000", _
"El Salvador;San Salvador;North America;20865;5300000", _
"Guyana;Georgetown;South America;214969;800000", _
"Jamaica;Kingston;North America;11424;2500000", _
"Mexico;Mexico City;North America;1967180;88600000", _
"Nicaragua;Managua;North America;139000;3900000", _
"Paraguay;Asuncion;South America;406576;4660000", _
"Peru;Lima;South America;1285215;21600000", _
"United States of America;Washington;North America;9363130;249200000", _
"Uruguay;Montevideo;South America;176140;3002000", _
"Venezuela;Caracas;South America;912047;19700000"}
Dim dataSource(data.Length - 1)() As String
For i As Integer = 0 To data.Length - 1
dataSource(i) = data(i).Split(";"c)
Next i
Dim table As New PdfTable()
table.Style.CellPadding = 2
table.Style.HeaderSource = PdfHeaderSource.Rows
table.Style.HeaderRowCount = 1
table.Style.ShowHeader = True
table.DataSource = dataSource
Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y))
y = y + result.Bounds.Height + 5
Dim brush2 As PdfBrush = PdfBrushes.Gray
Dim font2 As New PdfTrueTypeFont(New Font("Arial", 9.0F))
page.Canvas.DrawString(String.Format("* {0} countries in the list.", data.Length - 1), _
font2, brush2, 5, y)
'Save pdf file.
doc.SaveToFile("SimpleTable.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("SimpleTable.pdf")
End Sub
End Class
End Namespace





