locked
Pdf not get aligned when using itextsharp RRS feed

  • Question

  • User1259588416 posted

    I m using itextsharp to generate from html data  which is stored in table .But i m not getting proper alignment .margin 

    can any one help 

    Tuesday, September 23, 2014 1:00 AM

All replies

  • User-2077626789 posted

    hi,

    follow this simple example.

    http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

    may this help you.

    Tuesday, September 23, 2014 1:33 AM
  • User1259588416 posted

    Thanx for reply  but i m not using any table . simply i m taking htlm data from sql table then converting it into pdf 

    private void HtmlToPdfConvert()
    {
    try
    {
    string ClientID;
    string AssociateID;
    string DocumentType;
    //styles.LoadTagStyle(HtmlTags.H1, HtmlTags.FONTSIZE, "16");
    //styles.LoadTagStyle(HtmlTags.P, HtmlTags.FONTSIZE, "20");
    //styles.LoadTagStyle(HtmlTags.P, HtmlTags.COLOR, "Green");
    // styles.LoadTagStyle(HtmlTags.DIV, HtmlTags.COLOR, "Green");
    string file1 = ConfigurationManager.AppSettings["test1"].ToString();
    HtmltoPdfBusinessLogic htmlpdf = new HtmltoPdfBusinessLogic();
    ds = htmlpdf.GetContent();
    if (ds != null)
    {
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    file2 = file1;
    label2.Text = ds.Tables[0].Rows[i]["PdfFileName"].ToString();
    string fname = label2.Text.ToString();
    string[] words = fname.Split('_');
    for (int j = 0; j < words.Length - 2; j++)
    {
    ClientID = words[0];
    file2 = file2 + ClientID;
    AssociateID = words[1];
    file2 = file2 + "\\" + AssociateID;
    DocumentType = words[2];
    file2 = file2 + "\\" + DocumentType;
    }
    //Directory.CreateDirectory(file2);
    pdffname = file2 + "\\" + label2.Text + ".pdf";
    if (System.IO.Directory.Exists(file2))
    {
    string[] files = System.IO.Directory.GetFiles(file2);
    if (files.Length > 0)
    {
    foreach (string file in files)
    File.Delete(file);
    ConvertHtmlToPdf(i);
    }
    else
    {
    ConvertHtmlToPdf(i);
    }
    }
    else
    {
    ConvertHtmlToPdf(i);
    }

    }
    this.Close();

    }

    protected void ConvertHtmlToPdf(int i)
    {
    try
    {


    bool IsPageBreakExists = false;
    System.IO.Directory.CreateDirectory((file2));
    outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), pdffname);
    label1.Text = ds.Tables[0].Rows[i]["TM_Content"].ToString();
    string htmltxt = label1.Text.ToString() + imgpath;
    // HtmlStream = label1.Text;
    document = new Document(PageSize.A4, 15, 10, 15, 10);
    FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None);
    PdfWriter w = PdfWriter.GetInstance(document, fs);
    document.Open();
    List<IElement> objects = HTMLWorker.ParseToList(new StringReader(htmltxt), styles);
    foreach (IElement element in objects)
    {
    if (element.Chunks.Count == 0)
    {
    document.Add(Chunk.NEWLINE);
    }
    foreach (Chunk chun in element.Chunks)
    {
    IsPageBreakExists = false;
    if (chun.Content.Contains("--------------------------PageBreak---------------------------------------"))
    {
    chun.Content.Replace("--------------------------PageBreak---------------------------------------", " ");
    document.NewPage();
    IsPageBreakExists = true;
    break;
    }

    }
    if (!IsPageBreakExists)
    document.Add(element);

    }
    document.Close();
    file2 = string.Empty;
    this.Close();
    }
    catch (Exception exp)
    {
    MessageBox.Show(exp.Message);
    }

    }

    }
    catch (Exception exp)
    {
    MessageBox.Show(exp.Message);
    }
    }

    hope this will give u some clear idea 

    Tuesday, September 23, 2014 3:36 AM