将 XPS 转换为 PDF C#

问题描述 投票:0回答:1

编译后,出现错误:

/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. 

我正在尝试将 XPS 转换为 PDF。为此,我通过 NuGet 下载了 XpsToPdf 库。其中,有一个名为XpsConverter的类。这是我的代码:

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");

从错误中可以明显看出,缺少实现 XpsDocument 类的库。我发现System.Windows.Xps.Packaging中存在这样一个类,但无法通过NuGet安装该包。你能建议我做错了什么吗?

这是我当前的依赖项:

<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>

我应该做哪些调整来解决这个问题?

c# pdf xps
1个回答
0
投票

来自 XpsToPdfnuget 支持的框架:

产品版本.NET....NET框架...
net6.0-windows
...
所以它需要

net6.0-windows

 目标框架,即 net6.0
 具有 Windows 特定功能,而仅仅 
net6.0
 是不够的:

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> ...
    
© www.soinside.com 2019 - 2024. All rights reserved.