Wednesday, 06 April 2011 09:47
PDF Extract in C#, VB.NET
The sample demonstrates how to extract images and text from PDF document.
(NO screenshot)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Spire.Pdf;
namespace Extraction
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"Sample2.pdf");
StringBuilder buffer = new StringBuilder();
IList<Image> images = new List<Image>();
foreach (PdfPageBase page in doc.Pages)
{
buffer.Append(page.ExtractText());
foreach (Image image in page.ExtractImages())
{
images.Add(image);
}
}
doc.Close();
//save text
String fileName = "TextInPdf.txt";
File.WriteAllText(fileName, buffer.ToString());
//save image
int index = 0;
foreach (Image image in images)
{
String imageFileName
= String.Format("Image-{0}.png", index++);
image.Save(imageFileName, ImageFormat.Png);
}
//Launching the Text file.
System.Diagnostics.Process.Start(fileName);
}
}
}
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports Spire.Pdf
Namespace Extraction
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
doc.LoadFromFile("Sample2.pdf")
Dim buffer As New StringBuilder()
Dim images As IList(Of Image) = New List(Of Image)()
For Each page As PdfPageBase In doc.Pages
buffer.Append(page.ExtractText())
For Each image As Image In page.ExtractImages()
images.Add(image)
Next image
Next page
doc.Close()
'save text
Dim fileName As String = "TextInPdf.txt"
File.WriteAllText(fileName, buffer.ToString())
'save image
Dim index As Integer = 0
For Each image As Image In images
Dim imageFileName As String = String.Format("Image-{0}.png", index)
index += 1
image.Save(imageFileName, ImageFormat.Png)
Next image
'Launching the Text file.
Process.Start(fileName)
End Sub
End Class
End Namespace
Wednesday, 06 April 2011 09:44
PDF Booklet in C#, VB.NET
using System;
using Spire.Pdf;
namespace Booklet
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
String srcPdf = @"Sample2.pdf";
float width = PdfPageSize.A4.Width * 2;
float height = PdfPageSize.A4.Height;
doc.CreateBooklet(srcPdf, width, height, true);
//Save pdf file.
doc.SaveToFile("Booklet.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Booklet.pdf");
}
}
}
Imports Spire.Pdf
Namespace Booklet
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
Dim srcPdf As String = "Sample2.pdf"
Dim width As Single = PdfPageSize.A4.Width * 2
Dim height As Single = PdfPageSize.A4.Height
doc.CreateBooklet(srcPdf, width, height, True)
'Save pdf file.
doc.SaveToFile("Booklet.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Booklet.pdf")
End Sub
End Class
End Namespace
Published in
Document Operation
Wednesday, 06 April 2011 09:41
PDF ViewPreference in C#, VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace ViewerPreference
{
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 page
DrawPage(page);
//set view reference
doc.ViewerPreferences.CenterWindow = true;
doc.ViewerPreferences.DisplayTitle = false;
doc.ViewerPreferences.FitWindow = false;
doc.ViewerPreferences.HideMenubar = true;
doc.ViewerPreferences.HideToolbar = true;
doc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage;
//Save pdf file.
doc.SaveToFile("ViewerPreference.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("ViewerPreference.pdf");
}
private static void DrawPage(PdfPageBase page)
{
float pageWidth = page.Canvas.ClientSize.Width;
float y = 0;
//page header
PdfPen pen1 = new PdfPen(Color.LightGray, 1f);
PdfBrush brush1 = new PdfSolidBrush(Color.LightGray);
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right);
String text = "Demo of Spire.Pdf";
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1);
SizeF size = font1.MeasureString(text, format1);
y = y + size.Height + 1;
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);
//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;
text = "Summary of Science";
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
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.Graphics
Namespace ViewerPreference
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 page
DrawPage(page)
'set view reference
doc.ViewerPreferences.CenterWindow = True
doc.ViewerPreferences.DisplayTitle = False
doc.ViewerPreferences.FitWindow = False
doc.ViewerPreferences.HideMenubar = True
doc.ViewerPreferences.HideToolbar = True
doc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage
'Save pdf file.
doc.SaveToFile("ViewerPreference.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("ViewerPreference.pdf")
End Sub
Private Shared Sub DrawPage(ByVal page As PdfPageBase)
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim y As Single = 0
'page header
Dim pen1 As New PdfPen(Color.LightGray, 1.0F)
Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray)
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Right)
Dim text As String = "Demo of Spire.Pdf"
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1)
Dim size As SizeF = font1.MeasureString(text, format1)
y = y + size.Height + 1
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y)
'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
text = "Summary of Science"
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2)
size = 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
Published in
Document Settings
Tagged under
Wednesday, 06 April 2011 09:37
PDF Properties in C#, VB.NET
The sample demonstrates how to set PDF document properties and PDF file information.

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace Properties
{
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 page
DrawPage(page);
//set document info
doc.DocumentInformation.Author = "Harry Hu";
doc.DocumentInformation.Creator = "Harry Hu";
doc.DocumentInformation.Keywords = "pdf, demo, document information";
doc.DocumentInformation.Producer = "Spire.Pdf";
doc.DocumentInformation.Subject = "Demo of Spire.Pdf";
doc.DocumentInformation.Title = "Document Information";
//file info
doc.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream;
doc.FileInfo.IncrementalUpdate = false;
doc.FileInfo.Version = PdfVersion.Version1_5;
//Save pdf file.
doc.SaveToFile("Properties.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Properties.pdf");
}
private static void DrawPage(PdfPageBase page)
{
float pageWidth = page.Canvas.ClientSize.Width;
float y = 0;
//page header
PdfPen pen1 = new PdfPen(Color.LightGray, 1f);
PdfBrush brush1 = new PdfSolidBrush(Color.LightGray);
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right);
String text = "Demo of Spire.Pdf";
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1);
SizeF size = font1.MeasureString(text, format1);
y = y + size.Height + 1;
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);
//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;
text = "Summary of Science";
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
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
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace Properties
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 page
DrawPage(page)
'set document info
doc.DocumentInformation.Author = "Harry Hu"
doc.DocumentInformation.Creator = "Harry Hu"
doc.DocumentInformation.Keywords = "pdf, demo, document information"
doc.DocumentInformation.Producer = "Spire.Pdf"
doc.DocumentInformation.Subject = "Demo of Spire.Pdf"
doc.DocumentInformation.Title = "Document Information"
'file info
doc.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream
doc.FileInfo.IncrementalUpdate = False
doc.FileInfo.Version = PdfVersion.Version1_5
'Save pdf file.
doc.SaveToFile("Properties.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Properties.pdf")
End Sub
Private Shared Sub DrawPage(ByVal page As PdfPageBase)
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim y As Single = 0
'page header
Dim pen1 As New PdfPen(Color.LightGray, 1.0F)
Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray)
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Right)
Dim text As String = "Demo of Spire.Pdf"
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1)
Dim size As SizeF = font1.MeasureString(text, format1)
y = y + size.Height + 1
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y)
'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
text = "Summary of Science"
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2)
size = 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
Published in
Document Settings
Tagged under
Wednesday, 06 April 2011 09:33
PDF FromHTML in C#, VB.NET
using System;
using Spire.Pdf;
namespace FromHTML
{
class Program
{
[STAThread]
static void Main(string[] args)
{
//Create a pdf document.
PdfDocument doc = new PdfDocument();
String url = "http://www.e-iceblue.com/";
doc.LoadFromHTML(url, false, true, true);
//Save pdf file.
doc.SaveToFile("FromHTML.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("FromHTML.pdf");
}
}
}
Imports Spire.Pdf
Namespace FromHTML
Friend Class Program
_
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfDocument()
Dim url As String = "http://www.e-iceblue.com/"
doc.LoadFromHTML(url, False, True, True)
'Save pdf file.
doc.SaveToFile("FromHTML.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("FromHTML.pdf")
End Sub
End Class
End Namespace
Wednesday, 06 April 2011 09:30
PDF DigitalCertificate in C#, VB.NET
The sample demonstrates how to sign PDF document with digital certificate.

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
namespace DigitalSignature
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
PdfNewDocument doc = new PdfNewDocument();
// Create one page
PdfPageBase page = doc.Pages.Add();
//Draw the page
DrawPage(page);
String pfxPath = @"Demo.pfx";
PdfCertificate cert = new PdfCertificate(pfxPath, "e-iceblue");
PdfSignature signature = new PdfSignature(doc, page, cert, "demo");
signature.ContactInfo = "Harry Hu";
signature.Certificated = true;
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill;
//Save pdf file.
doc.Save("DigitalSignature.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("DigitalSignature.pdf");
}
private static void DrawPage(PdfPageBase page)
{
float pageWidth = page.Canvas.ClientSize.Width;
float y = 0;
//page header
PdfPen pen1 = new PdfPen(Color.LightGray, 1f);
PdfBrush brush1 = new PdfSolidBrush(Color.LightGray);
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right);
String text = "Demo of Spire.Pdf";
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1);
SizeF size = font1.MeasureString(text, format1);
y = y + size.Height + 1;
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);
//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;
text = "Summary of Science";
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
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.Graphics
Imports Spire.Pdf.Security
Namespace DigitalSignature
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim doc As New PdfNewDocument()
' Create one page
Dim page As PdfPageBase = doc.Pages.Add()
'Draw the page
DrawPage(page)
Dim pfxPath As String = "Demo.pfx"
Dim cert As New PdfCertificate(pfxPath, "e-iceblue")
Dim signature As New PdfSignature(doc, page, cert, "demo")
signature.ContactInfo = "Harry Hu"
signature.Certificated = True
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill
'Save pdf file.
doc.Save("DigitalSignature.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("DigitalSignature.pdf")
End Sub
Private Shared Sub DrawPage(ByVal page As PdfPageBase)
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim y As Single = 0
'page header
Dim pen1 As New PdfPen(Color.LightGray, 1.0F)
Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray)
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Right)
Dim text As String = "Demo of Spire.Pdf"
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1)
Dim size As SizeF = font1.MeasureString(text, format1)
y = y + size.Height + 1
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y)
'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
text = "Summary of Science"
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2)
size = 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
Published in
Security
Tagged under
Wednesday, 06 April 2011 09:27
PDF Decrypt in C#, VB.NET
The sample demonstrates how to decrypt an encrypted PDF document and extract the embedded picture.

