Using custom fonts for watermarks

GroupDocs.Watermark is a robust library for managing watermarks in documents of various formats, offering a wide array of customization options. Among its powerful features is the ability to use custom fonts, enabling developers to incorporate unique typography into their watermarks.

In this article, we’ll explore how to use custom fonts that are not installed on the system. Additionally, we will demonstrate how to configure a Linux Docker container for testing scenarios where specific fonts are installed directly in the container environment.

Why Use Custom Fonts for Watermarks?

Using custom fonts for watermarks offers several advantages:

  1. Brand Identity: Ensure your documents align with your organization’s typography guidelines.
  2. System Independence: Avoid dependency on system-installed fonts, ensuring portability and compatibility across different environments.

How Does GroupDocs.Watermark Enable Custom Fonts?

GroupDocs.Watermark simplifies the use of custom fonts by allowing developers to specify a folder containing font files. You can then reference the desired font by its family name, which makes the watermarking process flexible and easy to integrate into your workflow.

The implementation involves three main steps:

  1. Specify the Folder Containing Fonts: Define the path to a directory that holds your font files (e.g., .ttf, .otf).
  2. Set the Font for the Watermark: Use the Font class to initialize the font with its family name, folder path, and size.
  3. Add the Watermark to the Document: Apply the configured watermark to your target document.

Step-by-Step Implementation in C#

Here’s how you can use custom fonts in your watermarking solution with GroupDocs.Watermark:

Key Steps:

  • Specify the path to the document and the output file.
  • Set the folder path where the custom font files reside.
  • Initialize the Font object with the family name and properties.
  • Create a text watermark and configure its properties.
  • Add the watermark to the document and save it.
using GroupDocs.Watermark;
using GroupDocs.Watermark.Options; 
using GroupDocs.Watermark.Watermarks; 

class Program
{
    static void Main()
    {
        string documentPath = "path-to-your-document.docx"; 
        string outputFileName = "path-to-output/document-with-watermark.docx"; 

        // Initialize the Watermarker
        using (Watermarker watermarker = new Watermarker(documentPath))
        {
            // Specify the folder containing custom font files
            string fontsFolder = "path-to-folder_with_fonts";

            // Initialize the font to be used for the watermark
            Font font = new Font("font_family_name", fontsFolder, 36, FontStyle.Bold); // Font family name, size and style

            // Create the watermark object
            TextWatermark watermark = new TextWatermark("Test watermark", font);

            // Set additional watermark properties
            watermark.ForegroundColor = Color.Blue; // Set the foreground color of the watermark
            watermark.Opacity = 0.4; // Set the opacity of the watermark
            watermark.HorizontalAlignment = HorizontalAlignment.Center; // Center horizontally
            watermark.VerticalAlignment = VerticalAlignment.Center; // Center vertically

            // Add the watermark to the document
            watermarker.Add(watermark);

            // Save the watermarked document
            watermarker.Save(outputFileName);
        }
    }
}

Testing GroupDocs.Watermark in a Linux Docker Container

When testing GroupDocs.Watermark in a Linux Docker container, you may encounter scenarios where you want to assume that specific fonts are installed in the system. This is particularly useful for verifying font-dependent functionality or for environments where font folder configuration is not feasible.

Here’s how to configure a Docker container to install the required dependencies and custom fonts.

Dockerfile for Testing

Below is a sample Dockerfile for running a .NET project named WebApp in a Linux Docker container. The file also demonstrates how to install a custom font (MyFont.ttf) and the dependencies required for GroupDocs.Watermark:

# Use ASP.NET runtime as base image
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

# Add libgdiplus and libc6-dev for graphics support
RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev

# Add `contrib` archive area to package sources list
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list

# Add default fonts
RUN apt-get update && apt-get install -y ttf-mscorefonts-installer fontconfig
RUN fc-cache -f -v # Refresh font cache

# Copy custom font to the font directory
COPY ["WebApp/MyFont.ttf", "/usr/share/fonts/truetype/"]
RUN fc-cache -f -v # Refresh font cache again

# Building the .NET application
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApp/WebApp.csproj", "WebApp/"]
RUN dotnet restore "WebApp/WebApp.csproj"
COPY . .
WORKDIR "/src/WebApp"
RUN dotnet build "WebApp.csproj" -c Release -o /app/build

# Publish the application
FROM build AS publish
RUN dotnet publish "WebApp.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Final stage with ASP.NET runtime
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

# Set the entry point for the container
ENTRYPOINT ["dotnet", "WebApp.dll"]

Key Points in the Dockerfile

Installing Required Libraries:

RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev

These libraries are essential for proper rendering of images in Linux.

Installing Default Fonts:

RUN apt-get update && apt-get install -y ttf-mscorefonts-installer fontconfig

RUN fc-cache -f -v

This step installs default fonts, which are required if you are using GroupDocs.Watermark without setting a license.

Adding a Custom Font:

COPY ["WebApp/MyFont.ttf", "/usr/share/fonts/truetype/"]

RUN fc-cache -f -v

This command copies the custom font (MyFont.ttf) to the appropriate font directory in the container and updates the font cache.

Building and Running the Application:

The remaining commands configure the Docker container to build and run your .NET application (WebApp), ensuring that the custom font is available during runtime.

Enabling Unix Support in .csproj

Due to limitations in the System.Drawing.Common library in .NET 6 for Linux, you need to enable Unix support by adding a specific setting to your .csproj file. For more details on these limitations, refer to the Microsoft documentation.

<ItemGroup>
    <RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
</ItemGroup>

This setting ensures that the System.Drawing functionalities work correctly in a Linux environment, which is essential for proper rendering when using GroupDocs.Watermark.

Best Practices for Using Custom Fonts

To make the most of this feature, follow these best practices:

  1. Organize Fonts: Keep your custom fonts organized in a dedicated folder for easy reference.
  2. Validate Font Names: Ensure you correctly specify the font family name to avoid rendering issues.
  3. Test in a Containerized Environment: Use Docker containers to test your application in a controlled, Linux-based environment.

Conclusion

The ability to use custom fonts in GroupDocs.Watermark enhances your control over watermark design, allowing you to meet specific branding and styling requirements. By specifying a font folder or installing fonts in a Linux container, you can seamlessly test and deploy your application in any environment.

The provided Dockerfile and C# examples serve as a comprehensive guide for implementing and testing this functionality. Try them out to ensure your watermarking solution is flexible, portable, and ready for production.

Get a Free Trial

You can try GroupDocs.Watermark APIs for free by just downloading and installing the latest version on our release downloads website.

You can also get a temporary license to test all the library’s functionalities without any constraints. Head to the temporary license page to apply for a temporary license.

See Also

For more information and additional resources, you may find the following links useful: