Generate PDF in C # without using Microsoft.Office.Interop [closed]

1

Hello,

I'm having trouble generating pdf without using Microsoft.Office.Interop. Other DLLs I've found are not free.

Does anyone have a tip?

    
asked by anonymous 26.12.2017 / 11:48

2 answers

1

I've already used iText to create PDFs in Java. The version for C # is the iTextSharp that is available in the free version (license AGPL ) and commercial.

Examples of use:

26.12.2017 / 14:06
0

You have the Report.NET library that is also open source.

Here is an example of a class to generate the PDF using the library

public class Report<TClasse> : Report
    where TClasse : class
{
    private readonly IList<TClasse> _elementos;

    private FontDef fontDef_Helvetica;
    private const Double rPosLeft = 20;  // millimeters
    private const Double rPosRight = 195;  // millimeters
    private const Double rPosTop = 24;  // millimeters
    private const Double rPosBottom = 278;  // millimeters

    public Report(IList<TClasse> elementos)
        : base()
    {
        _elementos = elementos;
    }

    public static string Create(IList<TClasse> elementos)
    {
        Report<TClasse> report = new Report<TClasse>(elementos);
        string diretorio = HttpContext.Current.Server.MapPath("~/Arquivos/Reports/" + typeof(TClasse).Name);
        if (!Directory.Exists(diretorio))
            Directory.CreateDirectory(diretorio);

        string arquivo = Path.Combine(diretorio, $"{Guid.NewGuid()}.pdf");
        report.Save(arquivo);

        return arquivo;
    }

    protected override void Create()
    {
        fontDef_Helvetica = new FontDef(this, FontDef.StandardFont.Helvetica);
        FontProp fontProp_Text = new FontPropMM(fontDef_Helvetica, 1.9);  // standard font
        FontProp fontProp_Header = new FontPropMM(fontDef_Helvetica, 1.9);  // font of the table header
        fontProp_Header.bBold = true;

        // create table
        TableLayoutManager tlm;
        using (tlm = new TableLayoutManager(fontProp_Header))
        {
            tlm.rContainerHeightMM = rPosBottom - rPosTop;  // set height of table
            tlm.tlmCellDef_Header.rAlignV = RepObj.rAlignCenter;  // set vertical alignment of all header cells
            tlm.tlmCellDef_Default.penProp_LineBottom = new PenProp(this, 0.05, Color.LightGray);  // set bottom line for all cells
            tlm.tlmHeightMode = TlmHeightMode.AdjustLast;
            tlm.eNewContainer += new TableLayoutManager.NewContainerEventHandler(Tlm_NewContainer);

            MontarTabela(tlm, fontProp_Text);
        }

        // print page number and current date/time
        Double rY = rPosBottom + 1.5;
        foreach (Page page in enum_Page)
        {
            page.AddLT_MM(rPosLeft, rY, new RepString(fontProp_Text, DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString()));
            page.AddRT_MM(rPosRight, rY, new RepString(fontProp_Text, page.iPageNo + " / " + iPageCount));
        }
    }

    public virtual void MontarTabela(TableLayoutManager tlm, FontProp fontProp_Text)
    {
        var propriedades = typeof(TClasse).GetProperties().Where(a => a.GetCustomAttributes(typeof(ColunaRelatorioAttribute), true).Count() > 0)
                .OrderBy(a => ((ColunaRelatorioAttribute)a.GetCustomAttributes(typeof(ColunaRelatorioAttribute), true)[0]).Posicao)
                .ToList();

        foreach (var propriedade in propriedades)
        {
            var colunaRelatoicio = propriedade.GetCustomAttribute<ColunaRelatorioAttribute>(true);
            tlm.AdicionarColuna(colunaRelatoicio.Titulo, colunaRelatoicio.Largura, colunaRelatoicio.TextMode);
        }

        foreach (var elemento in _elementos)
        {
            tlm.NewRow();
            foreach (var propriedade in propriedades)
            {
                var colunaRelatoicio = propriedade.GetCustomAttribute<ColunaRelatorioAttribute>(true);
                if (propriedade.PropertyType == typeof(string) || propriedade.PropertyType == typeof(Guid))
                    tlm.Add(colunaRelatoicio.Posicao, new RepString(fontProp_Text, propriedade.GetValue(elemento).ToString()));
                else if (propriedade.PropertyType == typeof(DateTime))
                    tlm.Add(colunaRelatoicio.Posicao, new RepDateTime(fontProp_Text, DateTime.Parse(propriedade.GetValue(elemento).ToString())));
            }
        }
    }

    public void Tlm_NewContainer(Object oSender, TableLayoutManager.NewContainerEventArgs ea)
    {  // only "public" for NDoc, should be "private"
        new Page(this);

        // first page with caption
        if (page_Cur.iPageNo == 1)
        {
            FontProp fontProp_Title = new FontPropMM(fontDef_Helvetica, 7);
            fontProp_Title.bBold = true;
            page_Cur.AddCT_MM(rPosLeft + (rPosRight - rPosLeft) / 2, rPosTop, new RepString(fontProp_Title, "Listagem de bairros"));
            ea.container.rHeightMM -= fontProp_Title.rLineFeedMM;  // reduce height of table container for the first page
        }

        // the new container must be added to the current page
        page_Cur.AddMM(rPosLeft, rPosBottom - ea.container.rHeightMM, ea.container);
    }
}

The Neighborhood entity

[Table("Bairros")]
[DisplayColumn("Nome")]
public class Bairro : Entidade
{
    [Key]
    public Guid BairroId { get; set; }

    [Required]
    [Index("IX_Bairro_CidadeId")]
    public Guid CidadeId { get; set; }

    [Required]
    [StringLength(200)]
    [Display(Name = "Bairro")]
    [ColunaRelatorio(Largura = 20, Posicao = 0, Titulo = "Bairro")]
    public string Nome { get; set; }

    public string NomeAbreviado { get; set; }

    public string NomeFonetizado { get; set; }

    [ForeignKey(nameof(CidadeId))]
    public virtual Cidade Cidade { get; set; }

    [InverseProperty(nameof(Logradouro.Bairro))]
    public virtual ICollection<Logradouro> Logradouros { get; set; }
}

the attribute that I created to assist in assembling the table

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class ColunaRelatorioAttribute : Attribute
{
    public Int16 Largura { get; set; }
    public Int16 Posicao { get; set; }
    public string Titulo { get; set; }
    public TlmTextMode TextMode { get; set; } = TlmTextMode.MultiLine;
}

The call in the controller of my application in ASP.NET MVC

var arquivo = Report<Bairro>.Create(db.Bairros.Include(bairro => bairro.Cidade).ToList());                

return File(arquivo, "application/pdf", "Bairros.pdf");
    
26.12.2017 / 12:02