Skip to content

福昕 PDF SDK .NETCore 库

本指南将介绍如何使用福昕 PDF SDK for .NET Core 创建一个基础的 C# 控制台示例工程和一个跨平台的示例工程。示例工程演示将 PDF 文档的首页渲染成位图,并将其保存为 JPG 图片。我们将以 .NET Core 2.2 为例,展示如何在 Windows、Linux 和 Mac x64 平台上创建工程和自动切换库文件。

先决条件

开发环境

  • Windows/Linux/macOS x64 .NETCore 版本: ≥ 2.1
  • MacOS_arm64 .NET SDK 版本:≥ 6.0
  • 福昕 PDF SDK .NETCore 库

系统支持

我们提供 Windows、Linux 和 MacOS 平台的详细系统支持信息,包括操作系统版本、编译器要求等。请选择您的操作系统平台,查看详细信息。

Windows 平台 Linux 平台 Mac 平台

运行示例

使用命令行运行示例

请参阅 示例,其中包含了示例工程简介,特定示例的依赖信息,以及如何通过命令行运行示例。

.NET SDK 6.0 或更高版本

如果您使用的是 .NET SDK 6.0 或更高版本,请按照以下步骤操作以运行示例:

  1. 修改项目文件: 打开您的 .csproj 项目文件。

  2. 更新 TargetFramework:<TargetFramework> 属性的值设置为 net6.0

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

创建基础示例工程

创建基础 .NET Core 示例工程

  1. 创建 .NET Core 控制台应用:

    • 打开终端窗口,导航到您希望创建工程的目录。
    • 运行以下命令创建名为 test_dotnetcore 的 C# 控制台应用程序:
    bash
    dotnet new console -lang C# -o test_dotnetcore
  2. 复制 SDK 库文件:

    • 将 Foxit PDF SDK for .NET Core 包文件夹下的 lib 文件夹复制到 test_dotnetcore 工程目录下。
  3. 准备 PDF 测试文件:

    • 将一个 PDF 文件(例如 Sample.pdf)复制到 test_dotnetcore 目录下,用于测试。
  4. 配置项目文件 (.csproj):

    • 编辑 test_dotnetcore.csproj 文件,添加以下代码:
    [.csproj]
    xml
      <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>netcoreapp2.2</TargetFramework>
          <!-- For Mac arm64 -->
          <!-- <TargetFramework>netcoreapp6.0</TargetFramework> -->
          </PropertyGroup>
    
        <ItemGroup>
          <Reference Include="fsdk_dotnetcore">
            <HintPath>lib\fsdk_dotnetcore.dll</HintPath>
          </Reference>
        </ItemGroup>
    
        <ItemGroup>
          <!-- For Windows -->
          <FSdkLibSourceFiles Include="\lib\*fsdk*.*" />
          <FSdkLibSourceFilesX86 Include="\lib\x86_vc15\fsdk.dll" />
          <FSdkLibSourceFilesX64 Include="\lib\x64_vc15\fsdk.dll" />
          <!-- For Linux -->
          <!-- <FSdkLibSourceFiles Include="lib\libfsdk.so" /> -->
          <!-- For MacOS -->
          <!-- <FSdkLibSourceFiles Include="libfsdk.dylib"> -->
        </ItemGroup>
    
        <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
          <Copy SourceFiles="@(FSdkLibSourceFiles)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="True" />
        </Target>
    
        <ItemGroup>
          <None Update="Sample.pdf">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          </None>
        </ItemGroup>
      </Project>

    NOTE

    • 请根据您的目标平台, 选择正确的 <TargetFramework>FSdkLibSourceFiles.
    • 请确保 lib 文件夹中的 libfsdk.so(Linux) 、libfsdk.dylib (macOS) 和 fsdk_dotnetcore.dll 文件与您的目标平台架构匹配。
  5. 编写 C# 代码:

    • 在任意文本编辑器中打开 Program.cs 文件,添加您的 C# 代码。
    [Program.cs]
    cs
    using System;
    
    using foxit.common;
    using foxit.common.fxcrt;
    using foxit.pdf;
    
    namespace test_dotnetcore
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                // The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
                // The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
                string sn = " ";
                string key = " ";
                ErrorCode error_code = Library.Initialize(sn, key);
                if (error_code != ErrorCode.e_ErrSuccess)
                {
                    return;
                }
    
                using (PDFDoc doc = new PDFDoc("Sample.pdf"))
                {
                    error_code = doc.LoadW("");
                    if (error_code != ErrorCode.e_ErrSuccess)
                    {
                        Library.Release();
                        return;
                    }
    
                    using (PDFPage page = doc.GetPage(0))
                    {
                        // Parse page.
                        page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
    
                        int width = (int)(page.GetWidth());
                        int height = (int)(page.GetHeight());
                        Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
    
                        // Prepare a bitmap for rendering.
                        foxit.common.Bitmap bitmap = new foxit.common.Bitmap(width, height,foxit.common.Bitmap.DIBFormat.e_DIBRgb32);
                        bitmap.FillRect(0xFFFFFFFF, null);
    
                        // Render page
                        Renderer render = new Renderer(bitmap, false);
                        render.StartRender(page, matrix, null);
    
                        // Add the bitmap to image and save the image.
                        foxit.common.Image image = new foxit.common.Image();
                        image.AddFrame(bitmap);
                        image.SaveAs("testpage.jpg");
                    }
                }
                Library.Release();
            }
        }
    }
  6. 运行工程:

    • 在终端窗口中,导航到 test_dotnetcore 目录, 运行以下命令:
    bash
    dotnet run
    • 如果成功运行,将在 test_dotnetcore 文件夹下生成 testpage.jpg 文件。

