Ibrahim shaikh
Ibrahim shaikh

Reputation: 343

problem with table format while downloading pdf in iTextSharp in asp.net mvc

I am using iTextsharp 5.5 to download my data as pdf in which I worked with the following Link example, below is the pdf which is downloaded

Image

In the above Image (#800000) background represent my headers, as you can see my image above the headers are not aligned properly

below is my code for the same

public FileResult DownloadInventoryDumpReportPdf(int AccountId, int UserId, int ItemStatusId)
{
    MemoryStream workStream = new MemoryStream();
    StringBuilder status = new StringBuilder("");
    DateTime dTime = DateTime.Now;

    DocPro.RMS.BusinessLayer.Reports.IInventoryDumpReport a = (DocPro.RMS.BusinessLayer.Reports.IInventoryDumpReport)DALFinder.GetInstance(typeof(DocPro.RMS.BusinessLayer.Reports.IInventoryDumpReport));
    var result = a.Get(AccountId, UserId, ItemStatusId);

    BusinessLayer.IMaster.IRefFieldSetting b = (BusinessLayer.IMaster.IRefFieldSetting)DALFinder.GetInstance(typeof(BusinessLayer.IMaster.IRefFieldSetting));
    var headers = b.GetRefField(UserId);

    //file name to be created   
    string strPDFFileName = string.Format("SamplePdf" + dTime.ToString("yyyyMMdd") + "-" + ".pdf");
    Document doc = new Document();
    doc.SetMargins(0f, 0f, 0f, 0f);
    //Create PDF Table with 5 columns  
    PdfPTable tableLayout = new PdfPTable(21);
    doc.SetMargins(0f, 0f, 0f, 0f);
    //Create PDF Table  

    //file will created in this path  
    string strAttachment = Server.MapPath("~/Downloadss/" + strPDFFileName);


    PdfWriter.GetInstance(doc, workStream).CloseStream = false;
    doc.Open();

    //Add Content to PDF   
    doc.Add(AddContentToPDF(tableLayout, headers, result));

    // Closing the document  
    doc.Close();

    byte[] byteInfo = workStream.ToArray();
    workStream.Write(byteInfo, 0, byteInfo.Length);
    workStream.Position = 0;


    return File(workStream, "application/pdf", strPDFFileName);

}

protected PdfPTable AddContentToPDF(PdfPTable tableLayout, BusinessEntities.Master.REFField header, List<BusinessEntities.Reports.InventoryDumpReport> result)
{
    tableLayout.AddCell(new PdfPCell(new Phrase("Creating Pdf using ItextSharp", new Font(Font.FontFamily.HELVETICA, 8, 1, new iTextSharp.text.BaseColor(0, 0, 0))))
    {
        Colspan = 12,
        Border = 0,
        PaddingBottom = 5,
        HorizontalAlignment = Element.ALIGN_CENTER
    });

    ////Add header  
    AddCellToHeader(tableLayout, "SrNo");
    AddCellToHeader(tableLayout, "Item Code");
    AddCellToHeader(tableLayout, "Item Description");
    AddCellToHeader(tableLayout, "Alternate Code");
    AddCellToHeader(tableLayout, "Object Name");

    AddCellToHeader(tableLayout, "Location Code");
    AddCellToHeader(tableLayout, "Container Code");
    AddCellToHeader(tableLayout, "Shipper Code");
    AddCellToHeader(tableLayout, "Status");
    AddCellToHeader(tableLayout, "New Status DateTime");

    AddCellToHeader(tableLayout, "Expiry Date");
    AddCellToHeader(tableLayout, "Created By");
    AddCellToHeader(tableLayout, "Created Date");
    AddCellToHeader(tableLayout, "Modified By");
    AddCellToHeader(tableLayout, "Modified Date");

    AddCellToHeader(tableLayout, header.displayName01);
    AddCellToHeader(tableLayout, header.displayName02);
    AddCellToHeader(tableLayout, header.displayName03);
    AddCellToHeader(tableLayout, header.displayName04);
    AddCellToHeader(tableLayout, header.displayName05);
    AddCellToHeader(tableLayout, header.displayName06);

    ////Add body  
    var currentIndex = 1;
    foreach (var d in result)
    {
        var ItemStatusDateTime = d.ItemStatusDateTime == null ? "" : d.ItemStatusDateTime;
        var ExpiryDate = d.ExpiryDate == null ? "" : d.ExpiryDate;
        var CreatedDate = d.CreatedDate == null ? "" : d.CreatedDate;
        var ModifiedDate = d.ModifiedDate == null ? "" : d.ModifiedDate;

        AddCellToBody(tableLayout, currentIndex.ToString());
        AddCellToBody(tableLayout, d.ItemCode);
        AddCellToBody(tableLayout, d.ItemDescription);
        AddCellToBody(tableLayout, d.AlternateCode);
        AddCellToBody(tableLayout, d.ObjectName);

        AddCellToBody(tableLayout, d.LocationCode);
        AddCellToBody(tableLayout, d.ContainerItemCode);
        AddCellToBody(tableLayout, d.ShipperCode);
        AddCellToBody(tableLayout, d.Status);
        AddCellToBody(tableLayout, ItemStatusDateTime);

        AddCellToBody(tableLayout, ExpiryDate);
        AddCellToBody(tableLayout, d.CreatedByUserName);
        AddCellToBody(tableLayout, CreatedDate);
        AddCellToBody(tableLayout, d.ModifiedByUserName);
        AddCellToBody(tableLayout, ModifiedDate);

        AddCellToBody(tableLayout, d.Ref01);
        AddCellToBody(tableLayout, d.Ref02);
        AddCellToBody(tableLayout, d.Ref03);
        AddCellToBody(tableLayout, d.Ref04);
        AddCellToBody(tableLayout, d.Ref05);
        AddCellToBody(tableLayout, d.Ref06);
        currentIndex++;
    }
    return tableLayout;
}

private static void AddCellToHeader(PdfPTable tableLayout, string cellText)
{

    tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
    {
        HorizontalAlignment = Element.ALIGN_LEFT,
        Padding = 5,
        BackgroundColor = new iTextSharp.text.BaseColor(128, 0, 0)
    });
}

// Method to add single cell to the body  
private static void AddCellToBody(PdfPTable tableLayout, string cellText)
{
    tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.FontFamily.HELVETICA, 8, 1, iTextSharp.text.BaseColor.BLACK)))
    {
        HorizontalAlignment = Element.ALIGN_LEFT,
        Padding = 5,
        BackgroundColor = new iTextSharp.text.BaseColor(255, 255, 255)
    });
}

how can I fix this?

Upvotes: 0

Views: 171

Answers (1)

vitalipf
vitalipf

Reputation: 21

I think iText did exactly what you asked for, no? What result do you expect? I suppose something like this:

Expected result?

Just add empty cells where necessary, 4 empty cells on each iteration in your case.

Upvotes: 2

Related Questions