添加 PDF 页面印章

在 PDF 文件的所有页面上添加 PDF 页面印章

PdfFileStamp 类允许您在 PDF 文件的所有页面上添加 PDF 页面印章。为了添加 PDF 页面印章,您首先需要创建 PdfFileStampStamp 类的对象。您还需要使用 Stamp 类的 PdfFileStamp 方法创建 PDF 页面印章。您还可以使用 Stamp 对象设置其他属性,如原点、旋转、背景等。然后,您可以使用 PdfFileStamp 类的 AddStamp 方法将印章添加到 PDF 文件中。最后,使用 PdfFileStamp 类的 Close 方法保存输出 PDF 文件。以下代码片段演示了如何在 PDF 文件的所有页面上添加 PDF 页面印章。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddPageStampOnAllPages()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PdfFileStamp object
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Bind PDF document
        fileStamp.BindPdf(dataDir + "SourcePDF.pdf");

        // Create stamp
        var stamp = new Aspose.Pdf.Facades.Stamp();
        // Bind PDF document
        stamp.BindPdf(dataDir + "AddPageStampOnAllPages.pdf", 1);
        stamp.SetOrigin(20, 20);
        stamp.Rotation = 90.0F;
        stamp.IsBackground = true;

        // Add stamp to PDF file
        fileStamp.AddStamp(stamp);

        // Save PDF document
        fileStamp.Save(dataDir + "PageStampOnAllPages_out.pdf");
    }
}

在 PDF 文件的特定页面上添加 PDF 页面印章

PdfFileStamp 类允许您在 PDF 文件的特定页面上添加 PDF 页面印章。为了添加 PDF 页面印章,您首先需要创建 PdfFileStampStamp 类的对象。您还需要使用 Stamp 类的 BindPdf 方法创建 PDF 页面印章。您还可以使用 Stamp 对象设置其他属性,如原点、旋转、背景等。由于您想在 PDF 文件的特定页面上添加 PDF 页面印章,您还需要设置 Stamp 类的 Pages 属性。此属性需要一个整数数组,包含您想要添加印章的页面编号。然后,您可以使用 PdfFileStamp 类的 AddStamp 方法将印章添加到 PDF 文件中。最后,使用 PdfFileStamp 类的 Close 方法保存输出 PDF 文件。以下代码片段演示了如何在 PDF 文件的特定页面上添加 PDF 页面印章。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddPageStampOnCertainPages()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PdfFileStamp object
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Bind PDF document
        fileStamp.BindPdf(dataDir + "SourcePDF.pdf");

        // Create stamp
        var stamp = new Aspose.Pdf.Facades.Stamp();
        // Bind PDF document
        stamp.BindPdf(dataDir + "PageStampOnCertainPages.pdf", 1);
        stamp.SetOrigin(20, 20);
        stamp.Rotation = 90.0F;
        stamp.IsBackground = true;
        stamp.Pages = new[] { 1, 3 };  // Apply stamp to specific pages (1 and 3)

        // Add stamp to PDF file
        fileStamp.AddStamp(stamp);

        // Save PDF document
        fileStamp.Save(dataDir + "PageStampOnCertainPages_out.pdf");
    }
}

在 PDF 文件中添加页码

PdfFileStamp 类允许您在 PDF 文件中添加页码。为了添加页码,您首先需要创建 PdfFileStamp 类的对象。如果您想以“第 X 页,共 N 页”的形式显示页码,其中 X 是当前页码,N 是 PDF 文件中的总页数,则您首先需要使用 PdfFileInfo 类的 NumberOfpages 属性获取页数。为了获取当前页码,您可以在文本中使用 # 符号。您可以使用 FormattedText 类格式化页码文本。如果您想从特定数字开始页码编号,则可以设置 StartingNumber 属性。一旦您准备好在文件中添加页码,您需要调用 PdfFileStamp 类的 AddPageNumber 方法。最后,使用 PdfFileStamp 类的 Close 方法保存输出 PDF 文件。以下代码片段演示了如何在 PDF 文件中添加页码。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddPageNumberInPdfFile()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PdfFileStamp object
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Bind PDF document
        fileStamp.BindPdf(dataDir + "StampPDF.pdf");

        // Get total number of pages
        int totalPages = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "StampPDF.pdf").NumberOfPages;

        // Create formatted text for page number
        var formattedText = new Aspose.Pdf.Facades.FormattedText($"Page # of {totalPages}",
            System.Drawing.Color.AntiqueWhite,
            System.Drawing.Color.Gray,
            Aspose.Pdf.Facades.FontStyle.TimesBoldItalic,
            Aspose.Pdf.Facades.EncodingType.Winansi,
            false, 12);

        // Set starting number for first page; you might want to start from 2 or more
        fileStamp.StartingNumber = 1;
        // Add page number in upper right corner
        fileStamp.AddPageNumber(formattedText, (int)PageNumPosition.PosUpperRight);

        // Save PDF document
        fileStamp.Save(dataDir + "AddPageNumber_out.pdf");
    }
}

// Add PDF Page Numbers
public enum PageNumPosition
{
    PosBottomMiddle, PosBottomRight, PosUpperRight, PosSidesRight, PosUpperMiddle, PosBottomLeft, PosSidesLeft, PosUpperLeft
}

自定义编号样式

PdfFileStamp 类提供了将页码信息作为印章对象添加到 PDF 文档中的功能。在此版本之前,该类仅支持 1,2,3,4 作为页码样式。然而,一些客户要求在 PDF 文档中放置页码印章时使用自定义编号样式。为了满足这一要求,引入了 NumberingStyle 属性,该属性接受来自 NumberingStyle 枚举的值。以下是该枚举中提供的值。

  • 小写字母。
  • 大写字母。
  • 阿拉伯数字。
  • 小写罗马数字。
  • 大写罗马数字。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddCustomPageNumberInPdfFile()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();

    // Create PdfFileStamp object
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Bind PDF document
        fileStamp.BindPdf(dataDir + "StampPDF.pdf");

        // Get total number of pages
        int totalPages = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "StampPDF.pdf").NumberOfPages;

        // Create formatted text for page number
        var formattedText = new Aspose.Pdf.Facades.FormattedText($"Page # of {totalPages}",
            System.Drawing.Color.AntiqueWhite,
            System.Drawing.Color.Gray,
            Aspose.Pdf.Facades.FontStyle.TimesBoldItalic,
            Aspose.Pdf.Facades.EncodingType.Winansi,
            false, 12);

        // Specify numbering style as Numerals Roman UpperCase
        fileStamp.NumberingStyle = Aspose.Pdf.NumberingStyle.NumeralsRomanUppercase;

        // Set starting number for first page; you might want to start from 2 or more
        fileStamp.StartingNumber = 1;

        // Add page number in upper right corner
        fileStamp.AddPageNumber(formattedText, (int)PageNumPosition.PosUpperRight);

        // Save PDF document
        fileStamp.Save(dataDir + "AddCustomPageNumber_out.pdf");
    }
}

// Add PDF Page Numbers
public enum PageNumPosition
{
    PosBottomMiddle, PosBottomRight, PosUpperRight, PosSidesRight, PosUpperMiddle, PosBottomLeft, PosSidesLeft, PosUpperLeft
}