Foxit PDF SDK .NET Framework Library
This section describes how to run samples and create a basic project using the Foxit PDF SDK .NET Framework library. The project demonstrates rendering the first page of a PDF document to a bitmap and saving it as a JPG image.
Prerequisites
Development environment
- Windows .NET Framework version: ≥ 4.0
- Foxit PDF SDK .NET library
System support
We provide detailed system support information for Windows, including operating system versions, compiler requirements, and more. Select your platform to view the details.
WindowsRun samples from the command line
See Samples for an overview of the sample projects, dependency information for specific samples, and instructions for running samples from the command line.
Run samples in Visual Studio
After building with Visual Studio, a bin folder is generated under \examples\simple_demo\, containing .exe executables. The executable name depends on the build configuration.
Run all samples
- Open the solution: In the
\examples\simple_demofolder, open the solution file (.sln) that matches your Visual Studio version. - Build the solution: In Visual Studio, select Build > Build Solution to compile all samples.
- Run a sample: After a successful build, double-click the generated
.exeto run the corresponding sample.
Run a specific sample
- Build the sample project: In Visual Studio Solution Explorer, right-click the target sample project and select Build. Alternatively, double-click the
.vcxprojfile in the sample folder to open and build that project. - Run the sample: After a successful build, double-click the generated
.exeto run the sample.
To see detailed sample output, run from the command line.
- Open
cmd.exe, usecdto navigate to\examples\simple_demo\bin, then run the desired.exe.
Quick start
Create a C# .NET Framework sample project quickly
Create a console application:
- Open Visual Studio and create a C# console application named
test_dotnet.
- Open Visual Studio and create a C# console application named
Copy SDK library files:
- Copy the
libfolder from the SDK package into thetest_dotnetproject directory.
- Copy the
Add a reference to fsdk_dotnet.dll:
- In Solution Explorer, right-click the
test_dotnetproject and select "Add" -> "Reference...". - In the Reference Manager dialog, select the "Browse" tab.
- Based on your build configuration, navigate to
test_dotnet\lib\x64_vc10,test_dotnet\lib\x86_vc10, ortest_dotnet\lib\anycpu_vc15. - Select
fsdk_dotnet.dlland click "Add".
NOTE
- Ensure the architecture of
fsdk_dotnet.dllmatches your application's platform target (Win32/Win64/AnyCPU).
- In Solution Explorer, right-click the
Add the fsdk.dll native library:
- Win32/Win64 platform target:
- Right-click the
test_dotnetproject and selectAdd->Existing Item.... - Based on your build configuration, navigate to
test_dotnet\lib\x64_vc15ortest_dotnet\lib\x86_vc15. - Select
fsdk.dlland clickAdd.
- Right-click the
- AnyCPU platform target:
- Right-click the
test_dotnetproject, selectAdd-> "New Folder...", and create a folder namedx64. - Right-click the
x64folder, selectAdd->Existing Item..., navigate totest_dotnet\lib\x64_vc15\, selectfsdk.dll, and clickAdd. - Repeat the steps above to create an
x86folder and addfsdk.dllfromtest_dotnet\lib\x86_vc15\.
- Right-click the
NOTE
- Set the
Copy to Output Directoryproperty offsdk.dlltoCopy if newer. - For AnyCPU, you must successfully call Library.Initialize before creating SDK objects.
- Win32/Win64 platform target:
Add using declarations (Program.cs):
Add the following using declarations at the top of
Program.cs:csharpusing foxit; using foxit.common; using foxit.common.fxcrt; using foxit.pdf;
Initialize the Foxit PDF SDK library:
- Before calling any API, initialize the Foxit PDF SDK library with your license key. See Initialize the SDK library.
Load and parse a PDF document:
- Copy
Sample.pdfto thetest_dotnet\test_dotnetfolder. - Add code in
Program.csto loadSample.pdfand parse its first page.
[loadpdf.cs]
csharpPDFDoc doc = new PDFDoc("..\..\Sample.pdf"); error_code = doc.LoadW(""); if (error_code != ErrorCode.e_ErrSuccess) { return; } // Get the first page of the document. PDFPage page = doc.GetPage(0); // Parse page. page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);- Copy
Render and save the PDF page:
- Add code to render the PDF page to a bitmap and save it as a JPG image.
[SaveAsImage.cs]
csint 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_DIBArgb, System.IntPtr.Zero, 0); 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");:::
Build the project:
- Click
Build->Build Solutionto compile the project. - The executable
test_dotnet.exeis generated undertest_dotnet\test_dotnet\bin\Debugortest_dotnet\test_dotnet\bin\Release, depending on the build configuration.
NOTE
- For Win32/Win64, ensure
fsdk.dllandfsdk_dotnet.dllare copied to the same folder astest_dotnet.exe. - For AnyCPU, ensure
fsdk_dotnet.dllis copied to the same folder astest_dotnet.exe, and that thex64andx86folders contain the correspondingfsdk.dllfiles.
- Click
Run the project:
- Double-click
test_dotnet.exeto run the project. - If it runs successfully,
testpage.jpgis generated in the current folder.
- Double-click
Complete code example:
[Program.cs]
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
namespace test_dotnet
{
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_DIBArgb, System.IntPtr.Zero, 0);
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();
}
}
}