Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# PDF
I have a code bellow:
the first i using itexsharp library:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
 
the second i have code c#:
 
public partial class _Default : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
 

        }
    }
 
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        String htmlText = "<font  " +
         " color=\"#0000FF\"><b><i>Title One</i></b></font><font   " +
         " color=\"black\"><br><br>Some text here<br><br><br><font   " +
         " color=\"#0000FF\"><b><i>Another title here   " +
         " </i></b></font><font   " +
         " color=\"black\"><br><br>Text1<br>Text2<br><OL><LI><DIV Style='color:green'>Pham Duy Hoa</DIV></LI><LI>how are u</LI></OL><br/>"+
         "<table border='1'><tr><td style='color:red;text-align:right;width:20%'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>ASFAFA</td></tr><tr><td style='color:red;text-align:right'>123456</td><td style='color:green;width:60%'>78910</td><td style='color:red;width:20%'>DAFSDGAFW</td></tr></table><br/>"+
         "<div><ol><li>123456</li><li>123456</li><li>123456</li><li>123456</li></ol></div>";
 

 
        HTMLToPdf(htmlText, "PDFfile.pdf");
    }
 
    public void HTMLToPdf(string HTML, string FilePath)
    {
        Document document = new Document();
 
        PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Chap0101.pdf", FileMode.Create));
        document.Open();
        Image pdfImage = Image.GetInstance(Server.MapPath("logo.png"));
 
        pdfImage.ScaleToFit(100, 50);
 
        pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);
 
        document.Add(pdfImage);
        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(HTML));
        document.Close();
        ShowPdf("Chap0101.pdf");
    }
    private void ShowPdf(string s)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "inline;filename=" + s);
        Response.ContentType = "application/pdf";
        Response.WriteFile(s);
        Response.Flush();
        Response.Clear();
    }
}
 
but i don't insert image into string htmltext.
please help me.
thanks
Posted 29-May-11 19:31pm
Edited 29-May-11 20:55pm
v2
Comments
Sunasara Imdadhusen at 30-May-11 1:56am
   
Added PRE tag.
Mausam Bharati at 9-Jul-13 7:48am
   
What will be Chap0101.pdf in case of table from database generated through a dll?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
 Dim jpeg As iTextSharp.text.Image
 Dim _Chunk As Chunk
jpeg = iTextSharp.text.Image.GetInstance(imagepath)
_Chunk = New Chunk(jpeg, 0, 0)
document.Add(_Chunk);
 
This will works!!!
 
Thanks,
  Permalink  
Comments
hoa20101988 at 30-May-11 21:37pm
   
Thanks, but i want to insert image in HTml of code c# and change size image.
sharad_sharma82 at 19-Jan-12 2:25am
   
Hi if you can to add the image file from code file the use the below code and it will work fine for me :
1.apply the [img] that refer image tag in desgin part where you want to show the image
<td valign="top" style="text-align: right">
[Img]
</td>
2.replace this [img] with image tag
string imgpath = "<img src='' style='width:50;height:50;padding-left-100px;' runat="server" id='ig' alt='' />";
contents = contents.Replace("[img]", imgpath .ToString());
//contents = contents.Replace("[ORDERDATE]", DateTime.Now.ToShortDateString());
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
 
// Enumerate the elements, adding each one to the Document...
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
mayur mkt at 27-Sep-12 6:09am
   
can any one of you tell me..how to insert color or how can i get css effects which i have applied in my asp.net page..i am using code in asp.net
Poojit at 24-Apr-14 3:26am
   
Hi All,
 
I am encountring same issue with image.
Please help.

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 30 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100