如何使用DataBase值创建PDF文件 [英] How to create PDF file with DataBase Values

查看:64
本文介绍了如何使用DataBase值创建PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用PDFSharp dll

创建一个Pdf文件并将文件保存在一个文件夹中。

Ny问题是当我通过一个特定的

从文本框到DrawString的字符串

结果没问题,但是当我使用

数据库值显示在创建的
$ b中$ b Pdf所有书面值都在同一个地方重叠。

我的意思是我只是在Pdf文件中得到一个黑色图像。

下面是我的代码尝试''

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

推荐答案

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





  • 变量的内容是什么 id
  • page.Width.Point page.Height.Point ?<的值是多少? / li>
  • 看起来每次调用 gfx.DrawString 都会在同一个地方进行绘制。


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

    • 来自我的某个应用程序的代码片段可能对您有所帮助。您需要在DrawString方法中为要写入的每一行指定X和Y坐标。注意,在While循环中,我保持对左边距使用相同的X坐标,并将每行的Y坐标(yPos)增加到我写出的字体的高度。



      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


      这篇关于如何使用DataBase值创建PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