We express our huge gratitude to Peter Robinson for the creation of manual about «Using Document .Net in Linux» (PDF).
In order to build multi-platform applications using .NET Core on Linux, the first steps are for installing in our Linux machine the required tools.
We need to install .NET Core SDK from Microsoft and to allow us to develop easier, we will install an advance editor with a lot of features, Visual Studio Code from Microsoft.
Both installations are very easy and the detailed description can be found by these two links:
Check that the directory with fonts "/usr/share/fonts/truetype" is exist. Also check that it contains *.ttf files.
You can verify the successful addition of the new path by running chkfontpath command or by listing X font server's /etc/X11/XF86Config file.
If you do not have root access, copy the *.ttf to ~/.fonts directory instead.
Or you may install “Microsoft TrueType core fonts” using terminal and command: $ sudo apt install ttf-mscorefonts-installer
Read more about TrueType Fonts and “How to install Microsoft fonts, How to update fonts cache files, How to confirm new fonts installation” .
With these steps, we will ready to start developing.
In next paragraphs we will explain in detail how to create simple console application. All of them are based on this VS Code guide:
Get Started with C# and Visual Studio CodeNot only is possible to create .NET Core applications that will run on Linux using Linux as a developing platform. It is also possible to create it using a Windows machine and any modern Visual Studio version, as Microsoft Visual Studio Community 2022.
Create a new folder in your Linux machine with the name Merge PDF files.
For example, let’s create the folder “Merge PDF files” on Desktop ( Right click-> New Folder):
Open VS Code and click in the menu File->Open Folder. From the dialog, open the folder you’ve created previously:
Next you will see the similar screen:
Now, open the integrated console – the Terminal: follow to the menu Terminal -> New
Terminal (or press Ctrl+Shift+’):
Create a new console application, using dotnet command.
Type this command in the Terminal console: dotnet new console
Now we are going to convert this simple application into something more interesting. We’ll transform it into an application that will convert a Merge PDF files.
First of all, we need to add the package reference to the sautinSoft.pdf assembly using Nuget.
In order to do it, follow to the Explorer and open project file “Merge PDF files.csproj” within
VS Code to edit it:
Add these lines into the Merge PDF files.csproj file:
<ItemGroup>
<PackageReference Include="sautinsoft.pdf" Version="*" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.7" />
<PackageReference Include="HarfBuzzSharp.NativeAssets.Linux" Version="*" />
</ItemGroup>
The first reference installs the latest version sautinSoft.pdf package from Nuget.
The second reference installs the SkiaSharp.NativeAssets.Linux package, which adds 2D graphics to .Net applications for Linux.
The third reference installs the HarfBuzzSharp.NativeAssets.Linux package, which adds complex typography support for Linux in .NET applications.
At once as we’ve added the package reference, we have to save the “Merge PDF files.csproj” and restore the added package.
Follow to the Terminal and type the command: dotnet restore
Good, now our application has the reference to sautinSoft.pdf package and we can write the code to convert Merge PDF files and other formats.
Follow to the Explorer, open the Program.cs, remove all the code and type the new:
using SautinSoft.Pdf;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// SautinSoft.Pdf.PdfDocument.SetLicense("1234567890");
string[] inpFiles = new string[] {
Path.GetFullPath(@"/home/alex/Desktop/MergeFile01.pdf"),
Path.GetFullPath(@"/home/alex/Desktop/MergeFile02.pdf"),
Path.GetFullPath(@"/home/alex/Desktop/MergeFile03.pdf")};
string outFile = @"/home/alex/Desktop/Merged.pdf";
using (var pdf = new PdfDocument())
{
// Merge multiple PDF files into single PDF.
foreach (var inpFile in inpFiles)
{
using (var source = PdfDocument.Load(inpFile))
{
foreach (var page in source.Pages)
pdf.Pages.AddClone(page);
}
}
pdf.Save(outFile);
}
// Show the result PDF document.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
}
}
}
To make tests, we need an input PDF’s documents.
Launch our application and convert the “sample.pdf” into “sample.pdf”, type the
command: dotnet run
If you don’t see any exceptions, everything is fine, and we can check the result produced by the SautinSoft.Pdf library.
The new file “sample.pdf” has to appear on the Desktop:
Open the result:
Well done! You have created the “Merge PDF files” application under Linux!
If you have any troubles or need extra code, or help, don’t hesitate to ask our SautinSoft
Team at support@sautinsoft.com.
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: