Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void pdf()
{
    iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
    PdfPTable table = new PdfPTable(3);
    table.TotalWidth = 400f;
    Document document = new Document(PageSize.A2, 88f, 88f, 10f, 10f);

    MemoryStream memoryStream = new MemoryStream();

    PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
    document.Open();
    PdfContentByte content = writer.DirectContent;
    iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(document.PageSize);
    rectangle.Left += document.LeftMargin;
    rectangle.Right -= document.RightMargin;
    rectangle.Top -= document.TopMargin;
    rectangle.Bottom += document.BottomMargin;

    content.SetColorStroke(iTextSharp.text.BaseColor.BLUE);
    content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
    content.Stroke();            

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Invoice.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

    divM.RenderControl(htmlTextWriter);
    StringReader sr = new StringReader(stringWriter.ToString());
  //  Document doc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

    HTMLWorker htmlparser = new HTMLWorker(document);
    htmlparser.SetStyleSheet(styles);
    PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();
    htmlparser.Parse(sr);
    document.Close();
    Response.Write(document);
    Response.End();
}


Here i am not getting any borders in pdf page..and i tried another way.
C#
styles.LoadStyle("pdf", "border", "1");

If i applied this line,i got every line with border
C#
styles.LoadStyle("pdf", "border-top", "1");
   styles.LoadStyle("pdf", "border-bottom", "1");
   styles.LoadStyle("pdf", "border-left", "1");
   styles.LoadStyle("pdf", "border-right", "1");
   styles.LoadStyle("pdf", "size", "4");

If i applied this code ,then i am not getting any border .. How can i add page borders in pdf page using Itextsharp.dll file
Posted
Updated 10-Sep-14 20:28pm
v3
Comments
kbrandwijk 10-Sep-14 9:16am    
I think LoadStyle overrides the previous LoadStyle, look into LoadTagStyle. You can apply that multiple times to the same object (pdf in this case). However, using LoadStyle like this is deprecated by now. You should consider moving to XMLWorker (http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html)

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900