Link to home
Start Free TrialLog in
Avatar of rowmark

asked on

ITextSharp Binary Data issue


I have a crystal report in my 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()
Dim strStream As New  _
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))
                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;")
                Dim jaction As PdfAction = PdfAction.JavaScript(js.ToString(), writer)
                doc.Add(New Paragraph(blob ))
                rpt = Nothing

Open in new window

Avatar of 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:  (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);


Avatar of Mike McCracken
Mike McCracken

Avatar of rowmark


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:  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


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


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??
Avatar of jamesrh
Flag of United States of America image

Link to home
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


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,