使用自訂字體作為浮水印

GroupDocs.Watermark 是一個強大的庫,用於管理各種格式文檔中的水印,提供廣泛的自定義選項。它的強大功能之一是能夠使用自定義字體,這使得開發人員可以將獨特的排版納入他們的水印中。

在這篇文章中,我們將探討如何使用未安裝在系統上的自訂字型。此外,我們將演示如何配置 Linux Docker 容器,以便在特定字型直接安裝在容器環境中的測試情境中進行測試。

為什麼要使用自訂字型作為水印?

使用自訂字型作為水印有幾個優勢:

  1. 品牌識別:確保您的文件符合組織的排版指南。
  2. 系統獨立性:避免依賴系統安裝的字體,確保在不同環境中具有可攜性和兼容性。

GroupDocs.Watermark 如何啟用自定義字體?

GroupDocs.Watermark 透過允許開發人員指定包含字體文件的資料夾,簡化了自定義字體的使用。您可以通過其字體名稱來引用所需的字體,這使得水印處理過程靈活易於整合到您的工作流程中。

實施包含三個主要步驟:

  1. 指定包含字體的資料夾:定義字體檔案的目錄路徑(例如,.ttf.otf)。
  2. 設置水印的字體:使用 Font 類來初始化字體,並設置其家族名稱、文件夾路徑和大小。
  3. 將水印添加到文檔:將配置的水印應用到您的目標文檔。

逐步實現 C#

以下是您如何在與 GroupDocs.Watermark 的水印解決方案中使用自定義字體:

關鍵步驟:

  • 指定文檔和輸出文件的路徑。
  • 設置自訂字體檔案所在的資料夾路徑。
  • 初始化 Font 物件,並設定字型家族名稱和屬性。
  • 創建一個文本水印並配置其屬性。
  • 將水印添加到文件並保存。
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);
        }
    }
}

在 Linux Docker 容器中測試 GroupDocs.Watermark

當在Linux Docker容器中測試GroupDocs.Watermark時,您可能會遇到希望假設特定字體已安裝在系統中的情況。這對於驗證依賴字體的功能或在字體文件夾配置不可行的環境中特別有用。

這裡是如何配置 Docker 容器以安裝所需的依賴項和自定義字體。

Dockerfile for Testing

以下是用於在 Linux Docker 容器中運行名為 WebApp 的 .NET 項目的示例 Dockerfile。該文件還演示了如何安裝自定義字體 (MyFont.ttf) 以及 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"]

Dockerfile中的要點

安裝所需的函式庫:

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

這些函式庫對於在 Linux 中正確呈現圖像是必不可少的。

安裝默認字型:

執行 apt-get 更新 && apt-get 安裝 -y ttf-mscorefonts-installer fontconfig

RUN fc-cache -f -v

此步驟安裝預設字型,如果您在未設定許可證的情況下使用 GroupDocs.Watermark,則需要這些字型。

添加自訂字型:

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

RUN fc-cache -f -v

此命令將自定義字體(MyFont.ttf)複製到容器中適當的字體目錄並更新字體緩存。

構建和運行應用程序:

剩餘的命令配置 Docker 容器以構建和運行您的 .NET 應用程式 (WebApp),確保在運行時可用自定義字體。

.csproj 中啟用 Unix 支援

由於 .NET 6 在 Linux 上的 System.Drawing.Common 庫存在限制,您需要通過在 .csproj 文件中添加特定設置來啟用 Unix 支持。 有關這些限制的詳細資訊,請參閱 Microsoft documentation

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

這個設置確保 System.Drawing 功能在 Linux 環境中正常運作,這對於在使用 GroupDocs.Watermark 時進行正確的渲染至關重要。

使用自訂字型的最佳實踐

要充分利用此功能,請遵循以下最佳做法:

  1. 整理字型:將您的自訂字型保存在專用資料夾中,以便輕鬆參考。
  2. 驗證字型名稱:確保正確指定字型系列名稱以避免渲染問題。
  3. 在容器化環境中進行測試:使用 Docker 容器在受控的 Linux 基礎環境中測試您的應用程序。

結論

在 GroupDocs.Watermark 中使用自定義字體的能力增強了您對水印設計的控制,使您能夠滿足特定的品牌和樣式要求。通過指定字體文件夾或在 Linux 容器中安裝字體,您可以輕鬆地在任何環境中測試和部署您的應用程序。

提供的 Dockerfile 和 C# 示例作為實施和測試此功能的綜合指南。試試它們,以確保您的水印解決方案靈活、可攜帶並準備投入生產。

獲取免費試用

您可以通过仅下载并安装我们最新版本的 release downloads website 免费试用 GroupDocs.Watermark API。

您還可以獲得一個臨時許可證,以便在沒有任何限制的情況下測試所有圖書館的功能。前往 臨時許可證頁面 以申請臨時許可證。

另請參見

如需更多信息和其他资源,您可能会发现以下链接有用: