PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
进入样本文件夹内并从命令行运行示例:
如果您收到错误消息 bash: ./test.sh: Permission denied,您需要输入以下命令:
chmod+x run .sh可以按照下面所述的手动或 nuget 集成进行操作。本节将帮助您构建您的第一个 ComPDFKit 应用程序。如果您能够打开、保存和关闭 PDF文档,则可以轻松集成 ComPDF PDF SDK 的其余部分。
通过控制台创建一个名为 ComPDFKit Demo 的新项目:
mkdir ComPDFKitDemo
cd ComPDFKitDemo
dotnet new console -o "ComPDFKit Demo"将 ComPDFKitNative.so 文件和 ComPDFKit.NET.dll 文件复制到项目文件夹中。
将以下代码添加到 .csproj 文件中:
<ItemGroup>
<Reference Include="ComPDFKit.NET.Core">
<HintPath>ComPDFKit.NET.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="ComPDFKitNative.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>您最终的 ComPDFKit Demo.csproj 文件应该如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="ComPDFKit.NET.Core">
<HintPath>ComPDFKit.NET.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="ComPDFKitNative.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>通过控制台创建一个名为 ComPDFKit Demo 的新项目:
mkdir ComPDFKitDemo
cd ComPDFKitDemo
dotnet new console -o "ComPDFKit Demo"进入项目文件夹,安装 ComPDFKit.Core NuGet 包:
dotnet add package ComPDFKit.NetCore我们已经完成了所有准备步骤。现在让我们使用 ComPDFKit PDF SDK 创建一个带有一个空白页的 PDF 文件,用下面这段代码替换您的 Program.cs 文件,注意:需要在 LicenseVerify() 方法中指定您的许可证密钥 XML 文件路径。
using ComPDFKit.NativeMethod;
using ComPDFKit.PDFDocument;
using System.Reflection.Metadata;
namespace ComPDFKit_Demo
{
public class Program
{
private static bool LicenseVerify()
{
if (!CPDFSDKVerifier.LoadNativeLibrary())
return false;
string xmlPath = "您的许可证密钥 XML 文件的路径";
LicenseErrorCode verifyResult = CPDFSDKVerifier.LicenseVerify(xmlPath);
return (verifyResult == LicenseErrorCode.E_LICENSE_SUCCESS);
}
public static void Main()
{
LicenseVerify();
CPDFDocument document = CPDFDocument.CreateDocument();
// 插入到第一页。
int pageIndex = 0;
int pageWidth = 595;
int pageHeight = 842;
// InsertPage方法可以指定一个图片路径,当图片路径为空时即插入空白页面。
document.InsertPage(pageIndex, pageWidth, pageHeight, "");
document.WriteToFilePath("new_file.pdf");// 将文档对象全量保存到当前路径
Console.WriteLine("Done. Results saved in new_file.pdf");
}
}
}现在您可以从命令行运行程序:
dotnet run现在您会在程序输出目录下找到 new_file.pdf 文件,它是一个带有一个空白页的 PDF 文件。