创建基础示例工程
创建基础 .NET Core 示例工程
创建 .NET Core 控制台应用:
- 打开终端窗口,导航到您希望创建工程的目录。
- 运行以下命令创建名为
test_dotnetcore
的 C# 控制台应用程序:
bashdotnet new console -lang C# -o test_dotnetcore
复制 SDK 库文件:
- 将 Foxit PDF SDK for .NET Core 包文件夹下的
lib
文件夹复制到test_dotnetcore
工程目录下。
- 将 Foxit PDF SDK for .NET Core 包文件夹下的
准备 PDF 测试文件:
- 将一个 PDF 文件(例如
Sample.pdf
)复制到test_dotnetcore
目录下,用于测试。
- 将一个 PDF 文件(例如
配置项目文件 (.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
文件与您的目标平台架构匹配。
- 编辑
编写 C# 代码:
- 在任意文本编辑器中打开
Program.cs
文件,添加您的 C# 代码。
[Program.cs]
csusing 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(); } } }
- 在任意文本编辑器中打开
运行工程:
- 在终端窗口中,导航到
test_dotnetcore
目录, 运行以下命令:
bashdotnet run
- 如果成功运行,将在
test_dotnetcore
文件夹下生成testpage.jpg
文件。
- 在终端窗口中,导航到
NOTE
- 请确保
libfsdk.so
(libfsdk.dylib
macOS) 和fsdk_dotnetcore.dll
文件已正确复制到输出目录 (test_dotnetcore/bin/Debug/netcoreapp2.2
或test_dotnetcore/bin/Debug/net6.0
macOS). - 如果动态库未自动复制,请手动将其复制到输出目录。
- 根据您的实际需求,修改
Program.cs
中的代码,实现更复杂的 PDF 处理功能。
创建跨平台基础示例工程
创建跨平台 .NET Core 示例工程
创建 .NET Core 控制台应用:
- 打开终端窗口,导航到您希望创建工程的目录。
- 运行以下命令创建名为
test_autodotnetcore
的 C# 控制台应用程序:
bashdotnet new console -lang C# -o test_autodotnetcore
准备 SDK 库文件:
在
test_autodotnetcore
文件夹下,创建lib
文件夹。在
lib
文件夹下,创建win
、linux
和osx
三个子文件夹,分别用于存放 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.txt
和gsdk_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 └── ...
准备 PDF 测试文件:
- 将一个 PDF 文件(例如
Sample.pdf
)复制到test_autodotnetcore
目录下,用于测试。
- 将一个 PDF 文件(例如
配置项目文件 (.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
此配置会自动根据当前操作系统选择正确的库文件。
- 编辑
编写 C# 代码:
- 打开
Program.cs
文件,添加以下代码:
Details
csusing 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(); } } }
- 打开
运行工程:
- 在终端窗口中,导航到
test_autodotnetcore
目录, 运行以下命令:
bashdotnet build test_autodotnetcore.csproj dotnet run
- 如果成功运行,将在
test_dotnetcore
文件夹下生成testpage.jpg
文件。 - 如果您需要编译 32位的库,运行以下指令:
bashdotnet build test_autodotnetcore.csproj -p:Platform=x86 dotnet run
- 如果成功运行, 将在
test_autodotnetcore
或test_autodotnetcore/bin
文件夹下生成testpage.jpg
文件。
- 在终端窗口中,导航到