Export Devexpress XtraPivotGrid with theme to pdf using Itextsharp - asp.net

I am using DevExpress ASPXPivotGrid control for my ASP.NET site.I am also using DevExpress ASPXPivotGridExporter control to ASPXPivotGrid to pdf. The problem I am having is that ASPXPivotGridExporter cannot export themes and right to left languages. Exported output always come out with default formating and just left to right languages. I would like to know if its possible to export pivot grid with theme and with rtl languages ? If not is there work around it with ITextSharp?
this what I tried so far
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "inline;filename=TestPage.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
ASPXPivotGrid1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 100.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
Using memoryStream As New MemoryStream
Chart1.SaveImage(memoryStream, ChartImageFormat.Png)
Dim img As Image = Image.GetInstance(memoryStream.GetBuffer())
img.ScalePercent(75.0F)
pdfDoc.Add(img)
End Using
pdfDoc.Close()
Response.Write(pdfDoc)
Response.[End]()

using aspxpivotgridexporter doesn't support right to left languages. but finally i was able to find a work around by exporting pivot grid to html using pivot grid exporter then I can add the needed styles to the resulting html file and adding the direction to the text wither rtl or ltr

Related

Trying to convert a gridview into a PDF document

Basic principle is that I am trying to take a GridView and turn it into a PDF document. The GridView includes controls like checkboxes and some css classes with some colouring.
I have tried putting this together, but I think I am misunderstanding how something works and now hit a dead end. At present it all compiles and runs, but when you try and open the returned PDF it says it fails to load. The returned file is 1KB in size, so I am guessing that it isn't actually a valid PDF.
I am using iText7 and htmlPDF, with my code looking like this.
Protected Sub ExportToPDF(sender As Object, e As ImageClickEventArgs)
Using sw As New StringWriter()
Using hw As New HtmlTextWriter(sw)
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
gvCSC.DataBind()
gvCSC.RenderControl(hw)
Dim pdfWriter As New PdfWriter(Response.OutputStream)
Dim pdfDoc As New PdfDocument(pdfWriter)
Dim sr As New StringReader(sw.ToString)
pdfDoc.SetDefaultPageSize(New PageSize(PageSize.A4.Rotate))
Dim doc As Document = HtmlConverter.ConvertToDocument(sr.ToString, pdfDoc, New ConverterProperties)
Response.End()
pdfDoc.Close()
End Using
End Using
End Sub
I am hoping that I am just being stupid as I've not used iText7 before and it just needs someone to point out the obvious for me. Any help in solving this will be appreciated.

itextSharp null value showing &nbsp

I am using iTextSharp to create a pdf from a gridview. When there is a null value in the table the pdf places a &nbsp 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 &nbsp 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.

itextsharp error: "The document has no pages."

I set as references three dlls:
itextsharp.dll: the core library
itextsharp.xtra.dll: extra functionality (PDF 2!)
itextsharp.pdfa.dll: PDF/A-related functionality
This project is hosted on http://sourceforge.net/projects/itextsharp/
You can find the latest release here:
http://sourceforge.net/projects/itextsharp/files/itextsharp/
I get an error when executing this code:
On pdfDoc.Close(), "The document has no pages."
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
gv.DataBind()
gv.AllowPaging = "False"
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Dim frm As New HtmlForm()
gv.Parent.Controls.Add(frm)
frm.Attributes("runat") = "server"
frm.Controls.Add(gv)
frm.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.[End]()
The class HTMLWorker is deprecated and should no longer be used. HTML to PDF functionality has been replaced by technology called XML Worker. I see that you include itextsharp.xtra and itextsharp.pdfa which are 2 DLLs you don't need. I don't see you including the xmlworker DLL.
As for the exception: when you get the message "The document has no pages", you are trying to create a document without any content (and that doesn't make sense). How is this possible? Well, it all depends on the content of sr. That content is either empty or it contains HTML that can't be interpreted by HTMLWorker.
Extra remark: next to itextpdf.xtra, you wrote (PDF 2!). While the xtra package contains functonality that didn't exist in PDF 1.7, it's not the PDF 2 package. The PDF 2 specification is to be expected at the earliest by the end of 2015 (a more realistic estimation is 2016). At iText, we've already implemented PDF 2.0 functionality based on the draft of the spec, but that functionality is (1) not limited to what is in the xtra package, and (2) not part of a specification that has been publicly released by ISO yet.

How to Read and do changes in HtmlTextWriter

In an ASP.NET site, I am rendering a GridView control to export it's details in a excel file
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Type", "application/vnd.ms-excel")
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
gridviewObject.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
Now, it's getting rendered,but the gridview uses few images to beautify the data.
While exporting the data I don't want to show images in excel.
So I want to find the tag and want to replace it by blank string.
Can anybody tell me how I can do that?

ASP.NET page to image or PDF

I have an ASP.NET page like this:
http://farm4.static.flickr.com/3631/3690714302_c17b259863.jpg
My table is Gridview and some Label, anybody can tell me how to create a button to convert my page to image or PDF file and save it to desktop.
It can be done by code on the backend that renders the html into a memory Graphic object and then serves that resultant bitmap back, or by printing the page (on the server) through a PDF writer and then capturing the result.
Javascript, no.
Or google "online convert html pdf" and use one of those services.
Hai use iTextSharp to convert your webpage to pdf.
It looks very nice.
Just download the dll file and add reference to your application.
Then in button click give the coding as follows.
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", attachment; filename="jaison.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(iTextSharp.text.PageSize.A2, 10, 10, 10, 10)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
Surely this code will help you.
All the best!

Resources