管理页眉和页脚

在 PDF 文件中添加页眉

PdfFileStamp 类允许您在 PDF 文件中添加页眉。为了添加页眉,您首先需要创建 PdfFileStamp 类的对象。您可以使用 FormattedText 类格式化页眉文本。一旦您准备好在文件中添加页眉,您需要调用 PdfFileStamp 类的 AddHeader 方法。您还需要在 AddHeader 方法中至少指定顶部边距。最后,使用 PdfFileStamp 类的 Close 方法保存输出的 PDF 文件。以下代码片段向您展示如何在 PDF 文件中添加页眉。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddHeader()
{
    // 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 + "sample.pdf");

        // Create formatted text for the header
        var formattedText = new Aspose.Pdf.Facades.FormattedText(
            "Aspose - Your File Format Experts!",
            System.Drawing.Color.Yellow,
            System.Drawing.Color.Black,
            Aspose.Pdf.Facades.FontStyle.Courier,
            Aspose.Pdf.Facades.EncodingType.Winansi,
            false,
            14);

        // Add header
        fileStamp.AddHeader(formattedText, 10);

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

在 PDF 文件中添加页脚

PdfFileStamp 类允许您在 PDF 文件中添加页脚。为了添加页脚,您首先需要创建 PdfFileStamp 类的对象。您可以使用 FormattedText 类格式化页脚文本。一旦您准备好在文件中添加页脚,您需要调用 PdfFileStamp 类的 AddFooter 方法。您还需要在 AddFooter 方法中至少指定底部边距。最后,使用 PdfFileStamp 类的 Close 方法保存输出的 PDF 文件。以下代码片段向您展示如何在 PDF 文件中添加页脚。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddFooter()
{
    // 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 + "sample.pdf");

        // Create formatted text for the footer
        var formattedText = new Aspose.Pdf.Facades.FormattedText(
            "Aspose - Your File Format Experts!",
            System.Drawing.Color.Blue,
            System.Drawing.Color.Gray,
            Aspose.Pdf.Facades.FontStyle.Courier,
            Aspose.Pdf.Facades.EncodingType.Winansi,
            false,
            14);

        // Add footer
        fileStamp.AddFooter(formattedText, 10);

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

在现有 PDF 文件的页眉中添加图像

PdfFileStamp 类允许您在 PDF 文件的页眉中添加图像。为了在页眉中添加图像,您首先需要创建 PdfFileStamp 类的对象。之后,您需要调用 PdfFileStamp 类的 AddHeader 方法。您可以将图像传递给 AddHeader 方法。最后,使用 PdfFileStamp 类的 Close 方法保存输出的 PDF 文件。以下代码片段向您展示如何在 PDF 文件的页眉中添加图像。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImageHeader()
{
    // 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 + "sample.pdf");

        // Add Header
        using (var fs = new FileStream(dataDir + "ImageHeader.png", FileMode.Open))
        {
            fileStamp.AddHeader(fs, 10);  // Add image header with position offset

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

在现有 PDF 文件的页脚中添加图像

PdfFileStamp 类允许您在 PDF 文件的页脚中添加图像。为了在页脚中添加图像,您首先需要创建 PdfFileStamp 类的对象。之后,您需要调用 PdfFileStamp 类的 AddFooter 方法。您可以将图像传递给 AddFooter 方法。最后,使用 PdfFileStamp 类的 Close 方法保存输出的 PDF 文件。以下代码片段向您展示如何在 PDF 文件的页脚中添加图像。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImageFooter()
{
    // 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 + "sample.pdf");

        // Add footer
        using (var fs = new FileStream(dataDir + "ImageFooter.png", FileMode.Open))
        {
            fileStamp.AddFooter(fs, 10);  // Add image footer with position offset

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