I need a NuGet which is able to convert .docx files to .pdfs, for free. I stumbled accross FreeSpire.Doc but I am having trouble using it.
Here's my code:
using mediere_API.Processors.Interfaces;
using Spire.Doc;
namespace mediere_API.Processors.Implementations;
public class SpireProcessor : ISpireProcessor
{
public Stream ConvertDocxToPdf(Stream document)
{
Document doc = new Document();
document.Position = 0;
doc.LoadFromStream(document, FileFormat.Auto);
var finalFileStream = new MemoryStream();
doc.SaveToStream(finalFileStream, FileFormat.PDF);
return finalFileStream;
}
}
On the line with doc.SaveToStream, I get this exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at spr↨.ᜀ(Byte[] A_0)
at sprẃ..ctor(PointF A_0, SizeF A_1, Byte[] A_2, spr A_3)
at sprẃ..ctor(PointF A_0, SizeF A_1, Byte[] A_2)
at spr㏃.ᜀ(PointF A_0, PointF A_1, Single A_2, Boolean A_3)
at spr㏃.ᜀ(PointF A_0, PointF A_1, Single A_2)
at spr㏃.ᜁ(spr㈻ A_0)
at spr㏃.ᜀ(spr㏀ A_0, spr㏀ A_1, Boolean A_2, spr㈻ A_3)
at spr㎿.ᜁ(spr㈻ A_0)
at spr㎿.ᜃ(spr㈻ A_0)
at spr⛃.ᜂ(spr⓬ A_0)
at spr⓬.ᜀ(spr⓭ A_0)
at sprⓘ.ᜀ(spr⓭ A_0, spr✕ A_1)
at sprⓒ.ᜀ(spr⓭ A_0)
at sprⓓ.ᜀ(spr⓭ A_0)
at sprⓚ.ᜀ(spr⓭ A_0)
at spr⛃.ᜀ(sprⓚ A_0)
at spr⛃.ᜀ(sprⓚ A_0, spr⒵ A_1)
at Spire.Doc.Document.ᜀ(Int32 A_0, spr⛃ A_1)
at sprⷎ.ᜀ(spr⍿ A_0, spr⛃ A_1)
at sprⷎ.ᜂ(spr⍿ A_0)
at Spire.Doc.Document.ᜅ(Stream A_0, ToPdfParameterList A_1)
at Spire.Doc.Document.ᜁ(Stream A_0)
at Spire.Doc.Document.SaveToFile(Stream stream, FileFormat fileFormat)
at Spire.Doc.Document.SaveToStream(Stream stream, FileFormat fileFormat)
at mediere_API.Processors.Implementations.SpireProcessor.ConvertDocxToPdf(Stream document) in /home/octavian/Desktop/bestdavnic73/mediere-api/Processors/Implementations/SpireProcessor.cs:line 14
at mediere_API.Processors.Implementations.PersoanaProcessor.CreateAnexaPlanMediereAsync(Int32 persoanaId, Nullable`1 startDate, Nullable`1 endDate) in /home/octavian/Desktop/bestdavnic73/mediere-api/Processors/Implementations/PersoanaProcessor.cs:line 339
at mediere_API.Controllers.PersoaneController.CreateAnexaMediere(Int32 Id, String startDate, String endDate) in /home/octavian/Desktop/bestdavnic73/mediere-api/Controllers/PersoaneController.cs:line 138
at lambda_method134(Closure, Object)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at mediere_API.Middleware.SchemaHandlingMiddleware.InvokeAsync(HttpContext context, ITenantProcessor tenantProcessor) in /home/octavian/Desktop/bestdavnic73/mediere-api/Middleware/SchemaHandlingMiddleware.cs:line 32
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
and I cannot understand why.
I tried to edit my code in multiple ways, but nothing seems to make it work. I always get this error.
Also, if I change FileFormat.PDF to FileFormat.docx, I get no error but the file is a word document, not a PDF, as I intend.
So, why am I getting this exception and how can I fix it?
Thanks.
Edit: I get the same error if I change doc.SaveToStream to
doc.SaveToFile("/var/tmp/test.pdf", FileFormat.PDF);
For anybody that might have this problem, the document that was passed to this function was generated with OpenXML. For some reason, for those borders:
I get that error. To be more specific, for the InsideHorizontalBorder/InsideVerticalBorder ones, I don't know why. If I don't use those two, everything works fine.