using System;
using System.Drawing;
using Spire.Pdf;
namespace Decryption
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document.
String encryptedPdf = @"Encrypted.pdf";
PdfDocument doc = new PdfDocument(encryptedPdf, "test");
//extract image
Image image = doc.Pages[0].ImagesInfo[0].Image;
doc.Close();
//Save image file.
image.Save("Wikipedia_Science.png", System.Drawing.Imaging.ImageFormat.Png);
//Launching the image file.
System.Diagnostics.Process.Start("Wikipedia_Science.png");
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Namespace Decryption
Friend Class Program
Shared Sub Main(ByVal args() As String)
'Create a pdf document.
Dim encryptedPdf As String = "Encrypted.pdf"
Dim doc As New PdfDocument(encryptedPdf, "test")
'extract image
Dim image As Image = doc.Pages(0).ImagesInfo(0).Image
doc.Close()
'Save image file.
image.Save("Wikipedia_Science.png", System.Drawing.Imaging.ImageFormat.Png)
'Launching the image file.
Process.Start("Wikipedia_Science.png")
End Sub
End Class
End Namespace
Wednesday, 06 April 2011 09:23
PDF Encryption in C#, VB.NET
The sample demonstrates how to encrypt an PDF document and set owner/user password.

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Security;
namespace Encryption
{
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 page
DrawPage(page);
//encrypt
doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
doc.Security.OwnerPassword = "e-iceblue";
doc.Security.UserPassword = "test";
doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;
//Save pdf file.
doc.SaveToFile("Encryption.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Encryption.pdf");
}
private static void DrawPage(PdfPageBase page)
{
float pageWidth = page.Canvas.ClientSize.Width;
float y = 0;
//page header
PdfPen pen1 = new PdfPen(Color.LightGray, 1f);
PdfBrush brush1 = new PdfSolidBrush(Color.LightGray);
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right);
String text = "Demo of Spire.Pdf";
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1);
SizeF size = font1.MeasureString(text, format1);
y = y + size.Height + 1;
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);
//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;
text = "Summary of Science";
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);
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.Graphics
Imports Spire.Pdf.Security
Namespace Encryption
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 page
DrawPage(page)
'encrypt
doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit
doc.Security.OwnerPassword = "e-iceblue"
doc.Security.UserPassword = "test"
doc.Security.Permissions = PdfPermissionsFlags.Print Or PdfPermissionsFlags.FillFields
'Save pdf file.
doc.SaveToFile("Encryption.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Encryption.pdf")
End Sub
Private Shared Sub DrawPage(ByVal page As PdfPageBase)
Dim pageWidth As Single = page.Canvas.ClientSize.Width
Dim y As Single = 0
'page header
Dim pen1 As New PdfPen(Color.LightGray, 1.0F)
Dim brush1 As PdfBrush = New PdfSolidBrush(Color.LightGray)
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic))
Dim format1 As New PdfStringFormat(PdfTextAlignment.Right)
Dim text As String = "Demo of Spire.Pdf"
page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1)
Dim size As SizeF = font1.MeasureString(text, format1)
y = y + size.Height + 1
page.Canvas.DrawLine(pen1, 0, y, pageWidth, y)
'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
text = "Summary of Science"
page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2)
size = 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
Published in
Security
Tagged under
Wednesday, 06 April 2011 09:19
PDF FormField in C#, VB.NET
The sample demonstrates how to work with form, text box, check box, radio button, list box, combo box, button and form action in PDF document.

