Wednesday, 06 April 2011 06:56
PDF ImageWaterMark in C#, VB.NET
The sample demonstrates how to draw an image as watermark in PDF document.

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace ImageWaterMark
{
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);
Image img = Image.FromFile(@"Background.png");
page.BackgroundImage = img;
//Draw page
DrawPage(page);
//Save pdf file.
doc.SaveToFile("ImageWaterMark.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("ImageWaterMark.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 ImageWaterMark
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 img As Image = Image.FromFile("Background.png")
page.BackgroundImage = img
'Draw table
DrawPage(page)
'Save pdf file.
doc.SaveToFile("ImageWaterMark.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("ImageWaterMark.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
WaterMark
Wednesday, 06 April 2011 05:52
PDF TextWaterMark in C#, VB.NET
using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
namespace TextWaterMark
{
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);
PdfTilingBrush brush
= new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
brush.Graphics.SetTransparency(0.3f);
brush.Graphics.Save();
brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
brush.Graphics.RotateTransform(-45);
brush.Graphics.DrawString("Spire.Pdf Demo",
new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
new PdfStringFormat(PdfTextAlignment.Center));
brush.Graphics.Restore();
brush.Graphics.SetTransparency(1);
page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
//Draw the page
DrawPage(page);
//Save pdf file.
doc.SaveToFile("TextWaterMark.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("TextWaterMark.pdf");
}
private static void DrawPage(PdfPageBase page)
{
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("Category Sales by Year", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
y = y + font1.MeasureString("Category Sales by Year", format1).Height;
y = y + 5;
String[][] data
= {
new String[]{"Category Name", "1994 Sale Amount", "1995 Sale Amount", "1996 Sale Amount"},
new String[]{"Beverages", "38,487.20", "102,479.46", "126,901.53"},
new String[]{"Condiments", "16,402.95", "51,041.83", "38,602.31"},
new String[]{"Confections", "23,812.90", "79,752.25", "63,792.07"},
new String[]{"Dairy Products", "30,027.79", "116,495.45", "87,984.05"},
new String[]{"Grains/Cereals", "7,313.92", "53,823.48", "34,607.19"},
new String[]{"Meat/Poultry", "19,856.86", "77,164.75", "66,000.75"},
new String[]{"Produce", "10,694.96", "45,973.69", "43,315.93"},
new String[]{"Seafood", "16,247.77", "64,195.51", "50,818.46"}
};
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.BorderPen = new PdfPen(brush1, 0.75f);
table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
table.Style.ShowHeader = true;
table.DataSource = data;
PdfLayoutResult result = table.Draw(page, new PointF(0, y));
y = y + result.Bounds.Height + 5;
PdfBrush brush2 = PdfBrushes.LightGray;
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 9f));
page.Canvas.DrawString("* All data from NorthWind", font2, brush2, 5, y);
}
}
}
Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.Tables
Namespace TextWaterMark
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 brush As New PdfTilingBrush(New SizeF(page.Canvas.ClientSize.Width \ 2, page.Canvas.ClientSize.Height \ 3))
brush.Graphics.SetTransparency(0.3F)
brush.Graphics.Save()
brush.Graphics.TranslateTransform(brush.Size.Width \ 2, brush.Size.Height \ 2)
brush.Graphics.RotateTransform(-45)
brush.Graphics.DrawString("Spire.Pdf Demo", New PdfFont(PdfFontFamily.Helvetica, 24), _
PdfBrushes.Violet, 0, 0, New PdfStringFormat(PdfTextAlignment.Center))
brush.Graphics.Restore()
brush.Graphics.SetTransparency(1)
page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.ClientSize))
'Draw the page
DrawPage(page)
'Save pdf file.
doc.SaveToFile("TextWaterMark.pdf")
doc.Close()
'Launching the Pdf file.
Process.Start("TextWaterMark.pdf")
End Sub
Private Shared Sub DrawPage(ByVal page As PdfPageBase)
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("Category Sales by Year", font1, brush1, _
page.Canvas.ClientSize.Width \ 2, y, format1)
y = y + font1.MeasureString("Category Sales by Year", format1).Height
y = y + 5
Dim data()() As String _
= {New String() {"Category Name", "1994 Sale Amount", "1995 Sale Amount", "1996 Sale Amount"}, _
New String() {"Beverages", "38,487.20", "102,479.46", "126,901.53"}, _
New String() {"Condiments", "16,402.95", "51,041.83", "38,602.31"}, _
New String() {"Confections", "23,812.90", "79,752.25", "63,792.07"}, _
New String() {"Dairy Products", "30,027.79", "116,495.45", "87,984.05"}, _
New String() {"Grains/Cereals", "7,313.92", "53,823.48", "34,607.19"}, _
New String() {"Meat/Poultry", "19,856.86", "77,164.75", "66,000.75"}, _
New String() {"Produce", "10,694.96", "45,973.69", "43,315.93"}, _
New String() {"Seafood", "16,247.77", "64,195.51", "50,818.46"}}
Dim table As New PdfTable()
table.Style.CellPadding = 2
table.Style.BorderPen = New PdfPen(brush1, 0.75F)
table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue
table.Style.DefaultStyle.Font = New PdfTrueTypeFont(New Font("Arial", 10.0F))
table.Style.HeaderSource = PdfHeaderSource.Rows
table.Style.HeaderRowCount = 1
table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue
table.Style.HeaderStyle.Font = New PdfTrueTypeFont(New Font("Arial", 11.0F, FontStyle.Bold))
table.Style.HeaderStyle.StringFormat = New PdfStringFormat(PdfTextAlignment.Center)
table.Style.ShowHeader = True
table.DataSource = data
Dim result As PdfLayoutResult = table.Draw(page, New PointF(0, y))
y = y + result.Bounds.Height + 5
Dim brush2 As PdfBrush = PdfBrushes.LightGray
Dim font2 As New PdfTrueTypeFont(New Font("Arial", 9.0F))
page.Canvas.DrawString("* All data from NorthWind", font2, brush2, 5, y)
End Sub
End Class
End Namespace
Published in
WaterMark
Wednesday, 21 July 2010 10:12
Word Watermark in C#, VB.NET
The sample demonstrates how to insert watermark into Word document.

