I'm trying to apply a license to my proyect using the method Spire.License.LicenseProvider.SetLicenseKey("your license key"); in my ASP.NET Core Application, where I installed the Spire.Office 5.12.8; I'm using this method beacuse I don't have permission to place any archive in the server.
Reading your licensing guide, I noticed need to place this method in the startup method of the startup class. I did it but, when I try to excecute the method where I'm trying to generate a PDF I got the error "Source array was not long enough. Check the source index, length, and the array's lower bounds" in this line:
- Code: Select all
Document document = new Document();
Again, this works fine when I remove the Spire.License.LicenseProvider.SetLicenseKey("your license key"); method, but with the free license limitations.
Here is my Stratup.cs:
- Code: Select all
using GES.Cedulas.Web.Helpers;
using GES.Cedulas.Web.Model;
using GES.Cedulas.Web.Repositories;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Text;
using VueCliMiddleware;
namespace GES.Cedulas.Web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Spire.License.LicenseProvider.SetLicenseKey("My License");
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddNewtonsoftJson();
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);
var appSettings = appSettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);
services.AddAuthentication(x => {
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(x =>{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.IncludeErrorDetails = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false,
RequireExpirationTime = true,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
});
//Base de datos
services.AddDbContext<BD_HerramientasDGSGContext>(opts =>
opts.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));
//Repositorios
services.AddScoped<ICedulasRepository, CedulasRepository>();
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseVueCli(npmScript: "serve", port: 8080);
spa.Options.StartupTimeout = TimeSpan.FromSeconds(200);
}
});
}
}
}
and the Word/PDF method:
- Code: Select all
public ActionResult ReporteMensajeria(string folio)
{
var cedula = repository.getCedula("Mensajeria", folio);
CedMenRecoleccion[] recoleccion = new CedMenRecoleccion[cedula.arreglos.arregloReco.Count];
CedMenEntrega[] entrega = new CedMenEntrega[cedula.arreglos.arregloEntre.Count];
CedMenAcuse[] acuse = new CedMenAcuse[cedula.arreglos.arregloAcuse.Count];
CedMenExtravio[] extravio = new CedMenExtravio[cedula.arreglos.arregloExtravio.Count];
CedMenMalEstado[] malEstado = new CedMenMalEstado[cedula.arreglos.arregloMalEstado.Count];
CedMenMaterialUniforme[] uniforme = new CedMenMaterialUniforme[cedula.arreglos.objetoMaterialUniforme.Count];
for (int i = 0; i < recoleccion.Length; i++)
{
recoleccion[i] = cedula.arreglos.arregloReco[i];
}
for (int i = 0; i < entrega.Length; i++)
{
entrega[i] = cedula.arreglos.arregloEntre[i];
}
for (int i = 0; i < acuse.Length; i++)
{
acuse[i] = cedula.arreglos.arregloAcuse[i];
}
for (int i = 0; i < extravio.Length; i++)
{
extravio[i] = cedula.arreglos.arregloExtravio[i];
}
for (int i = 0; i < malEstado.Length; i++)
{
malEstado[i] = cedula.arreglos.arregloMalEstado[i];
}
for (int i = 0; i < uniforme.Length; i++)
{
uniforme[i] = cedula.arreglos.objetoMaterialUniforme[i];
}
//Jalar documento con marcas
Document document = new Document();
var path = @"d:\Usuarios\oiguerrero\Source\Repos\GESCore\GES.Cedulas.Web\Docs\ReporteMensualMensajeria.docx";
//var path = @"e:\sitios\Cedulas\Docs\ReporteMensualMensajeria.docx";
document.LoadFromFile(path);
//Crear Tabla
Section tablas = document.AddSection();
if (recoleccion.Length > 0)
{
Table tablaRecoleccion = tablas.AddTable(true);
String[] cabeceraRecoleccion = { "Número de Guía", "Código de Rastreo", "Fecha Programada", "Fecha Efectiva", "Tipo de Servicioa" };
var datosRecoleccion = recoleccion.Select(w => new String[] { w.fcNoGuia, w.fcCodRastreo, w.fdFechaProgramada.ToString().Substring(0, 10), w.fdFechaEfectiva.ToString().Substring(0, 10), w.fcTipoServicio }).ToArray();
tablaRecoleccion.ResetCells(datosRecoleccion.Length + 1, cabeceraRecoleccion.Length);
TableRow recRow = tablaRecoleccion.Rows[0];
recRow.IsHeader = true;
recRow.Height = 23;
recRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < cabeceraRecoleccion.Length; i++)
{
//Alineacion de celdas
Paragraph p = recRow.Cells[i].AddParagraph();
recRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Formato de datos
TextRange TR = p.AppendText(cabeceraRecoleccion[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
}
for (int r = 0; r < datosRecoleccion.Length; r++)
{
TableRow DataRow = tablaRecoleccion.Rows[r + 1];
//Fila Height
DataRow.Height = 20;
//Columnas
for (int c = 0; c < datosRecoleccion[r].Length; c++)
{
//Alineacion de Celdas
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Llenar datos en filas
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(datosRecoleccion[r][c]);
//Formato de celdas
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 11;
}
}
BookmarksNavigator marcaRecoleccion = new BookmarksNavigator(document);
marcaRecoleccion.MoveToBookmark("Recoleccion", true, true);
marcaRecoleccion.InsertTable(tablaRecoleccion);
document.Replace("||Recoleccion||", "", false, true);
}
else
document.Replace("||Recoleccion||", "Sin Incidencias en el mes", false, true);
if (entrega.Length > 0)
{
Table tablaEntrega = tablas.AddTable(true);
String[] cabeceraEntrega = { "Número de Guía", "Código de Rastreo", "Fecha Programada", "Fecha Efectiva", "Tipo de Serviciob" };
var datosEntrega = entrega.Select(w => new String[] { w.fcNoGuia, w.fcCodRastreo, w.fdFechaProgramada.ToString().Substring(0, 10), w.fdFechaEfectiva.ToString().Substring(0, 10), w.fcTipoServicio }).ToArray();
tablaEntrega.ResetCells(datosEntrega.Length + 1, cabeceraEntrega.Length);
TableRow entRow = tablaEntrega.Rows[0];
entRow.IsHeader = true;
entRow.Height = 23;
entRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < cabeceraEntrega.Length; i++)
{
//Alineacion de celdas
Paragraph p = entRow.Cells[i].AddParagraph();
entRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Formato de datos
TextRange TR = p.AppendText(cabeceraEntrega[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
}
for (int r = 0; r < datosEntrega.Length; r++)
{
TableRow DataRow = tablaEntrega.Rows[r + 1];
//Fila Height
DataRow.Height = 20;
//Columnas
for (int c = 0; c < datosEntrega[r].Length; c++)
{
//Alineacion de Celdas
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Llenar datos en filas
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(datosEntrega[r][c]);
//Formato de celdas
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 11;
}
}
BookmarksNavigator marcaEntrega = new BookmarksNavigator(document);
marcaEntrega.MoveToBookmark("Entrega", true, true);
marcaEntrega.InsertTable(tablaEntrega);
document.Replace("||Entrega||", "ewqw", false, true);
}
else
document.Replace("||Entrega||", "Sin Incidencias en el mes", false, true);
if (acuse.Length > 0)
{
Table tablaAcuse = tablas.AddTable(true);
String[] cabeceraAcuse = { "Número de la Semana", "Fecha Programada", "Fecha Efectiva" };
var datosAcuse = acuse.Select(w => new String[] { w.fiNoSemana.ToString(), w.fdFechaProgramada.ToString().Substring(0, 10), w.fdFechaEfectiva.ToString().Substring(0, 10) }).ToArray();
tablaAcuse.ResetCells(datosAcuse.Length + 1, cabeceraAcuse.Length);
TableRow acuRow = tablaAcuse.Rows[0];
acuRow.IsHeader = true;
acuRow.Height = 23;
acuRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < cabeceraAcuse.Length; i++)
{
//Alineacion de celdas
Paragraph p = acuRow.Cells[i].AddParagraph();
acuRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Formato de datos
TextRange TR = p.AppendText(cabeceraAcuse[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
}
for (int r = 0; r < datosAcuse.Length; r++)
{
TableRow DataRow = tablaAcuse.Rows[r + 1];
//Fila Height
DataRow.Height = 20;
//Columnas
for (int c = 0; c < datosAcuse[r].Length; c++)
{
//Alineacion de Celdas
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Llenar datos en filas
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(datosAcuse[r][c]);
//Formato de celdas
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 11;
}
}
BookmarksNavigator marcaAcuse = new BookmarksNavigator(document);
marcaAcuse.MoveToBookmark("Acuses", true, true);
marcaAcuse.InsertTable(tablaAcuse);
document.Replace("||Acuses||", "", false, true);
}
else
document.Replace("||Acuses||", "Sin Incidencias en el mes", false, true);
if (extravio.Length > 0)
{
Table tablaExtravio = tablas.AddTable(true);
String[] cabeceraExtravio = { "Número de Guía", "Código de Rastreo", "Tipo de Servicio" };
var datosExtravio = extravio.Select(w => new String[] { w.fcNoGuia, w.fcCodRastreo, w.fcTipoServicio }).ToArray();
tablaExtravio.ResetCells(datosExtravio.Length + 1, cabeceraExtravio.Length);
TableRow extRow = tablaExtravio.Rows[0];
extRow.IsHeader = true;
extRow.Height = 23;
extRow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < cabeceraExtravio.Length; i++)
{
//Alineacion de celdas
Paragraph p = extRow.Cells[i].AddParagraph();
extRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Formato de datos
TextRange TR = p.AppendText(cabeceraExtravio[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
}
for (int r = 0; r < datosExtravio.Length; r++)
{
TableRow DataRow = tablaExtravio.Rows[r + 1];
//Fila Height
DataRow.Height = 20;
//Columnas
for (int c = 0; c < datosExtravio[r].Length; c++)
{
//Alineacion de Celdas
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Llenar datos en filas
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(datosExtravio[r][c]);
//Formato de celdas
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 11;
}
}
BookmarksNavigator marcaExtravio = new BookmarksNavigator(document);
marcaExtravio.MoveToBookmark("Extravio", true, true);
marcaExtravio.InsertTable(tablaExtravio);
document.Replace("||Extravio||", "", false, true);
}
else
document.Replace("||Extravio||", "Sin Incidencias en el mes", false, true);
if (malEstado.Length > 0)
{
//Crear Tabla
Table tablaMalEstado = tablas.AddTable(true);
//Crear Cabeceras y datos
String[] cabeceraMalEstado = { "Número de Guía", "Código de Rastreo", "Tipo de Servicio" };
var datosMalEstado = malEstado.Select(w => new String[] { w.fcNoGuia, w.fcCodRastreo, w.fcTipoServicio }).ToArray();
//Agregar Celdas
tablaMalEstado.ResetCells(datosMalEstado.Length + 1, cabeceraMalEstado.Length);
//Cabecera
TableRow malERow = tablaMalEstado.Rows[0];
malERow.IsHeader = true;
//Fila Height
malERow.Height = 23;
//Formato Cabecera
malERow.RowFormat.BackColor = Color.AliceBlue;
for (int i = 0; i < cabeceraMalEstado.Length; i++)
{
//Alineacion de celdas
Paragraph p = malERow.Cells[i].AddParagraph();
malERow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
p.Format.HorizontalAlignment = HorizontalAlignment.Center;
//Formato de datos
TextRange TR = p.AppendText(cabeceraMalEstado[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
}
//Fila de datos
for (int r = 0; r < datosMalEstado.Length; r++)
{
TableRow DataRow = tablaMalEstado.Rows[r + 1];
//Fila Height
DataRow.Height = 20;
//Columnas
for (int c = 0; c < datosMalEstado[r].Length; c++)
{
//Alineacion de Celdas
DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
//Llenar datos en filas
Paragraph p2 = DataRow.Cells[c].AddParagraph();
TextRange TR2 = p2.AppendText(datosMalEstado[r][c]);
//Formato de celdas
p2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 11;
}
}
//Buscar la Marca e Instertar
BookmarksNavigator marcaMalEstado = new BookmarksNavigator(document);
marcaMalEstado.MoveToBookmark("MalEstado", true, true);
marcaMalEstado.InsertTable(tablaMalEstado);
document.Replace("||MalEstado||", "", false, true);
}
else
document.Replace("||MalEstado||", "Sin Incidencias en el mes", false, true);
//Salvar y Lanzar
byte[] toArray = null;
using(MemoryStream ms1 = new MemoryStream())
{
document.SaveToStream(ms1, Spire.Doc.FileFormat.PDF);
toArray = ms1.ToArray();
}
return File(toArray, "application/pdf", "ReporteMensajeria.pdf");
}