Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 22-Dec-12 0:10am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Karwa_Vivek at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 460
1 OriginalGriff 220
2 Kornfeld Eliyahu Peter 210
3 DamithSL 210
4 Peter Leow 100
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100