private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
InsertWatermark(document);
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void InsertWatermark(Document document)
{
Paragraph paragraph = document.AddSection().AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a watermark into a document.");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Spire.Doc can generate, modify, convert, render and print documents without utilizing Microsoft Word.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("The sample demonstrates how to insert a watermark into a document.");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Microsoft Word is a word processor designed by Microsoft. It was first released in 1983 under the name Multi-Tool Word for Xenix systems. Subsequent versions were later written for several other platforms including IBM PCs running DOS (1983), the Apple Macintosh (1984), the AT&T Unix PC (1985), Atari ST (1986), SCO UNIX, OS/2, and Microsoft Windows (1989).");
paragraph = document.Sections[0].AddParagraph();
paragraph = document.Sections[0].AddParagraph();
paragraph.AppendText("Spire.Doc can generate, modify, convert, render and print documents without utilizing Microsoft Word.");
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "Watermark Demo";
txtWatermark.FontSize = 90;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
'Create word document
Dim document_Renamed As New Document()
InsertWatermark(document_Renamed)
'Save doc file.
document_Renamed.SaveToFile("Sample.doc",FileFormat.Doc)
'Launching the MS Word file.
WordDocViewer("Sample.doc")
End Sub
Private Sub InsertWatermark(ByVal document_Renamed As Document)
Dim paragraph_Renamed As Paragraph = document_Renamed.AddSection().AddParagraph()
paragraph_Renamed.AppendText("The sample demonstrates how to insert a watermark into a document.")
paragraph_Renamed.ApplyStyle(BuiltinStyle.Heading2)
Dim txtWatermark As New TextWatermark()
txtWatermark.Text = "Watermark Demo"
txtWatermark.FontSize = 90
txtWatermark.Layout = WatermarkLayout.Diagonal
document_Renamed.Watermark = txtWatermark
End Sub
Private Sub WordDocViewer(ByVal fileName As String)
Try
Process.Start(fileName)
Catch
End Try
End Sub
Published in
Context
