Link to home
Start Free TrialLog in
Avatar of rowmark
rowmark

asked on

ITextSharp Binary Data issue

Hello,

I have a crystal report in my ASP.net application and I am programmatically trying to create a pdf from that using ItextSharp. I do understand that from Crystal report you can export the report to pdf.

The reason I am again doing it from iTextSharp is that I need to embedd some javaScript into the pdf as shown in my code.

The problem I am facing is, I don't know how to add the binary data from the report into the                 doc.Add(New Paragraph(blob ))

If I just write a string value inot                 doc.Add(New Paragraph("Please print pdf" )) it is working absolutely fine.

Thanks in advance
Dim blob As Byte()
Dim rpt As ReportDocument = New ReportDocument()
 rpt.Load(Server.MapPath("reportNew.rpt"))
 
Dim strStream As New  _
            System.IO.BinaryReader(rpt.ExportToStream(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat))
Dim doc As New Document()
                Dim js As New StringBuilder
                Dim pdf As String = "c:\Print2Printer.pdf"
                Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdf, FileMode.Create))
                doc.Open()
 
                js.Append("var pp = this.getPrintParams();")
                js.Append("var iCopies = 2;")
                js.Append("pp.interactive = pp.constants.interactionLevel.silent;")
                js.Append("for ( var i = 0; i < 3; i++ ) { pp.firstPage = i; pp.lastPage = i;")
                js.Append("this.print(pp);")
                js.Append("}")
                Dim jaction As PdfAction = PdfAction.JavaScript(js.ToString(), writer)
                writer.AddJavaScript(jaction)
 
                doc.Add(New Paragraph(blob ))
                doc.Close()
 
                rpt.Close()
                rpt = Nothing

Open in new window

Avatar of jamesrh
jamesrh
Flag of United States of America image

I assume that the blob is image data?  If so, you can use the Image class of ITextSharp.  There is a relevent tutorial here:  http://itextsharp.sourceforge.net/tutorial/ch06.html  (it's all somewhat usefull but check out the Raw Image data section)

Basic code looks like:
//overloaded version of method being used is public static Image getInstance(byte[] img)
Image img= Image.getInstance(imagebytes);

//if you need to specify the position
img1.setAbsolutePosition(50, 500);

document.Add(img1);

Avatar of Mike McCracken
Mike McCracken

Avatar of rowmark

ASKER

jamesrh:,
thanks for your response. The blob is the actual crystal report binary data. I will try to use that as image and see if it works.

Thanks again. do let me know if you think of something else.
I guess I should have read the code a little more closely.
This article may provide some more insight more inline with what you are trying to do:  http://geekswithblogs.net/bsherwin/archive/2007/06/29/113566.aspx  Talks about merging two pdfs.  Which is esentially what you are trying to do.

Pay particular attention to:
reader = New iTextSharp.text.pdf.PdfReader(sqlbytes) 'takes a pdf document as an array of bytes.
Avatar of rowmark

ASKER

Thanks jamesr. I will surely try this and let you know the results.
Thanks again for you help.
Avatar of rowmark

ASKER

Jamesr,
I tried doing the merging stuff as you suggested. Though everything else works, The embedded javascript thing is not working.

Are there any limitations with iTextSharp??
ASKER CERTIFIED SOLUTION
Avatar of jamesrh
jamesrh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rowmark

ASKER

Thank you very much.
So what was the final fix for you?
i want to use iTextSharp to export my crystal report to PDF in my ASP.NET application.

I know ASP.NET has default export, however, i am facing a issue.
After export to pdf, the hyperlink inside the report is not there.

So i want to use iTextSharp to do the export ... and see how it goes.
So can i easy bind the rpt to a iTextSharp object, then export to PDF.

Please advise,
Tommy