using System;
using System.Drawing;
using System.IO;
using System.Xml.XPath;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
namespace FormField
{
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;
SetDocumentTemplate(doc, PdfPageSize.A4, margin);
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
float y = 0;
//title
y = DrawPageTitle(page, y);
//load form config data
using (Stream stream = File.OpenRead(@"Form.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNodeIterator sectionNodes = xpathDoc.CreateNavigator().Select("/form/section");
int fieldIndex = 0;
foreach (XPathNavigator sectionNode in sectionNodes)
{
//draw section label
String sectionLabel = sectionNode.GetAttribute("name", "");
y = DrawFormSection(sectionLabel, page, y);
XPathNodeIterator fieldNodes = sectionNode.Select("field");
foreach (XPathNavigator fieldNode in fieldNodes)
{
y = DrawFormField(fieldNode, doc.Form, page, y, fieldIndex++);
}
}
}
//draw button
y = y + 10;
float buttonWidth = 80;
float buttonX = (page.Canvas.ClientSize.Width - buttonWidth) / 2;
RectangleF buttonBounds = new RectangleF(buttonX, y, buttonWidth, 16f);
PdfButtonField button = new PdfButtonField(page, "submit");
button.Text = "Submit";
button.Bounds = buttonBounds;
PdfSubmitAction submitAction = new PdfSubmitAction("http://www.e-iceblue.com");
button.Actions.MouseUp = submitAction;
doc.Form.Fields.Add(button);
//Save pdf file.
doc.SaveToFile("FormField.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("FormField.pdf");
}
private static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
{
PdfPageTemplateElement leftSpace
= new PdfPageTemplateElement(margin.Left, pageSize.Height);
doc.Template.Left = leftSpace;
PdfPageTemplateElement topSpace
= new PdfPageTemplateElement(pageSize.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
//draw header label
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
String label = "Demo of Spire.Pdf";
SizeF size = font.MeasureString(label, format);
float y = topSpace.Height - font.Height - 1;
PdfPen pen = new PdfPen(Color.Black, 0.75f);
topSpace.Graphics.SetTransparency(0.5f);
topSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
y = y - 1 - size.Height;
topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format);
PdfPageTemplateElement rightSpace
= new PdfPageTemplateElement(margin.Right, pageSize.Height);
doc.Template.Right = rightSpace;
PdfPageTemplateElement bottomSpace
= new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
bottomSpace.Foreground = true;
doc.Template.Bottom = bottomSpace;
//draw footer label
y = font.Height + 1;
bottomSpace.Graphics.SetTransparency(0.5f);
bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
y = y + 1;
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.AutomaticFields
= new PdfAutomaticField[] { pageNumber, pageCount };
pageNumberLabel.Brush = PdfBrushes.Black;
pageNumberLabel.Font = font;
pageNumberLabel.StringFormat = format;
pageNumberLabel.Text = "page {0} of {1}";
pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y);
PdfImage headerImage
= PdfImage.FromFile(@"Header.png");
PointF pageLeftTop = new PointF(-margin.Left, -margin.Top);
PdfPageTemplateElement header = new PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension);
header.Foreground = false;
header.Graphics.SetTransparency(0.5f);
header.Graphics.DrawImage(headerImage, 0, 0);
doc.Template.Stamps.Add(header);
PdfImage footerImage
= PdfImage.FromFile(@"Footer.png");
y = pageSize.Height - footerImage.PhysicalDimension.Height;
PointF footerLocation = new PointF(-margin.Left, y);
PdfPageTemplateElement footer = new PdfPageTemplateElement(footerLocation, footerImage.PhysicalDimension);
footer.Foreground = false;
footer.Graphics.SetTransparency(0.5f);
footer.Graphics.DrawImage(footerImage, 0, 0);
doc.Template.Stamps.Add(footer);
}
private static float DrawPageTitle(PdfPageBase page, float y)
{
PdfBrush brush1 = PdfBrushes.MidnightBlue;
PdfBrush brush2 = PdfBrushes.Red;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold));
String title = "Your Account Information(* = Required)";
SizeF size = font1.MeasureString(title);
float x = (page.Canvas.ClientSize.Width - size.Width) / 2;
page.Canvas.DrawString("Your Account Information(", font1, brush1, x, y);
size = font1.MeasureString("Your Account Information(");
x = x + size.Width;
page.Canvas.DrawString("* = Required", font1, brush2, x, y);
size = font1.MeasureString("* = Required");
x = x + size.Width;
page.Canvas.DrawString(")", font1, brush1, x, y);
y = y + size.Height;
y = y + 3;
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));
String p = "Your information is not public, shared in anyway, or displayed on this site.";
page.Canvas.DrawString(p, font2, brush1, 0, y);
return y + font2.Height;
}
private static float DrawFormSection(String label, PdfPageBase page, float y)
{
PdfBrush brush1 = PdfBrushes.LightYellow;
PdfBrush brush2 = PdfBrushes.DarkSlateGray;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Bold));
PdfStringFormat format = new PdfStringFormat();
float height = font.MeasureString(label).Height;
page.Canvas.DrawRectangle(brush2, 0, y, page.Canvas.ClientSize.Width, height + 2);
page.Canvas.DrawString(label, font, brush1, 2, y + 1);
y = y + height + 2;
PdfPen pen = new PdfPen(PdfBrushes.LightSkyBlue, 0.75f);
page.Canvas.DrawLine(pen, 0, y, page.Canvas.ClientSize.Width, y);
return y + 0.75f;
}
private static float DrawFormField(XPathNavigator fieldNode, PdfForm form, PdfPageBase page, float y, int fieldIndex)
{
float width = page.Canvas.ClientSize.Width;
float padding = 2;
//measure field label
String label = fieldNode.GetAttribute("label", "");
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 9f));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle);
float labelMaxWidth = width * 0.4f - 2 * padding;
SizeF labelSize = font1.MeasureString(label, labelMaxWidth, format);
//measure field height
float fieldHeight = MeasureFieldHeight(fieldNode);
float height = labelSize.Height > fieldHeight ? labelSize.Height : fieldHeight;
height = height + 2;
//draw background
PdfBrush brush = PdfBrushes.SteelBlue;
if (fieldIndex % 2 == 1)
{
brush = PdfBrushes.LightGreen;
}
page.Canvas.DrawRectangle(brush, 0, y, width, height);
//draw field label
PdfBrush brush1 = PdfBrushes.LightYellow;
RectangleF labelBounds = new RectangleF(padding, y, labelMaxWidth, height);
page.Canvas.DrawString(label, font1, brush1, labelBounds, format);
//daw field
float fieldMaxWidth = width * 0.57f - 2 * padding;
float fieldX = labelBounds.Right + 2 * padding;
float fieldY = y + (height - fieldHeight) / 2;
String fieldType = fieldNode.GetAttribute("type", "");
String fieldId = fieldNode.GetAttribute("id", "");
bool required = "true" == fieldNode.GetAttribute("required", "");
switch (fieldType)
{
case "text":
case "password":
PdfTextBoxField textField = new PdfTextBoxField(page, fieldId);
textField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
textField.BorderWidth = 0.75f;
textField.BorderStyle = PdfBorderStyle.Solid;
textField.Required = required;
if ("password" == fieldType)
{
textField.Password = true;
}
if ("true" == fieldNode.GetAttribute("multiple", ""))
{
textField.Multiline = true;
textField.Scrollable = true;
}
form.Fields.Add(textField);
break;
case "checkbox":
PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, fieldId);
float checkboxWidth = fieldHeight - 2 * padding;
float checkboxHeight = checkboxWidth;
checkboxField.Bounds = new RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight);
checkboxField.BorderWidth = 0.75f;
checkboxField.Style = PdfCheckBoxStyle.Cross;
checkboxField.Required = required;
form.Fields.Add(checkboxField);
break;
case "list":
XPathNodeIterator itemNodes = fieldNode.Select("item");
if ("true" == fieldNode.GetAttribute("multiple", ""))
{
PdfListBoxField listBoxField = new PdfListBoxField(page, fieldId);
listBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
listBoxField.BorderWidth = 0.75f;
listBoxField.MultiSelect = true;
listBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
listBoxField.Required = required;
//add items into list box.
foreach (XPathNavigator itemNode in itemNodes)
{
String text = itemNode.SelectSingleNode("text()").Value;
listBoxField.Items.Add(new PdfListFieldItem(text, text));
}
listBoxField.SelectedIndex = 0;
form.Fields.Add(listBoxField);
break;
}
if (itemNodes != null && itemNodes.Count <= 3)
{
PdfRadioButtonListField radioButtonListFile
= new PdfRadioButtonListField(page, fieldId);
radioButtonListFile.Required = required;
//add items into radio button list.
float fieldItemHeight = fieldHeight / itemNodes.Count;
float radioButtonWidth = fieldItemHeight - 2 * padding;
float radioButtonHeight = radioButtonWidth;
foreach (XPathNavigator itemNode in itemNodes)
{
String text = itemNode.SelectSingleNode("text()").Value;
PdfRadioButtonListItem fieldItem = new PdfRadioButtonListItem(text);
fieldItem.BorderWidth = 0.75f;
fieldItem.Bounds = new RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight);
radioButtonListFile.Items.Add(fieldItem);
float fieldItemLabelX = fieldX + radioButtonWidth + padding;
SizeF fieldItemLabelSize = font1.MeasureString(text);
float fieldItemLabelY = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2;
page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY);
fieldY = fieldY + fieldItemHeight;
}
form.Fields.Add(radioButtonListFile);
break;
}
//combo box
PdfComboBoxField comboBoxField = new PdfComboBoxField(page, fieldId);
comboBoxField.Bounds = new RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight);
comboBoxField.BorderWidth = 0.75f;
comboBoxField.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
comboBoxField.Required = required;
//add items into combo box.
foreach (XPathNavigator itemNode in itemNodes)
{
String text = itemNode.SelectSingleNode("text()").Value;
comboBoxField.Items.Add(new PdfListFieldItem(text, text));
}
form.Fields.Add(comboBoxField);
break;
}
if (required)
{
//draw *
float flagX = width * 0.97f + padding;
PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
SizeF size = font3.MeasureString("*");
float flagY = y + (height - size.Height) / 2;
page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY);
}
return y + height;
}
private static float MeasureFieldHeight(XPathNavigator fieldNode)
{
String fieldType = fieldNode.GetAttribute("type", "");
float defaultHeight = 16f;
switch (fieldType)
{
case "text":
case "password":
if ("true" == fieldNode.GetAttribute("multiple", ""))
{
return defaultHeight * 3;
}
return defaultHeight;
case "checkbox":
return defaultHeight;
case "list":
if ("true" == fieldNode.GetAttribute("multiple", ""))
{
return defaultHeight * 3;
}
XPathNodeIterator itemNodes = fieldNode.Select("item");
if (itemNodes != null && itemNodes.Count <= 3)
{
return defaultHeight * 3;
}
return defaultHeight;
}
String message = String.Format("Invalid field type: {0}", fieldType);
throw new ArgumentException(message);
}
}
}
Imports System.Drawing
Imports System.IO
Imports System.Xml.XPath
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.AutomaticFields
Imports Spire.Pdf.Fields
Imports Spire.Pdf.Graphics
Namespace FormField
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
SetDocumentTemplate(doc, PdfPageSize.A4, margin)
' Create one page
Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, New PdfMargins(0))
Dim y As Single = 0
'title
y = DrawPageTitle(page, y)
'load form config data
Using stream As Stream = File.OpenRead("Form.xml")
Dim xpathDoc As New XPathDocument(stream)
Dim sectionNodes As XPathNodeIterator = xpathDoc.CreateNavigator().Select("/form/section")
Dim fieldIndex As Integer = 0
For Each sectionNode As XPathNavigator In sectionNodes
'draw section label
Dim sectionLabel As String = sectionNode.GetAttribute("name", "")
y = DrawFormSection(sectionLabel, page, y)
Dim fieldNodes As XPathNodeIterator = sectionNode.Select("field")
For Each fieldNode As XPathNavigator In fieldNodes
y = DrawFormField(fieldNode, doc.Form, page, y, fieldIndex)
fieldIndex += 1
Next fieldNode
Next sectionNode
End Using
'draw button
y = y + 10
Dim buttonWidth As Single = 80
Dim buttonX As Single = (page.Canvas.ClientSize.Width - buttonWidth) / 2
Dim buttonBounds As New RectangleF(buttonX, y, buttonWidth, 16.0F)
Dim button As New PdfButtonField(page, "submit")
button.Text = "Submit"
button.Bounds = buttonBounds
Dim submitAction As New PdfSubmitAction("http://www.e-iceblue.com")
button.Actions.MouseUp = submitAction
doc.Form.Fields.Add(button)
'Save pdf file.
doc.SaveToFile("FormField.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("FormField.pdf")
End Sub
Private Shared Sub SetDocumentTemplate(ByVal doc As PdfDocument, ByVal pageSize As SizeF, _
ByVal margin As PdfMargins)
Dim leftSpace As New PdfPageTemplateElement(margin.Left, pageSize.Height)
doc.Template.Left = leftSpace
Dim topSpace As New PdfPageTemplateElement(pageSize.Width, margin.Top)
topSpace.Foreground = True
doc.Template.Top = topSpace
'draw header label
Dim font As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Italic))
Dim format As New PdfStringFormat(PdfTextAlignment.Right)
Dim label As String = "Demo of Spire.Pdf"
Dim size As SizeF = font.MeasureString(label, format)
Dim y As Single = topSpace.Height - font.Height - 1
Dim pen As New PdfPen(Color.Black, 0.75F)
topSpace.Graphics.SetTransparency(0.5F)
topSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y)
y = y - 1 - size.Height
topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format)
Dim rightSpace As New PdfPageTemplateElement(margin.Right, pageSize.Height)
doc.Template.Right = rightSpace
Dim bottomSpace As New PdfPageTemplateElement(pageSize.Width, margin.Bottom)
bottomSpace.Foreground = True
doc.Template.Bottom = bottomSpace
'draw footer label
y = font.Height + 1
bottomSpace.Graphics.SetTransparency(0.5F)
bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y)
y = y + 1
Dim pageNumber As New PdfPageNumberField()
Dim pageCount As New PdfPageCountField()
Dim pageNumberLabel As New PdfCompositeField()
pageNumberLabel.AutomaticFields = New PdfAutomaticField() {pageNumber, pageCount}
pageNumberLabel.Brush = PdfBrushes.Black
pageNumberLabel.Font = font
pageNumberLabel.StringFormat = format
pageNumberLabel.Text = "page {0} of {1}"
pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y)
Dim headerImage As PdfImage = PdfImage.FromFile("Header.png")
Dim pageLeftTop As New PointF(-margin.Left, -margin.Top)
Dim header As New PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension)
header.Foreground = False
header.Graphics.SetTransparency(0.5F)
header.Graphics.DrawImage(headerImage, 0, 0)
doc.Template.Stamps.Add(header)
Dim footerImage As PdfImage = PdfImage.FromFile("Footer.png")
y = pageSize.Height - footerImage.PhysicalDimension.Height
Dim footerLocation As New PointF(-margin.Left, y)
Dim footer As New PdfPageTemplateElement(footerLocation, footerImage.PhysicalDimension)
footer.Foreground = False
footer.Graphics.SetTransparency(0.5F)
footer.Graphics.DrawImage(footerImage, 0, 0)
doc.Template.Stamps.Add(footer)
End Sub
Private Shared Function DrawPageTitle(ByVal page As PdfPageBase, ByVal y As Single) As Single
Dim brush1 As PdfBrush = PdfBrushes.MidnightBlue
Dim brush2 As PdfBrush = PdfBrushes.Red
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 12.0F, FontStyle.Bold))
Dim title As String = "Your Account Information(* = Required)"
Dim size As SizeF = font1.MeasureString(title)
Dim x As Single = (page.Canvas.ClientSize.Width - size.Width) / 2
page.Canvas.DrawString("Your Account Information(", font1, brush1, x, y)
size = font1.MeasureString("Your Account Information(")
x = x + size.Width
page.Canvas.DrawString("* = Required", font1, brush2, x, y)
size = font1.MeasureString("* = Required")
x = x + size.Width
page.Canvas.DrawString(")", font1, brush1, x, y)
y = y + size.Height
y = y + 3
Dim font2 As New PdfTrueTypeFont(New Font("Arial", 8.0F, FontStyle.Italic))
Dim p As String = "Your information is not public, shared in anyway, or displayed on this site."
page.Canvas.DrawString(p, font2, brush1, 0, y)
Return y + font2.Height
End Function
Private Shared Function DrawFormSection(ByVal label As String, ByVal page As PdfPageBase, _
ByVal y As Single) As Single
Dim brush1 As PdfBrush = PdfBrushes.LightYellow
Dim brush2 As PdfBrush = PdfBrushes.DarkSlateGray
Dim font As New PdfTrueTypeFont(New Font("Arial", 9.0F, FontStyle.Bold))
Dim format As New PdfStringFormat()
Dim height As Single = font.MeasureString(label).Height
page.Canvas.DrawRectangle(brush2, 0, y, page.Canvas.ClientSize.Width, height + 2)
page.Canvas.DrawString(label, font, brush1, 2, y + 1)
y = y + height + 2
Dim pen As New PdfPen(PdfBrushes.LightSkyBlue, 0.75F)
page.Canvas.DrawLine(pen, 0, y, page.Canvas.ClientSize.Width, y)
Return y + 0.75F
End Function
Private Shared Function DrawFormField(ByVal fieldNode As XPathNavigator, ByVal form As PdfForm, _
ByVal page As PdfPageBase, ByVal y As Single, ByVal fieldIndex As Integer) As Single
Dim width As Single = page.Canvas.ClientSize.Width
Dim padding As Single = 2
'measure field label
Dim label As String = fieldNode.GetAttribute("label", "")
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 9.0F))
Dim format As New PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Middle)
Dim labelMaxWidth As Single = width * 0.4F - 2 * padding
Dim labelSize As SizeF = font1.MeasureString(label, labelMaxWidth, format)
'measure field height
Dim fieldHeight As Single = MeasureFieldHeight(fieldNode)
Dim height As Single = If(labelSize.Height > fieldHeight, labelSize.Height, fieldHeight)
height = height + 2
'draw background
Dim brush As PdfBrush = PdfBrushes.SteelBlue
If fieldIndex Mod 2 = 1 Then
brush = PdfBrushes.LightGreen
End If
page.Canvas.DrawRectangle(brush, 0, y, width, height)
'draw field label
Dim brush1 As PdfBrush = PdfBrushes.LightYellow
Dim labelBounds As New RectangleF(padding, y, labelMaxWidth, height)
page.Canvas.DrawString(label, font1, brush1, labelBounds, format)
'daw field
Dim fieldMaxWidth As Single = width * 0.57F - 2 * padding
Dim fieldX As Single = labelBounds.Right + 2 * padding
Dim fieldY As Single = y + (height - fieldHeight) / 2
Dim fieldType As String = fieldNode.GetAttribute("type", "")
Dim fieldId As String = fieldNode.GetAttribute("id", "")
Dim required As Boolean = "true" = fieldNode.GetAttribute("required", "")
Select Case fieldType
Case "text", "password"
Dim textField As New PdfTextBoxField(page, fieldId)
textField.Bounds = New RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight)
textField.BorderWidth = 0.75F
textField.BorderStyle = PdfBorderStyle.Solid
textField.Required = required
If "password" = fieldType Then
textField.Password = True
End If
If "true" = fieldNode.GetAttribute("multiple", "") Then
textField.Multiline = True
textField.Scrollable = True
End If
form.Fields.Add(textField)
Case "checkbox"
Dim checkboxField As New PdfCheckBoxField(page, fieldId)
Dim checkboxWidth As Single = fieldHeight - 2 * padding
Dim checkboxHeight As Single = checkboxWidth
checkboxField.Bounds = New RectangleF(fieldX, fieldY + padding, checkboxWidth, checkboxHeight)
checkboxField.BorderWidth = 0.75F
checkboxField.Style = PdfCheckBoxStyle.Cross
checkboxField.Required = required
form.Fields.Add(checkboxField)
Case "list"
Dim itemNodes As XPathNodeIterator = fieldNode.Select("item")
If "true" = fieldNode.GetAttribute("multiple", "") Then
Dim listBoxField As New PdfListBoxField(page, fieldId)
listBoxField.Bounds = New RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight)
listBoxField.BorderWidth = 0.75F
listBoxField.MultiSelect = True
listBoxField.Font = New PdfFont(PdfFontFamily.Helvetica, 9.0F)
listBoxField.Required = required
'add items into list box.
For Each itemNode As XPathNavigator In itemNodes
Dim text As String = itemNode.SelectSingleNode("text()").Value
listBoxField.Items.Add(New PdfListFieldItem(text, text))
Next itemNode
listBoxField.SelectedIndex = 0
form.Fields.Add(listBoxField)
Exit Select
End If
If itemNodes IsNot Nothing AndAlso itemNodes.Count <= 3 Then
Dim radioButtonListFile As New PdfRadioButtonListField(page, fieldId)
radioButtonListFile.Required = required
'add items into radio button list.
Dim fieldItemHeight As Single = fieldHeight / itemNodes.Count
Dim radioButtonWidth As Single = fieldItemHeight - 2 * padding
Dim radioButtonHeight As Single = radioButtonWidth
For Each itemNode As XPathNavigator In itemNodes
Dim text As String = itemNode.SelectSingleNode("text()").Value
Dim fieldItem As New PdfRadioButtonListItem(text)
fieldItem.BorderWidth = 0.75F
fieldItem.Bounds = New RectangleF(fieldX, fieldY + padding, radioButtonWidth, radioButtonHeight)
radioButtonListFile.Items.Add(fieldItem)
Dim fieldItemLabelX As Single = fieldX + radioButtonWidth + padding
Dim fieldItemLabelSize As SizeF = font1.MeasureString(text)
Dim fieldItemLabelY As Single = fieldY + (fieldItemHeight - fieldItemLabelSize.Height) / 2
page.Canvas.DrawString(text, font1, brush1, fieldItemLabelX, fieldItemLabelY)
fieldY = fieldY + fieldItemHeight
Next itemNode
form.Fields.Add(radioButtonListFile)
Exit Select
End If
'combo box
Dim comboBoxField As New PdfComboBoxField(page, fieldId)
comboBoxField.Bounds = New RectangleF(fieldX, fieldY, fieldMaxWidth, fieldHeight)
comboBoxField.BorderWidth = 0.75F
comboBoxField.Font = New PdfFont(PdfFontFamily.Helvetica, 9.0F)
comboBoxField.Required = required
'add items into combo box.
For Each itemNode As XPathNavigator In itemNodes
Dim text As String = itemNode.SelectSingleNode("text()").Value
comboBoxField.Items.Add(New PdfListFieldItem(text, text))
Next itemNode
form.Fields.Add(comboBoxField)
End Select
If required Then
'draw *
Dim flagX As Single = width * 0.97F + padding
Dim font3 As New PdfTrueTypeFont(New Font("Arial", 10.0F, FontStyle.Bold))
Dim size As SizeF = font3.MeasureString("*")
Dim flagY As Single = y + (height - size.Height) / 2
page.Canvas.DrawString("*", font3, PdfBrushes.Red, flagX, flagY)
End If
Return y + height
End Function
Private Shared Function MeasureFieldHeight(ByVal fieldNode As XPathNavigator) As Single
Dim fieldType As String = fieldNode.GetAttribute("type", "")
Dim defaultHeight As Single = 16.0F
Select Case fieldType
Case "text", "password"
If "true" = fieldNode.GetAttribute("multiple", "") Then
Return defaultHeight * 3
End If
Return defaultHeight
Case "checkbox"
Return defaultHeight
Case "list"
If "true" = fieldNode.GetAttribute("multiple", "") Then
Return defaultHeight * 3
End If
Dim itemNodes As XPathNodeIterator = fieldNode.Select("item")
If itemNodes IsNot Nothing AndAlso itemNodes.Count <= 3 Then
Return defaultHeight * 3
End If
Return defaultHeight
End Select
Dim message As String = String.Format("Invalid field type: {0}", fieldType)
Throw New ArgumentException(message)
End Function
End Class
End Namespace
Wednesday, 06 April 2011 09:16
PDF Link in C#, VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
namespace Link
{
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;
float x = 0;
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12));
String label = "Simple Link: ";
PdfStringFormat format = new PdfStringFormat();
format.MeasureTrailingSpaces = true;
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12, FontStyle.Underline));
String url1 = "http://www.e-iceblue.com";
page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y);
y = y + font1.MeasureString(url1).Height;
label = "Web Link: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
String text = "e-iceblue";
PdfTextWebLink link2 = new PdfTextWebLink();
link2.Text = text;
link2.Url = url1;
link2.Font = font1;
link2.Brush = PdfBrushes.Blue;
link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
y = y + font1.MeasureString(text).Height;
label = "URI Annonationa: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
text = "Google";
PointF location = new PointF(x, y);
SizeF size = font1.MeasureString(text);
RectangleF linkBounds = new RectangleF(location, size);
PdfUriAnnotation link3 = new PdfUriAnnotation(linkBounds);
link3.Border = new PdfAnnotationBorder(0);
link3.Uri = "http://www.google.com";
(page as PdfNewPage).Annotations.Add(link3);
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
y = y + size.Height;
label = "URI Annonationa Action: ";
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
x = font.MeasureString(label, format).Width;
text = "JavaScript Action (Click Me)";
location = new PointF(x, y);
size = font1.MeasureString(text);
linkBounds = new RectangleF(location, size);
PdfUriAnnotation link4 = new PdfUriAnnotation(linkBounds);
link4.Border = new PdfAnnotationBorder(0.75f);
link4.Color = Color.LightGray;
//script
String script
= "app.alert({"
+ " cMsg: \"Hello.\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action = new PdfJavaScriptAction(script);
link4.Action = action;
(page as PdfNewPage).Annotations.Add(link4);
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
y = y + size.Height;
//Save pdf file.
doc.SaveToFile("Link.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("Link.pdf");
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Annotations
Imports Spire.Pdf.Graphics
Namespace Link
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
Dim x As Single = 0
Dim font As New PdfTrueTypeFont(New Font("Arial", 12))
Dim label As String = "Simple Link: "
Dim format As New PdfStringFormat()
format.MeasureTrailingSpaces = True
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
Dim font1 As New PdfTrueTypeFont(New Font("Arial", 12, FontStyle.Underline))
Dim url1 As String = "http://www.e-iceblue.com"
page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y)
y = y + font1.MeasureString(url1).Height
label = "Web Link: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
Dim text As String = "e-iceblue"
Dim link2 As New PdfTextWebLink()
link2.Text = text
link2.Url = url1
link2.Font = font1
link2.Brush = PdfBrushes.Blue
link2.DrawTextWebLink(page.Canvas, New PointF(x, y))
y = y + font1.MeasureString(text).Height
label = "URI Annonationa: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
text = "Google"
Dim location As New PointF(x, y)
Dim size As SizeF = font1.MeasureString(text)
Dim linkBounds As New RectangleF(location, size)
Dim link3 As New PdfUriAnnotation(linkBounds)
link3.Border = New PdfAnnotationBorder(0)
link3.Uri = "http://www.google.com"
TryCast(page, PdfNewPage).Annotations.Add(link3)
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
y = y + size.Height
label = "URI Annonationa Action: "
page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
x = font.MeasureString(label, format).Width
text = "JavaScript Action (Click Me)"
location = New PointF(x, y)
size = font1.MeasureString(text)
linkBounds = New RectangleF(location, size)
Dim link4 As New PdfUriAnnotation(linkBounds)
link4.Border = New PdfAnnotationBorder(0.75F)
link4.Color = Color.LightGray
'script
Dim script As String _
= "app.alert({" _
& " cMsg: ""Hello.""," _
& " nIcon: 3," _
& " cTitle: ""JavaScript Action""" _
& "});"
Dim action As New PdfJavaScriptAction(script)
link4.Action = action
TryCast(page, PdfNewPage).Annotations.Add(link4)
page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
y = y + size.Height
'Save pdf file.
doc.SaveToFile("Link.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("Link.pdf")
End Sub
End Class
End Namespace
Published in
Interaction
Tagged under



