I am using iTextSharp to create a pdf from a gridview. When there is a null value in the table the pdf places a   in its place. How do I remove this and show the null value? I do know the pdf is created after the gridview is already populated however is there a way to tell it that nulls should show as blanks? My code is as follows:
Protected Sub btnPDF_Click(sender As Object, e As System.EventArgs) Handles btnPDF.Click
Dim pdfTable As New PdfPTable(grdResults.HeaderRow.Cells.Count)
For Each headerCell As TableCell In grdResults.HeaderRow.Cells
Dim font As New Font()
font.Color = New BaseColor(grdResults.HeaderStyle.ForeColor)
Dim pdfCell As New PdfPCell(New Phrase(headerCell.Text, font))
pdfCell.BackgroundColor = New BaseColor(grdResults.HeaderStyle.BackColor)
pdfTable.AddCell(pdfCell)
Next
For Each gridViewRow As GridViewRow In grdResults.Rows
For Each tableCell As TableCell In gridViewRow.Cells
Dim font As New Font()
font.Color = New BaseColor(grdResults.RowStyle.ForeColor)
Dim pdfCell As New PdfPCell(New Phrase(tableCell.Text, font))
pdfCell.BackgroundColor = New BaseColor(grdResults.RowStyle.BackColor)
pdfTable.AddCell(pdfCell)
Next
Next
Dim pdfDocument As New Document(iTextSharp.text.PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
pdfDocument.SetPageSize(PageSize.A4.Rotate())
PdfWriter.GetInstance(pdfDocument, Response.OutputStream)
pdfDocument.Open()
pdfDocument.Add(pdfTable)
pdfDocument.Close()
Response.ContentType = "application/pdf"
Response.AppendHeader("content-disposition", "attachment, filename-results.pdf")
Response.Write(PdfDocument)
Response.Flush()
Response.End()
End Sub
The values that I was receiving the   in the pdf came from calculated fields. When there was no value for the calculation it would not put anything in the gridview cell so in turn it would put the blank space value in the pdf. I went back to the calculations and if a value was not determined than I placed a space for the value. For instance -
If dtRaw.Rows(0).Item("ParameterName").ToString() <> "NOY" Then
drCalcRow("NAAQGreater") = iNAAQS
drCalcRow("NAAQGreater80") = iNAAQS80
Else
drCalcRow("NAAQGreater") = " "
drCalcRow("NAAQGreater80") = " "
End If
It works however it adds a lot to the code. It would be nice to have something one time that would check when it loops through the rows as it writes the pdf. I have tried a couple of things but nothing worked or even made an impact.
Related
I have my temp PDF on the network and am using asp to fill in the fields and then download the file.
The problem I have is that the file downloaded is just the blank template, none of the fields are filled?
My code
Dim doc As New Document(PageSize.A4.Rotate)
Dim ms As New MemoryStream()
Dim writer = PdfWriter.GetInstance(doc, ms)
writer.Open()
Dim PdfR As New PdfReader("http://192.168.0.221/template.pdf")
Dim PdfS As New PdfStamper(PdfR, ms)
Dim fields As AcroFields = PdfS.AcroFields
fields.SetField("s1", "00")
fields.SetField("pono", "100")
PdfS.FormFlattening = True
PdfS.Close()
PdfR.Close()
Dim r = System.Web.HttpContext.Current.Response
r.ContentType = "application/pdf"
r.AddHeader("Content-Disposition", String.Format("attachment;filename=Testing.pdf", "Testing"))
r.BinaryWrite(ms.ToArray)
If anyone else ever hits this issue
1) If you dont mind your fields being editable then remove the FormFlattening command
2) Else add this fields.GenerateAppearances = True
I have a webpage with an export button. Is it possible to export a sql table to excel when this button is pressed? I can do it with a gridview but would just like a simple button with code behind to do the work. Can someone point me in the direction I need?
Here is a utility function you can use:
Public Shared Sub ExportToSpreadsheet(table As DataTable, filename As String)
' Get a hold of the HTTP context and clear it, because we are going to push the CSV data through the context
Dim context = HttpContext.Current
context.Response.Clear()
' Loop through each column in your data table
For Each column As DataColumn In table.Columns
' Write column names
context.Response.Write(column.ColumnName + ";")
Next
context.Response.Write(Environment.NewLine)
' Loop through each row in the data table
For Each row As DataRow In table.Rows
' Loop through each column in row
For i As Integer = 0 To table.Columns.Count - 1
' Write each column value
context.Response.Write(row(i).ToString().Replace(";", [String].Empty) & ";")
Next
' Write a new line between rows of data
context.Response.Write(Environment.NewLine)
Next
' Set the content type and headers
context.Response.ContentType = "text/csv"
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename & ".csv")
context.Response.[End]()
End Sub
Then you can call it like this:
ExportToSpreadsheet(YourDataTable, "YourFileName")
Note: Since it is a Shared function, then you can put it in a utility class and do not need to instantiate (New) the class to use the function.
If you want it in excel you can use the following code. Select the data and put it in a Gridview and do the following.
Dim GridView1 As New GridView
SqlDataSource1.SelectCommand = "SELECT * FROM TableName"
GridView1.DataSource = SqlDataSource1
GridView1.DataBind()
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
GridView1.RenderControl(oHtmlTextWriter)
Response.Write(oStringWriter.ToString())
Response.End()
You can also format the Gridview to make it look good on the Excel Sheet.
I have the following code in my .aspx file
<asp:Table ID="tabStudent" runat="server"></asp:Table>
I declared all variables which I used below. I wrote the following code snippet in the page load event. But it only shows only one row which having last row in my database. But when I am using message box inside this while loop to print those values I am getting all the rows.
Reader = Command.ExecuteReader()
While Reader.Read()
lblRollNo.Text = Reader.Item(0)
lblName.Text = Reader.Item(1)
lblDob.Text = Reader.Item(2)
tcRollNo.Controls.Add(lblRollNo)
tcName.Controls.Add(lblName)
tcDob.Controls.Add(lblDob)
TableRow.Cells.Add(tcRollNo)
TableRow.Cells.Add(tcName)
TableRow.Cells.Add(tcDob)
tabStudent.Rows.Add(TableRow)
End While
what is wrong with this code?
You are not creating new controls in the loop but you're always reusing the same.
While Reader.Read()
TableRow = New TableRow()
lblRollNo = New Label()
tcRollNo = New TableCell()
' and so on ...
lblRollNo.Text = Reader.Item(0)
lblName.Text = Reader.Item(1)
lblDob.Text = Reader.Item(2)
tcRollNo.Controls.Add(lblRollNo)
tcName.Controls.Add(lblName)
tcDob.Controls.Add(lblDob)
TableRow.Cells.Add(tcRollNo)
TableRow.Cells.Add(tcName)
TableRow.Cells.Add(tcDob)
tabStudent.Rows.Add(TableRow)
End While
I just noticed it last night,
Anyway, let's get to the interesting case here.
I have a ButtonField within DataGrid, and if you notice it here...
The Interface of that ButtonField is looks like a LINK.
But if we hover on it, it appeared as Javascript's call.
Here is the Image ScreenShot
Ya, that's the 1st case.
IT IS a javascript's call.
I didnt notice about it lately. (hehehe).
Then, if we click on that... it would call the createPDF() function. The function behind the scene (which I'm using VB.net) is to execute these code;
Protected Sub createPDF()
Dim document As New Document()
Dim mem As LengthFixingStream = New LengthFixingStream()
' instantiate a iTextSharp.text.pdf.Document
'Dim mem As New MemoryStream()
' PDF data will be written here
PdfWriter.GetInstance(document, mem)
' tie a PdfWriter instance to the stream
document.Open()
Dim titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD)
document.Add(New Paragraph("Northwind Traders Receipt", titleFont))
document.Close()
' automatically closes the attached MemoryStream
Dim docData As Byte() = mem.GetBuffer()
' get the generated PDF as raw data
' write the document data to response stream and set appropriate headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=testdoc.pdf")
Response.ContentType = "application/pdf"
Response.BinaryWrite(docData)
Response.[End]()
End Sub
But somehow... this of course would not deliver the PDF into the browser.
BEcause It's called by Javascript, nor the direct as Hyperlink (normally).
Thus, I'm wondering could we get the ASP.net Call new Window,
and then redirect the createPDF() result into it?
Correct me if i'm wrong...
Here is just some mockup so you get the idea. I haven't tested this. Basically you will have to put the above code in a new page...say "receipt.aspx" and execute it on the load event...you will need to setup an id parameter...if data is being pulled from the db to generate the pdf.
on the button click add the following
Dim sb As New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("window.open('receipt.aspx.htm?id=123'', 'Receipt',")
sb.Append("'width=800, height=800, menubar=yes, resizable=no');<")
sb.Append("/script>")
Dim t As Type = Me.GetType()
If Not ClientScript.IsStartUpScriptRegistered(t, "PopupScript") Then
ClientScript.RegisterStartUpScript(t, "PopupScript", sb.ToString())
End If
Notice the "id=123" querystring value I am passing to receipt.aspx?
You can then call that in the receipt.aspx page like this
Dim id as String = Request.QueryString("id")
CreatePDF(id)
...shoot! Just realized you are using a Grid...the principle remains the same, just wireup the buttons on RowDataBound event.
Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim Id As String = DirectCast(e.Row.Cells(0).FindControl("quotationid"), Label).Text
Dim myButton As New Button
myButton = DirectCast(e.Row.Cells(4).FindControl("btnViewReceipt"), Button)
myButton.Attributes.Add("OnClick", "window.open('receipt.aspx?id=" + id + "','Receipt','scrollbars=yes','width=800,height=800')")
End If
End Sub
I am new to ASP.NET. I am trying to display my sql results using a list view. I am using the example for grouping my results by a data field from the 4GuysFromRolla.com website. However, I find the way of grouping the items by a data field to be a bit clumsy. Is there a better way to do it?
Thanks.
Nested ListView - http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html
I have never used a ListView, but I did do grouping in a GridView. You can try porting this over to a ListView if you want:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim tblGrid As Table = Me.GridView1.Controls(0)
Dim strLastCat As String = "#"
Dim row As GridViewRow
For Each row In GridView1.Rows
Dim intRealIndex As Integer = tblGrid.Rows.GetRowIndex(row)
Dim strCat As String = Me.GridView1.DataKeys(row.RowIndex).Value
If strLastCat <> strCat Then
Dim rowHeader As New GridViewRow(intRealIndex, intRealIndex, DataControlRowType.Separator, DataControlRowState.Normal)
Dim newCell As New TableCell
newCell.ColumnSpan = Me.GridView1.Columns.Count
newCell.BackColor = System.Drawing.Color.FromArgb(61, 138, 20)
newCell.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255)
newCell.Font.Bold = True
newCell.Font.Size = New FontUnit(FontSize.Larger)
newCell.Text = strCat
rowHeader.Cells.Add(newCell)
tblGrid.Controls.AddAt(intRealIndex, rowHeader)
strLastCat = strCat
End If
Next
MyBase.Render(writer)
End Sub
The code creates headers each category. The final version can be viewed here: http://www.truedietreviews.com/diet-reviews/