Itudk_2010
asked on
Printing PDF document using C#
Hi all,
I have an aspx page whcih contains some lable controls with values. I would like to print those values in a table in PDF. I have added the iTextSharp PDF dll but I am not sure how to print the following StringBuilder object when a user clicks on the print button:
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
StringBuilder strHTML = new StringBuilder();
strHTML.Append("<table border=1>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Car Size: </b>" + lblCarSize.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Car Make: </b>" + lblCarMake.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Car Model: </b>" + lblCarModel.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Other Details: </b>" + lblOtherDetails.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Pink-up Time: </b>" + lblPickupTime.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Drop-off Time: </b>" + lblDropoffTime.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Pick-up Location: </b>" + lblPickupLocation.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Drop-off Location: </b>" + lblDropoffLocation.Text + " " + "</td>");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Pick-up Date: </b>" + lblPickupDate.Text + "</td>" + "<br />");
strHTML.Append("<td>" + "<b> Drop-off Date: </b>" + lblDropoffDate.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Total Charges: </b>" + lblTotalCharges.Text + " DKK " + "</td>");
strHTML.Append("</tr>");
strHTML.Append("</table>") ;
//Generating pdf
Document document = new Document();
PdfWriter.GetInstance(docu ment, new FileStream(@"C:\Temp\test. pdf", FileMode.Create));
document.Open();
}
Any ideas?
I have an aspx page whcih contains some lable controls with values. I would like to print those values in a table in PDF. I have added the iTextSharp PDF dll but I am not sure how to print the following StringBuilder object when a user clicks on the print button:
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
StringBuilder strHTML = new StringBuilder();
strHTML.Append("<table border=1>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Car Size: </b>" + lblCarSize.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Car Make: </b>" + lblCarMake.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Car Model: </b>" + lblCarModel.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Other Details: </b>" + lblOtherDetails.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Pink-up Time: </b>" + lblPickupTime.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Drop-off Time: </b>" + lblDropoffTime.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Pick-up Location: </b>" + lblPickupLocation.Text + " " + "</td>");
strHTML.Append("<td>" + "<b> Drop-off Location: </b>" + lblDropoffLocation.Text + " " + "</td>");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Pick-up Date: </b>" + lblPickupDate.Text + "</td>" + "<br />");
strHTML.Append("<td>" + "<b> Drop-off Date: </b>" + lblDropoffDate.Text + "</td>" + "<br />");
strHTML.Append("</tr>");
strHTML.Append("<tr>");
strHTML.Append("<td>" + "<b> Total Charges: </b>" + lblTotalCharges.Text + " DKK " + "</td>");
strHTML.Append("</tr>");
strHTML.Append("</table>")
//Generating pdf
Document document = new Document();
PdfWriter.GetInstance(docu
document.Open();
}
Any ideas?
ASKER
Hi,
That link does not help, any other ideas?
That link does not help, any other ideas?
the attached code will open the file in pdf reader and you can print using the pdf reader.
or create a .bat file to execute AcroRd32.exe /P c:/yourfilename.pdf
or create a .bat file to execute AcroRd32.exe /P c:/yourfilename.pdf
MemoryStream MStream = new MemoryStream();
iTextSharp.text.Document doc = new iTextSharp.text.Document();
PdfWriter writer = PdfWriter.GetInstance(doc, MStream);
doc.Open();
doc.Add(new Paragraph("Hello World"));
PdfContentByte cb = writer.DirectContent;
Barcode128 barcode = new Barcode128();
barcode.CodeType = Barcode128.CODE_C;
barcode.Code = "1234567890";
iTextSharp.text.Image img = barcode.CreateImageWithBarcode(cb, null, null);
doc.Add(new Phrase(new Chunk(img, 100, 0)));
doc.Close();
Response.ContentType ="application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(MStream.GetBuffer(), 0, MStream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
ASKER
Hi RonCraig_101,
Thanks for the code example. I can print a single string but how to print multiple stirngs or multiple lable control values. Your following code can print one line:
doc.Add(new Paragraph("Hello World"));
but how about multiple lines print inside a table?
Thanks for your help. Looking forward to your reply.
Thanks for the code example. I can print a single string but how to print multiple stirngs or multiple lable control values. Your following code can print one line:
doc.Add(new Paragraph("Hello World"));
but how about multiple lines print inside a table?
Thanks for your help. Looking forward to your reply.
try this
MemoryStream MStream = new MemoryStream();
iTextSharp.text.Document doc = new iTextSharp.text.Document();
PdfWriter writer = PdfWriter.GetInstance(doc, MStream);
StringBuilder strHTML = new StringBuilder();
doc.Open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
doc.Add(table);
doc.Add(table);
doc.Close();
Response.ContentType ="application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(MStream.GetBuffer(), 0, MStream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
ASKER
Thanks, but where can I add this:
StringBuilder strHTML = new StringBuilder();
I am not sure how and where to add the strHTML so all the values of the table will be shown in the pdf document?
any ideas?
Thanks again for your help.
StringBuilder strHTML = new StringBuilder();
I am not sure how and where to add the strHTML so all the values of the table will be shown in the pdf document?
any ideas?
Thanks again for your help.
I would forget about the stringbuilder just do this...
string value = "Car Make: " + lblCarMake.Text ;
table.AddCell(value);
but if you want to use it then you will have to cast the string as a table.
string testString = @"<?xml version=""1.0"" encoding=""UTF-8"">
<root><child><subchild>... ..</subchi ld></child ></root>";
DataSet ds = new DataSet();
StringReader stream = new StringReader(testString);
ds.ReadXml(stream);
DataTable dt = ds.Tables[0];
then do something like this to convert that to a pdfTable
http://www.dotnetspark.com/kb/1365-create-custom-table-pdf-document-using-itextsharp.aspx
string value = "Car Make: " + lblCarMake.Text ;
table.AddCell(value);
but if you want to use it then you will have to cast the string as a table.
string testString = @"<?xml version=""1.0"" encoding=""UTF-8"">
<root><child><subchild>...
DataSet ds = new DataSet();
StringReader stream = new StringReader(testString);
ds.ReadXml(stream);
DataTable dt = ds.Tables[0];
then do something like this to convert that to a pdfTable
http://www.dotnetspark.com/kb/1365-create-custom-table-pdf-document-using-itextsharp.aspx
ASKER
RonCraig_101,
I really appreciate your help. But this way it seems quite complicated. Is there any easiest way to implement that?
I will look on the link you provided.
Thanks.
I really appreciate your help. But this way it seems quite complicated. Is there any easiest way to implement that?
I will look on the link you provided.
Thanks.
ASKER
Hi RonCraig_101,
I did the following changes in your code and it works fine except the last lines are not show in the pdf table while printing. The lines which are not shown are values of string value10 and string value11 as shown below:
string value10 = "Other Details: " + otherCarDetails;
table.AddCell(value10);
string value11 = "Total Charges: " + totalCharges;
table.AddCell(value11);
The rest is fine--
MemoryStream MStream = new MemoryStream();
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, MStream);
//StringBuilder strHTML = new StringBuilder();
doc.Open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Your Booking Details!"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
string value = "Car Size: " + carSize;
table.AddCell(value);
string value2 = "Car Make: " + carMake;
table.AddCell(value2);
string value3 = "Car Model: " + carModel;
table.AddCell(value3);
string value4 = "Pick-up Time: " + pickupTime;
table.AddCell(value4);
string value5 = "Drop-off Time: " + dropOffTime;
table.AddCell(value5);
string value6 = "Pick-up Location: " + PickUpLocation;
table.AddCell(value6);
string value7 = "Pick-up Date: " + PickUpDate;
table.AddCell(value7);
string value8 = "Drop-off Location: " + DropOffLocation;
table.AddCell(value8);
string value9 = "Drop-off Date: " + DropOffDate;
table.AddCell(value9);
string value10 = "Other Details: " + otherCarDetails;
table.AddCell(value10);
string value11 = "Total Charges: " + totalCharges;
table.AddCell(value11);
doc.Add(table);
doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("conten t-disposit ion", "attachment;filename=file. pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Writ e(MStream. GetBuffer( ), 0, MStream.GetBuffer().Length );
Response.OutputStream.Flus h();
Response.End();
I did the following changes in your code and it works fine except the last lines are not show in the pdf table while printing. The lines which are not shown are values of string value10 and string value11 as shown below:
string value10 = "Other Details: " + otherCarDetails;
table.AddCell(value10);
string value11 = "Total Charges: " + totalCharges;
table.AddCell(value11);
The rest is fine--
MemoryStream MStream = new MemoryStream();
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc,
//StringBuilder strHTML = new StringBuilder();
doc.Open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("Your Booking Details!"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
string value = "Car Size: " + carSize;
table.AddCell(value);
string value2 = "Car Make: " + carMake;
table.AddCell(value2);
string value3 = "Car Model: " + carModel;
table.AddCell(value3);
string value4 = "Pick-up Time: " + pickupTime;
table.AddCell(value4);
string value5 = "Drop-off Time: " + dropOffTime;
table.AddCell(value5);
string value6 = "Pick-up Location: " + PickUpLocation;
table.AddCell(value6);
string value7 = "Pick-up Date: " + PickUpDate;
table.AddCell(value7);
string value8 = "Drop-off Location: " + DropOffLocation;
table.AddCell(value8);
string value9 = "Drop-off Date: " + DropOffDate;
table.AddCell(value9);
string value10 = "Other Details: " + otherCarDetails;
table.AddCell(value10);
string value11 = "Total Charges: " + totalCharges;
table.AddCell(value11);
doc.Add(table);
doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("conten
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Writ
Response.OutputStream.Flus
Response.End();
Replace this line
PdfPTable table = new PdfPTable(1);
Add these two lines
int[] widths = {100};
table.SetWidths(widths);
PdfPTable table = new PdfPTable(1);
Add these two lines
int[] widths = {100};
table.SetWidths(widths);
complete code
MemoryStream MStream = new MemoryStream();
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.LETTER, 72, 72, 72, 72);
PdfWriter writer = PdfWriter.GetInstance(doc, MStream);
doc.Open();
PdfPTable table = new PdfPTable(1);
int[] widths = { 100 };
table.SetWidths(widths);
PdfPCell cell = new PdfPCell();
string value = "Car Size: " + "carSize";
table.AddCell(value);
string value2 = "Car Make: " + "carMake";
table.AddCell(value2);
string value3 = "Car Model: " + "carModel";
table.AddCell(value3);
string value4 = "Pick-up Time: " + "pickupTime";
table.AddCell(value4);
string value5 = "Drop-off Time: " + "dropOffTime";
table.AddCell(value5);
string value6 = "Pick-up Location: " + "PickUpLocation";
table.AddCell(value6);
string value7 = "Pick-up Date: " + "PickUpDate";
table.AddCell(value7);
string value8 = "Drop-off Location: " + "DropOffLocation";
table.AddCell(value8);
string value9 = "Drop-off Date: " + "DropOffDate";
table.AddCell(value9);
string value10 = "Other Details: " + "otherCarDetails";
table.AddCell(value10);
string value11 = "Total Charges: " + "totalCharges";
table.AddCell(value11);
string value12 = "Total Charges: " + "totalCharges";
table.AddCell(value12);
table.AddCell(value12);
table.AddCell(value12);
table.AddCell(value12);
doc.Add(table);
doc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(MStream.GetBuffer(), 0, MStream.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The following lines of code work fine but it does not add any columns in the table. It displays the records in only rows so you don't see any columns in the table:
PdfPTable table = new PdfPTable(1);
int[] widths = { 100 };
table.SetWidths(widths);
Any other ideas?
I will also try to look into it in more detail.
PdfPTable table = new PdfPTable(1);
int[] widths = { 100 };
table.SetWidths(widths);
Any other ideas?
I will also try to look into it in more detail.
you have all the basic concepts... you just need to read more on how to format the pdftable to your liking...
PdfPTable table = new PdfPTable(1); will change the number of columns you have .... just make sure you use the corect number of cells per row...
if you want blank cells just add them..
PdfPTable table = new PdfPTable(1); will change the number of columns you have .... just make sure you use the corect number of cells per row...
if you want blank cells just add them..
ASKER
Ok, I will try.
Thanks a lot.
Thanks a lot.
could you please make as answerd please.
Thanks you .
Thanks you .
ASKER
Ok, but it is not 100% correct?
ASKER
solved
http://www.daniweb.com/forums/thread34107.html