Search code examples
c#pdfxps

Convert XPS To PDF C#


After compilation, I get an error:

/mnt/d/Users/Lev/adcm/18/ADCM_Connect/PilotWebApplication/PilotWebApplication.csproj : error NU1101: Unable to find package System.Windows. No packages exist with this id in source(s): nuget.org. 

I'm trying to convert XPS to PDF. For this purpose, I downloaded the XpsToPdf library via NuGet. In it, there is a class called XpsConverter. Here's my code:

var _xpsDocument = new XpsDocument(stream, FileAccess.Read);

// Create an instance of PdfDocument
PdfDocument pdfDocument = new PdfDocument();
// Use XpsConverter to convert XPS to PDF
XpsConverter.Convert(_xpsDocument, pdfDocument, 0);
// Prepare a stream for writing PDF
var pdfStream = new MemoryStream();
pdfDocument.Save(pdfStream);
pdfStream.Position = 0;
// Set response headers for PDF
Response.Headers.Add("Content-Type", "application/pdf");
var encodedFileName = System.Web.HttpUtility.UrlEncode(fileData.name);
Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{System.IO.Path.ChangeExtension(encodedFileName, ".pdf")}\"");
// Return FileStreamResult with PDF
return new FileStreamResult(pdfStream, "application/pdf");

From the error, it's clear that the library implementing the XpsDocument class is missing. I found that such a class exists in System.Windows.Xps.Packaging, but this package cannot be installed via NuGet. Can you suggest what I'm doing wrong?

Here are my current dependencies:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Ascon.Pilot.Common" Version="20.1.0" />
    <PackageReference Include="Ascon.Pilot.DataClasses" Version="20.1.0" />
    <PackageReference Include="Ascon.Pilot.DataModifier" Version="20.1.0" />
    <PackageReference Include="Ascon.Pilot.SDK" Version="23.19.0" />
    <PackageReference Include="Ascon.Pilot.Server.Api" Version="23.19.0" />
    <PackageReference Include="Ascon.Pilot.Transport" Version="20.1.0" />
    <PackageReference Include="GhostscriptSharp" Version="1.3.1.4" />
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Analyzers" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.10.0" />
    <PackageReference Include="PdfSharp" Version="1.50.5147" /> 
    <PackageReference Include="Serilog" Version="3.0.1" />
    <PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="7.0.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
    <PackageReference Include="Syncfusion.Pdf.Net.Core" Version="17.3.0.14" />
    <PackageReference Include="Syncfusion.XpsToPdfConverter.Net.Core" Version="17.3.0.14" /> 
    <!-- <PackageReference Include="System.Security.Claims" Version="4.3.0" />-->
    <PackageReference Include="XpsToPdf" Version="1.0.5" /> 
    <PackageReference Include="System.Windows" Version="8.0.0" />
  </ItemGroup>

</Project>

What adjustments should I make to resolve this issue?


Solution

  • From supported frameworks by XpsToPdf nuget:

    Product Versions
    .NET ... net6.0-windows ...
    .NET Framework ...

    So it requires net6.0-windows target framework, which is net6.0 with Windows-specific features, and just net6.0 would not be enough:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
        ...