my code stock.com

Ejemplo de vista.

Snippet options

Download: Download snippet as itextsharp.c.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

@{
    Document Document = ViewBag.Document;

    Document.SetPageSize(PageSize.A4);

    PdfPTable table = new PdfPTable(1);
    table.WidthPercentage = 100;
    
    PdfPCell cell01 = new PdfPCell();
    var parr = new Phrase("Demo: Crear un documento PDF", new Font(Font.FontFamily.HELVETICA, 16, Font.NORMAL, BaseColor.WHITE));    
    cell01.AddElement(parr);
    
    cell01.BackgroundColor = new BaseColor(147, 201, 255);
    cell01.FixedHeight = 200;
    cell01.NoWrap = true;
    
    table.AddCell(cell01);

    Document.Add(table);

    table = new PdfPTable(2);
    table.WidthPercentage = 100;
    
    Image img1 = Image.GetInstance(HttpContext.Current.Server.MapPath("../Content/images/01.jpg"));
    cell01 = new PdfPCell();
    cell01.AddElement(img1);    
    table.AddCell(cell01);

    img1 = Image.GetInstance(HttpContext.Current.Server.MapPath("../Content/images/02.jpg"));
    cell01 = new PdfPCell();
    cell01.AddElement(img1);
    table.AddCell(cell01);
    
    
    Document.Add(table);
    
    StringBuilder sb = new StringBuilder();
    sb.Append("<p>Nam eget quam condimentum risus consectetur consectetur. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer sollicitudin metus eget dui ultrices mollis. <b>Pellentesque venenatis</b>, libero sed commodo rutrum, lorem urna interdum ante, id ultrices sem est id orci. Donec elementum dolor id nunc feugiat vulputate. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque a egestas magna. Suspendisse venenatis gravida elementum. Etiam ullamcorper imperdiet leo. Sed facilisis urna eu massa consequat ullamcorper. Donec sodales metus sed nisl rhoncus consequat. Ut et est non justo elementum adipiscing pretium nec nibh <p> <br/> <br/>");

    sb.Append("<div>");
    
    sb.Append("<b> Neque porro quisquam est qui dolorem</b><br/> ");
    
    sb.Append(" <ul> <li>00001</li> <li>00002</li> </ul> ");

    sb.Append("</div> <br/>");
    
    sb.Append("<p>Integer commodo adipiscing massa ut imperdiet. Mauris eleifend augue a mauris volutpat sollicitudin. Nam varius tempus est, in luctus sapien accumsan vestibulum. Maecenas feugiat blandit dolor vel aliquet. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla facilisi. Vivamus nec justo est, nec ultrices ipsum. Vivamus interdum, orci quis placerat pellentesque, lacus neque mattis dolor, ac pulvinar arcu ipsum id ipsum. Ut suscipit lacinia erat ac porttitor. Curabitur pulvinar vehicula felis nec interdum. Aenean ut velit et nisl pretium laoreet. Sed at velit vitae neque commodo euismod ac vitae orci. Morbi sed nibh nunc. Aliquam mollis pharetra urna in condimentum. Suspendisse viverra dapibus tempor.</p>");

    sb.Append("<div>");

    sb.Append("<b> Neque porro quisquam est qui dolorem</b><br/> ");

    sb.Append(" <ul> <li>00001</li> <li>00002</li> </ul> ");

    sb.Append("</div> <br/>");

    sb.Append("<p>Nam eget quam condimentum risus consectetur consectetur. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Integer sollicitudin metus eget dui ultrices mollis. <b>Pellentesque venenatis</b>, libero sed commodo rutrum, lorem urna interdum ante, id ultrices sem est id orci. Donec elementum dolor id nunc feugiat vulputate. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque a egestas magna. Suspendisse venenatis gravida elementum. Etiam ullamcorper imperdiet leo. Sed facilisis urna eu massa consequat ullamcorper. Donec sodales metus sed nisl rhoncus consequat. Ut et est non justo elementum adipiscing pretium nec nibh <p> <br/> <br/>");
    
    var fontFac = FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLUE);
    StringReader str = new StringReader(sb.ToString());
    foreach (IElement element in HTMLWorker.ParseToList(str, new StyleSheet()))
    {
        if (element is Paragraph)
        {
            Paragraph p = new Paragraph();
            p.Font.Color = BaseColor.BLUE;
            for (int i = 0; i < ((Paragraph)element).Count; i++)
            {
                p.Add(((Paragraph) element)[i]);
            }
            Document.Add(p);          
        }
        else if(element is List)
        {
            List newList = new List();
            newList.IndentationLeft = 30f;
            
            List list = element as List;
            
            foreach (IElement item in list.Items)
            {
                if (item is ListItem)
                {

                    ListItem l = new ListItem(new Chunk(((ListItem)item).Chunks[0].Content, fontFac));
                    newList.Add(l);
                }
            }

            Document.Add(newList);
        }
        else
        {
            Document.Add(element);
        }
    }
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.