You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#实现XPS转PDF时遇NU1101错误的解决咨询

问题描述

在.NET 6.0项目中实现XPS转PDF功能时,引入XpsToPdf库后编译报错:

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

排查发现代码中使用的XpsDocument属于System.Windows.Xps.Packaging,但无法通过NuGet安装该组件。转换代码及项目依赖配置如下:

转换代码

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

项目依赖配置

<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>
解决方法

核心问题说明

System.Windows并非NuGet可安装包,它是.NET Framework专属组件;.NET 6作为跨平台框架,默认不包含该组件。以下提供两种可行方案:


方案1:使用已引入的Syncfusion组件完成转换(推荐,跨平台友好)

你的项目已经引用了Syncfusion.XpsToPdfConverter.Net.Core,该库专为.NET Core/.NET 6设计,无需依赖System.Windows,直接用流即可完成转换。

步骤:

  1. 删除无效的System.Windows引用,同时移除不需要的XpsToPdf包:
    <!-- 移除这两行 -->
    <PackageReference Include="XpsToPdf" Version="1.0.5" /> 
    <PackageReference Include="System.Windows" Version="8.0.0" />
    
  2. 修改转换代码为Syncfusion的API:
    using Syncfusion.Pdf;
    using Syncfusion.XpsToPdfConverter;
    
    // 直接通过流完成XPS转PDF,无需XpsDocument
    var pdfStream = new MemoryStream();
    XpsToPdfConverter.Convert(stream, pdfStream);
    pdfStream.Position = 0;
    
    // 响应头设置及返回逻辑保持不变
    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 new FileStreamResult(pdfStream, "application/pdf");
    
  3. 移除项目中版本不兼容的Microsoft.AspNetCore.App引用(.NET 6的ASP.NET Core SDK会自动包含相关依赖)。

方案2:适配Windows平台使用XpsDocument

若必须使用XpsDocument,需将项目改为Windows专属目标框架,仅能在Windows系统运行:

步骤:

  1. 修改项目文件中的TargetFramework为net6.0-windows:
    <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net6.0-windows</TargetFramework>
      <GenerateDocumentationFile>true</GenerateDocumentationFile>
      <!-- 若需WPF相关支持,可添加以下配置 -->
      <!-- <UseWPF>true</UseWPF> -->
    </PropertyGroup>
    
  2. 删除无效的System.Windows引用,net6.0-windows框架已包含System.Windows.Xps.Packaging相关API。
  3. 原转换代码可正常编译运行,但项目将失去跨平台能力。

内容的提问来源于stack exchange,提问作者Лев Герасимов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 12:05:55