I am developing a aspx page where users upload a document.i am saving it as a binary data in database.now i want to create a pdffile using itextsharp.how to create a pdffile using the binary data from database.can anyone help me in this issue.
Please remember to click “Mark as Answer” on the post that helps you and to unmark it if a marked post does not actually answer your question.
Thank you!
----------------------
"Microsoft Community Contributor Award 2011"
We r uploading pdf files and saving as binarydata in database.now i want to create a pdf file with uploaded data(ie saved binary data in database) usinf itextsharp.Plz anyone help me..
In that case, you don't need iTextSharp. Create a generic handler (.ashx file), retrieve the binary data from the database in the ProcessRequest Method and send it to the client.
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim pdfID As Integer = Convert.ToInt32(context.Request.QueryString("pdfID"))
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Const SQL As String = "SELECT [pdfData] FROM [pdfTable] WHERE [pdfID] = @pdfID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@pdfID", pdfID)
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader
If myReader.Read Then
context.Response.ContentType = "application/pdf"
context.Response.BinaryWrite(myReader("pdfData"))
context.Response.AddHeader("Content-Disposition", "attachment; filename=download.pdf"
End If
myReader.Close()
myConnection.Close()
End Using
End Sub
Thanks for ur reply..to display binary data in pdf file is not a problem.see my requirement is to restrict the print option
of a pdffile .so what im trying to do is creating a new pdf file with no menu bar and will display in a iframe.creating pdf file with hiding menu bar and file tool bar is possible with iTextSahrp.The problem is creating pdffile with binarydata stored in
database.
anandraaz
0 Points
7 Posts
Creating Pdf file through binarydata in asp.net
Jul 28, 2009 06:48 AM|LINK
hi ,
I am developing a aspx page where users upload a document.i am saving it as a binary data in database.now i want to create a pdffile using itextsharp.how to create a pdffile using the binary data from database.can anyone help me in this issue.
regards
anandraj
santa_1975
Star
8574 Points
1499 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 07:04 AM|LINK
Hi,
Hope the below URL helps.
http://blog.rubypdf.com/2006/10/25/itextsharpitext-example-in-aspnet/
Thank you!
----------------------
"Microsoft Community Contributor Award 2011"
Heloril
Participant
1470 Points
255 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 07:05 AM|LINK
Hi,
You can do it directly in the page in the page load event of a page:
protected void Page_Load(object sender, EventArgs e) { //Report issue if (IssueList != null) { byte[] pdfFile = IssueListReport.GeneratePdf(IssueList); Response.ContentType = "application/pdf"; Response.OutputStream.Write(pdfFile, 0, pdfFile.Length); } }Or by using an httpHandler.
/// <summary> /// Summary description for $codebehindclassname$ /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Handler1 : IHttpHandler { public void ProcessRequest(HttpContext context) {context. Response.ContentType = "application/pdf"; context.OutputStream.Write(pdfFile, 0, pdfFile.Length); } public bool IsReusable { get { return false; } } }Regards,
Helori.
My blog: http://www.notesfor.net/
anandraaz
0 Points
7 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 10:42 AM|LINK
hi thanks for ur reply..
i tried bud its not working..and most importtant thing its not creating pdf file.
regards
AnandRaj
hans_v
All-Star
35986 Points
6550 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 10:50 AM|LINK
What kind of documents are we dealing with?
anandraaz
0 Points
7 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 11:01 AM|LINK
hi ..
We r uploading pdf files and saving as binarydata in database.now i want to create a pdf file with uploaded data(ie saved binary data in database) usinf itextsharp.Plz anyone help me..
regards
AnandRaj
hans_v
All-Star
35986 Points
6550 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 11:36 AM|LINK
In that case, you don't need iTextSharp. Create a generic handler (.ashx file), retrieve the binary data from the database in the ProcessRequest Method and send it to the client.
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest Dim pdfID As Integer = Convert.ToInt32(context.Request.QueryString("pdfID")) Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Const SQL As String = "SELECT [pdfData] FROM [pdfTable] WHERE [pdfID] = @pdfID" Dim myCommand As New SqlCommand(SQL, myConnection) myCommand.Parameters.AddWithValue("@pdfID", pdfID) myConnection.Open() Dim myReader As SqlDataReader = myCommand.ExecuteReader If myReader.Read Then context.Response.ContentType = "application/pdf" context.Response.BinaryWrite(myReader("pdfData")) context.Response.AddHeader("Content-Disposition", "attachment; filename=download.pdf" End If myReader.Close() myConnection.Close() End Using End SubTo retrieve the pdf, call the ashx file like:
<asp:HyperLink ID="HyperLink1" runat="server" navigetUrl="~/pdf.ashx?pdfID=1" Text="Download" />
anandraaz
0 Points
7 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 12:31 PM|LINK
hi,
Thanks for ur reply..to display binary data in pdf file is not a problem.see my requirement is to restrict the print option
of a pdffile .so what im trying to do is creating a new pdf file with no menu bar and will display in a iframe.creating pdf file with hiding menu bar and file tool bar is possible with iTextSahrp.The problem is creating pdffile with binarydata stored in database.
Any suggestions and help ll be appriciated...
regards
Anandraj
Md Eqbal Ans...
Member
186 Points
93 Posts
Re: Creating Pdf file through binarydata in asp.net
Jul 28, 2009 12:47 PM|LINK
public bool CreatePDF(string path)
{
bool bRet = false;
Document document = new Document();
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
document.Open();
//document.AddAuthor("author name");
document.AddCreationDate();
iTextSharp.text.Table aTable = new iTextSharp.text.Table(3);
aTable.BorderWidth = 0;
aTable.Cellspacing = 0;
aTable.Cellpadding = 1;
aTable.Width = 100;
aTable.TableFitsPage = true;
Chunk Heading = new Chunk("Data value to be print on the PDF file");
Heading.Font.SetStyle(Font.BOLD);
Heading.Font.Size = 12;
Heading.Font.SetStyle(Font.UNDERLINE);
Cell tblTop = new Cell(Heading);
tblTop.BorderWidth = 0;
tblTop.Colspan = 3;
tblTop.HorizontalAlignment = 1;
aTable.AddCell(tblTop);
Chunk Detail = new Chunk("Data value to be print on the PDF file");
Detail.Font.Size = 10;
Detail.Font.SetStyle(Font.BOLD);
Cell tblHeader = new Cell(Detail);
tblHeader.BorderWidth = 0;
tblHeader.Colspan = 3;
tblHeader.HorizontalAlignment = 1;
aTable.AddCell(tblHeader);
// similarly add cells as much you want.........
document.Add(aTable);
bRet = true;
}
catch (DocumentException de)
{
Response.Write(de.Message);
}
catch (IOException ioe)
{
Response.Write(ioe.Message);
}
document.Close();
//if (bRet)
// Response.Write(path + " has been created");
return bRet;
}
try this..may be help you to generate pdf file.
Thanks & Best Regards,
Eqbal.
Please mark the post as answer, if you find this useful.