PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 01, 2024 12:22 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: WaterMark Trouble
PostPosted: Tue Mar 08, 2011 11:54 am 
Offline

Joined: Tue Mar 08, 2011 11:38 am
Posts: 6
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:
Image

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.


Top
 Profile  
Reply with quote  
 Post subject: Re: WaterMark Trouble
PostPosted: Tue Mar 08, 2011 11:59 am 
Offline

Joined: Tue Mar 08, 2011 11:38 am
Posts: 6
Oh... :roll:

I'm so sorry for spamming, i just found the problem... (i'm tired, been coding for more than 24h non stop)

i replaced
Code:
Response.AddHeader("content-length", fileData.Length.ToString())

with
Code:
Response.AddHeader("content-length", data.Length.ToString())


Sorry again :oops:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 72 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group