Hi,
i'm a new user and i have successfully created a pdf document.
Now i'm facing a new chalange as i would like to ad WaterMark to the existing document.
I was looking at samples at
http://pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=40&Itemid=51.
I was following them and i got an error:
This is my code:
(I'm curently coding a project in vb.net but feel free to write in C# since i'm familiar with both languages.)
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim fileData As Byte()
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("xxx").ConnectionString)
'some code...
connection.Open()
fileData = DirectCast(command.ExecuteScalar(), Byte())
connection.Close()
End Using
Dim mypdf As New System.IO.MemoryStream(fileData)
'Dim document As New PdfDocument(mypdf)
Dim document As PdfDocument = New PdfDocument()
document = PdfReader.Open(mypdf)
Dim page As PdfPage = document.Pages(0)
Dim gfx As XGraphics = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend)
Dim MyWaterM As String = "WaterMark"
' Get the size (in point) of the text
Dim options As XPdfFontOptions = New XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always)
Dim font As XFont = New XFont("Calibri", 12, XFontStyle.Regular, options)
Dim size As XSize = gfx.MeasureString(MyWaterM, font)
' Define a rotation transformation at the center of the page
gfx.TranslateTransform(page.Width.Point / 2, page.Height.Point / 2)
gfx.RotateTransform(-Math.Atan(page.Height.Point / page.Width.Point) * 180 / Math.PI)
gfx.TranslateTransform(-page.Width.Point / 2, -page.Height.Point / 2)
' Create a string format
Dim format As New XStringFormat()
format.Alignment = XStringAlignment.Near
format.LineAlignment = XLineAlignment.Near
' Create a dimmed red brush
Dim brush As XBrush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))
' Draw the string
gfx.DrawString(MyWaterM, font, brush, New XPoint((page.Width.Point - size.Width) / 2, (page.Height.Point - size.Height) / 2), format)
Dim stream As New MemoryStream()
document.Save(stream, False)
Dim data As Byte() = stream.ToArray()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", fileData.Length.ToString())
Response.BinaryWrite(data)
Response.End()
End Sub
If i remove the line:
Code:
gfx.DrawString(MyWaterM, font, brush, New XPoint((page.Width.Point - size.Width) / 2, (page.Height.Point - size.Height) / 2), format)
everything works
Please advise...
Thank you.