locked
pdf convertion problem with itexthsarp RRS feed

  • Question

  • User-536335358 posted

    i m rendering html content of the panel to the pdf with help of itextsharp but i dnt know how to control pdf size and the pdf format is not suitable for print and also By default page zoom is 138% but i need 100%. Can any one help me

    my code is here

       Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=name.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                pnlPrint.RenderControl(hw);
                pnlPrint.Style.Add("align", "center");
    
                iTextSharp.text.Image.ALIGN_CENTER.ToString();
               
               // Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            
    
    
                StringReader sr = new StringReader(sw.ToString());
               
    
    
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                Response.Write(pdfDoc);
                Response.End();



    Friday, November 2, 2012 3:26 AM

All replies

  • User1786833724 posted
    document.SetPageSize(reader.GetPageSizeWithRotation(1)); might help, probably ? http://www.codeproject.com/Articles/470190/PDF-Merger-and-Protector

    http://www.codeproject.com/Articles/81118/ITextSharp-Helper-Class

    Private Coordinates GetPageAlignment(string PageNumberText, Document document)
       {
    Coordinates Coordinates = new PDFLibrary.Coordinates();
    try
    {
    frmSettings frmsetting = new frmSettings();
    PageNumberSize PageNumberSize = frmsetting.GetPageNumberSize(PageNumberText);
     
    float TMargin = document.TopMargin;
    float RMargin = document.RightMargin;
    float BMargin = document.BottomMargin;
    float LMargin = document.LeftMargin;
     
    setting = new SettingManager(Path.Combine(Application.StartupPath, "settings.ini"));
    iTextSharp.text.Rectangle pageSize = document.PageSize;
     
    string value = setting.Read("Page Number and Formatting", "VerticalAlignment");
    switch (value)
    {
    case "top":
    Coordinates.Y = pageSize.Height - (PageNumberSize.Height + TMargin);
    break;
    case "middle":
    Coordinates.Y = (pageSize.Height - (PageNumberSize.Height + TMargin)) / 2;
    break;
    case "bottom":
    Coordinates.Y = BMargin - PageNumberSize.Height;
    break;
    }
    value = setting.Read("Page Number and Formatting", "HorizontalAlignment");
    switch (value)
    {
    case "left":
    Coordinates.X = LMargin;
    break;
    case "center":
    Coordinates.X = (pageSize.Width - (PageNumberSize.Width + RMargin)) / 2;
    break;
    case "right":
    Coordinates.X = pageSize.Width - (PageNumberSize.Width + RMargin);
    break;
    }
     
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    return Coordinates;
    }



    Friday, November 2, 2012 3:36 AM
  • User-536335358 posted

    Thanks for reply but Nothing happens with your code..i am using 5.0 version

    Friday, November 2, 2012 4:28 AM