Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi am Using PDFSharp dll
to creates a Pdf file and to save the file in a Folder.
Ny Problem is that when i am passing a particular
string from textbox to the DrawString
the result is fine but when I am using
Database Values to show in the created
Pdf all the written values are overlapping in the same Place.
I mean I am just getting a black image in the Pdf File.
Below is the Code i am trying'
public sub create()
Dim document As PdfDocument = New PdfDocument
document.Info.Title = "testPdf"
' Create an empty page
        Dim page As PdfPage = document.AddPage

        ' Get an XGraphics object for drawing
        Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Bold)

        'Fetch Values from DataBase
        Dim id As Integer = 0
        Dim sd As New SqlDataAdapter("Select * from testTable", myCon.con)
        Dim dt As New DataTable
        sd.Fill(dt)
For i2 As Integer = 0 To dt.Rows.Count - 1
                id = dt.Rows(i2).Item("ID")
               gfx.DrawString("" & id & "", font, XBrushes.Black, New XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.TopLeft)
 Next
Dim filename As String = "test.pdf"
document.Save(filename)
End sub


I think am Missing something may be the loop.Please assist.Any Ideas
Posted

This snippet of code from one of my applications may be helpful to you. You need to specify, in the DrawString method, the X and Y coordinates for each line to be written. Note, how in the "While" loop, I keep use the same X coordinate for the left margin and increment the Y coordinate (yPos) for each line by the height of the font that I am writing out.

VB
intLineCount = 0
'
' Write Title
ptCursor.Offset(leftMargin, topMargin)
' Center "strTitle" String
intTitleStart = leftMargin + (((rightMargin - leftMargin) \ 2) - _
    (e.Graphics.MeasureString(strTitle, fntTitle).ToSize.Width \ 2))
e.Graphics.DrawString(strTitle, fntTitle, brushTitle, intTitleStart, ptCursor.Y)
intEndOfSection = ptCursor.Y + 5 + e.Graphics.MeasureString(strTitle, fntTitle).ToSize.Height
e.Graphics.DrawLine(New Pen(brushTitle), leftMargin, intEndOfSection, rightMargin, intEndOfSection)
ptCursor.Y = intEndOfSection + 10
' Write Column Headers
e.Graphics.DrawString(strColumnHeader, fntData, brushText, ptCursor.X, ptCursor.Y)
intEndOfSection = ptCursor.Y + e.Graphics.MeasureString(strColumnHeader, fntData).ToSize.Height
e.Graphics.DrawLine(New Pen(brushText), leftMargin, intEndOfSection, rightMargin, intEndOfSection)
ptCursor.X = leftMargin
ptCursor.Y = intEndOfSection + 10
'
' Iterate each line.
While intLineCount < linesPerPage
    strLine = GetNextDatabaseRow()
    If strLine Is Nothing Then
        bOK = False
        Exit While
    End If
    yPos = ptCursor.Y + (intLineCount * fntData.GetHeight(e.Graphics))
    e.Graphics.DrawString(strLine, fntData, Brushes.Black, leftMargin, yPos, New StringFormat)
    intLineCount += 1
End While
 
Share this answer
 
VB
For i2 As Integer = 0 To dt.Rows.Count - 1
    id = dt.Rows(i2).Item("ID")
    gfx.DrawString("" & id & "", font, XBrushes.Black, New XRect(0, 0, page.Width.Point, page.Height.Point), XStringFormats.TopLeft)
Next


  • What is the content of the variable id?
  • What are the values of page.Width.Point and page.Height.Point?
  • It looks like each call to gfx.DrawString will draw in the same place.
 
Share this answer
 
Comments
Karwa_Vivek 22-Dec-12 5:39am    
id is just integers like 1,2,3,4 like this
page.Width.Point and page.Height.Point is the page width and height
and i agree that each call is drawing in the same place.that is the problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900