1

I have problem on creating .pdf file with the Page settings as Envelope ,landscape format.

Here is my Code to convert the asp page into pdf in Itextsharp

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Receipt.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
bind_Data();
this.Page.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);
//here i need to set Pagesize as Envelope.
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

i googled it but i couldn't find Envelope size.How do i set the page size as Envelope ,Landscape in dynamically

Thanks In Advance

Bruno Lowagie
  • 73,590
  • 10
  • 107
  • 159
King_Fisher
  • 1,079
  • 5
  • 18
  • 49

2 Answers2

2

You are creating an A4 document in landscape format with this line:

Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 100f, 0f);

If you want to create a document in envelope format, you shouldn't create an A4 page, instead you should do this:

Document pdfDoc = new Document(envelope, 10f, 10f, 100f, 0f);

In this line, envelope is an object of type Rectangle.

There is no such thing as the envelope size. There are different envelope sizes to choose from: http://www.paper-papers.com/envelope-size-chart.html

For instance, if you want to create a page with the size of a 6-1/4 Commercial Envelope, then you need to create a rectangle that measures 6 by 3.5 inch. The measurement system in PDF doesn't use inches, but user units. By default, 1 user unit = 1 point, and 1 inch = 72 points.

Hence you'd define the envelope variable like this:

Rectangle envelope = new Rectangle(432, 252);

Because:

6 inch x 72 points = 432 points (the width)
3.5 inch x 72 points = 252 points (the height)

If you want a different envelope type, you have to do the calculations of the dimensions of that envelope format.

rossfrank
  • 97
  • 1
  • 1
  • 6
Bruno Lowagie
  • 73,590
  • 10
  • 107
  • 159
  • Thank you so much sir,its Clear ,is it possible to add the border align size Border :Left 0.75,right 0.75,Top 0.16,Bottom 0.236 in Internet Explorer. it doesn't accept Decimal values Respected to x1,y1,... Guide me right way sir – King_Fisher Sep 17 '14 at 10:41
  • That is a **totally different** question. You should not hijack the comments of your own question (to which a correct answer was given, unfortunately, you didn't *accept* that correct answer) to ask a *different* question. I will not answer this in a comment section. – Bruno Lowagie Sep 17 '14 at 10:52
  • im new to this sir .i accept its a best one.can you tell me whats meaning for this line? Document pdfDoc = new Document(envelope, 10f, 10f, 100f, 0f); what is 10f,10f,100f,0f ? is this border? – King_Fisher Sep 17 '14 at 11:02
  • Those values are margins. They define the area where iText will not print anything when you use `Document.add()` – Bruno Lowagie Sep 18 '14 at 07:25
0

I want to generate multiple pdf by using this code but it is generating only one PDF ... then going to catch block, Please give solution of this problem.

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + begin + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
dotnetom
  • 23,993
  • 9
  • 50
  • 53