NOTE

  • 请确保 libfsdk.so (libfsdk.dylib macOS) 和 fsdk_dotnetcore.dll 文件已正确复制到输出目录 ( test_dotnetcore/bin/Debug/netcoreapp2.2test_dotnetcore/bin/Debug/net6.0 macOS).
  • 如果动态库未自动复制,请手动将其复制到输出目录。
  • 根据您的实际需求,修改 Program.cs 中的代码,实现更复杂的 PDF 处理功能。

创建跨平台基础示例工程

创建跨平台 .NET Core 示例工程

  1. 创建 .NET Core 控制台应用:

    • 打开终端窗口,导航到您希望创建工程的目录。
    • 运行以下命令创建名为 test_autodotnetcore 的 C# 控制台应用程序:
    bash
    dotnet new console -lang C# -o test_autodotnetcore
  2. 准备 SDK 库文件:

    • test_autodotnetcore 文件夹下,创建 lib 文件夹。

    • lib 文件夹下,创建 winlinuxosx 三个子文件夹,分别用于存放 Windows、Linux 和 macOS 平台的库文件。

    • 将以下库文件复制到相应的文件夹:

      • foxitpdfsdk_*_win_dotnetcore/lib -> test_autodotnetcore/lib/win
      • foxitpdfsdk_*_linux64_dotnetcore/lib -> test_autodotnetcore/lib/linux
      • foxitpdfsdk_*_mac_dotnetcore/lib -> test_autodotnetcore/lib/osx
    • 将任意平台包中 lib 目录下的试用许可证文件 gsdk_key.txtgsdk_sn.txt 复制到 test_autodotnetcore/lib 文件夹。

    • 完成上述操作后,工程目录结构应如下所示:

      test_autodotnetcore/
      ├── lib/
      │   ├── win/
      │   │   └── ... (Windows 库文件)
      │   ├── linux/
      │   │   └── ... (Linux 库文件)
      │   ├── osx/
      │   │   └── ... (macOS 库文件)
      │   ├── gsdk_key.txt
      │   └── gsdk_sn.txt
      ├── test_autodotnetcore.csproj
      ├── Program.cs
      └── ...
  3. 准备 PDF 测试文件:

    • 将一个 PDF 文件(例如 Sample.pdf)复制到 test_autodotnetcore 目录下,用于测试。
  4. 配置项目文件 (.csproj):

    • 编辑 test_autodotnetcore.csproj 文件,添加以下代码:
    [.csproj]
    xml
        <Project Sdk="Microsoft.NET.Sdk">
          <PropertyGroup>
            <OutputType>Exe</OutputType>
            <TargetFramework>netcoreapp2.2</TargetFramework>
            <AppendTargetFrameworkToOutputPath>False</AppendTargetFrameworkToOutputPath>
            <RuntimeIdentifiers>win-x86;win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
          </PropertyGroup>
    
          <PropertyGroup Condition="'<span class="math-inline">\(Configuration\)\|</span>(Platform)'=='Debug|AnyCPU'">
            <OutputPath>bin\</OutputPath>
          </PropertyGroup>
    
          <PropertyGroup Condition="'<span class="math-inline">\(Configuration\)\|</span>(Platform)'=='Release|AnyCPU'">
            <OutputPath>bin\</OutputPath>
          </PropertyGroup>
    
          <ItemGroup Condition="<span class="math-inline">\(\[MSBuild\]\:\:IsOsPlatform\(Windows\)\)"\>
          <Content Include\="</span>(OutputPath)fsdk.dll" Link="fsdk.dll">
              <CopyToOutputDirectory>Always</CopyToOutputDirectory>
              <Pack>True</Pack>
              <PackagePath>lib\netstandard2.2</PackagePath>
            </Content>
          </ItemGroup>
    
          <ItemGroup Condition="<span class="math-inline">\(\[MSBuild\]\:\:IsOsPlatform\(Linux\)\)"\>
          <Content Include\="</span>(OutputPath)libfsdk.so" Link="libfsdk.so">
              <CopyToOutputDirectory>Always</CopyToOutputDirectory>
              <Pack>True</Pack>
              <PackagePath>lib\netstandard2.2</PackagePath>
            </Content>
          </ItemGroup>
    
          <ItemGroup Condition="<span class="math-inline">\(\[MSBuild\]\:\:IsOsPlatform\(OSX\)\)"\>
          <Content Include\="</span>(OutputPath)libfsdk.dylib" Link="libfsdk.dylib">
              <CopyToOutputDirectory>Always</CopyToOutputDirectory>
              <Pack>True</Pack>
              <PackagePath>lib\netstandard2.2</PackagePath>
            </Content>
          </ItemGroup>
    
          <ItemGroup>
            <None Remove="lib\**" />
          </ItemGroup>
    
          <ItemGroup>
            <Reference Include="fsdk_dotnetcore">
              <HintPath><span class="math-inline">\(OutputPath\)fsdk\_dotnetcore\.dll</HintPath\>
            </Reference\>
          </ItemGroup\>
          <ItemGroup Condition\="</span>([MSBuild]::IsOsPlatform(Windows)) And '<span class="math-inline">\(Platform\)'\=\='x86'"\>
            <FSdkLibSourceFiles Include\="lib\\win\\x86\_vc15\\\*fsdk\*\.\*" /\>
          </ItemGroup\>
          <ItemGroup Condition\="</span>([MSBuild]::IsOsPlatform(Windows)) And ('<span class="math-inline">\(Platform\)'\=\='AnyCPU' Or '</span>(Platform)'=='x64')">
            <FSdkLibSourceFiles Include="lib\win\x64_vc15\*fsdk*.*" />
          </ItemGroup>
    
          <ItemGroup Condition="<span class="math-inline">\(\[MSBuild\]\:\:IsOsPlatform\(Linux\)\)"\>
            <FSdkLibSourceFiles Include\="lib\\linux\\\*fsdk\*\.\*" /\>
          </ItemGroup\>
          <ItemGroup Condition\="</span>([MSBuild]::IsOsPlatform(OSX))">
            <FSdkLibSourceFiles Include="lib\osx\*fsdk*.*" />
          </ItemGroup>
    
          <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
            <Copy SourceFiles="@(FSdkLibSourceFiles)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="True" />
          </Target>
    
          <ItemGroup>
            <None Update="Sample.pdf">
              <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            </None>
          </ItemGroup>
        </Project>

    NOTE

    此配置会自动根据当前操作系统选择正确的库文件。

  5. 编写 C# 代码:

    • 打开 Program.cs 文件,添加以下代码:
    Details
    cs
    using System;
    using foxit.common;
    using foxit.common.fxcrt;
    using foxit.pdf;
    
        namespace test_autodotnetcore
        {
            class Program
            {
                static void Main(string[] args)
                {
                    string sn = " "; // 从 gsdk_sn.txt 获取
                    string key = " "; // 从 gsdk_key.txt 获取
                    ErrorCode error_code = Library.Initialize(sn, key);
                    if (error_code != ErrorCode.e_ErrSuccess)
                    {
                        return;
                    }
    
                    using (PDFDoc doc = new PDFDoc("Sample.pdf"))
                    {
                        error_code = doc.LoadW("");
                        if (error_code != ErrorCode.e_ErrSuccess)
                        {
                            Library.Release();
                            return;
                        }
    
                        using (PDFPage page = doc.GetPage(0))
                        {
                            page.StartParse((int)PDFPage.ParseFlags.e_ParsePageNormal, null, false);
                            int width = (int)(page.GetWidth());
                            int height = (int)(page.GetHeight());
                            Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
                            foxit.common.Bitmap bitmap = new foxit.common.Bitmap(width, height, foxit.common.Bitmap.DIBFormat.e_DIBRgb32);
                            bitmap.FillRect(0xFFFFFFFF, null);
                            Renderer render = new Renderer(bitmap, false);
                            render.StartRender(page, matrix, null);
                            foxit.common.Image image = new foxit.common.Image();
                            image.AddFrame(bitmap);
                            image.SaveAs("testpage.jpg");
                        }
                    }
                    Library.Release();
                }
            }
    }
  6. 运行工程:

    • 在终端窗口中,导航到 test_autodotnetcore 目录, 运行以下命令:
    bash
    dotnet build test_autodotnetcore.csproj
    dotnet run
    • 如果成功运行,将在 test_dotnetcore 文件夹下生成 testpage.jpg 文件。
    • 如果您需要编译 32位的库,运行以下指令:
    bash
    dotnet build test_autodotnetcore.csproj -p:Platform=x86
    dotnet run
    • 如果成功运行, 将在 test_autodotnetcoretest_autodotnetcore/bin 文件夹下生成 testpage.jpg 文件。