Jak dodawać nagłówki i stopki w plikach PDF za pomocą IronPDF

Dodawanie nagłówków i stopek w plikach PDF przy użyciu języka C# i biblioteki IronPDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF pozwala latwo dodawac naglowki i stopki do dokumentow PDF w C# za pomoca metod takich jak AddTextHeaders i AddTextFooters dla prostego tekstu, lub AddHtmlHeaders i AddHtmlFooters dla tresci opartej na HTML z pelnym wsparciem stylow CSS. Ta potężna funkcjonalność jest niezbędna do tworzenia profesjonalnych plików PDF z zachowaniem spójnego wizerunku marki, numeracji stron i metadanych dokumentu.

Chcesz umieścić numery stron, logo firmy lub datę u góry lub u dołu każdej strony w dokumencie PDF? IronPDF ułatwia stosowanie nagłówków i stopek w plikach PDF w projekcie C#. Niezależnie od tego, czy generujesz raporty, faktury czy inne dokumenty biznesowe, nagłówki i stopki stanowią kluczowe elementy nawigacyjne i identyfikacyjne, które zwiększają użyteczność dokumentu.

Szybki start: Dodawanie nagłówków i stopek do plików PDF w języku C#

Bez wysiłku dodawaj nagłówki i stopki do dokumentów PDF za pomocą IronPDF w języku C#. W tym przewodniku pokazano, jak w kilka sekund zastosować nagłówki i stopki tekstowe z numerami stron i niestandardowym tekstem. Uzyj metod AddTextHeaders i AddTextFooters, aby szybko ulepszyc prezentacje PDF. Zapisz zaktualizowany plik PDF z minimalną ilością kodu, zapewniając profesjonalny wygląd dokumentów.

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf
  2. Skopiuj i uruchom ten fragment kodu.

    new IronPdf.ChromePdfRenderer { RenderingOptions = { TextHeader = new IronPdf.TextHeaderFooter { CenterText = "Report • {date}" }, TextFooter = new IronPdf.TextHeaderFooter { RightText = "Page {page} of {total-pages}" } } }
        .RenderHtmlAsPdf("<h1>Hello World!</h1>")
        .SaveAs("withHeadersFooters.pdf");
  3. Wdrożenie do testowania w środowisku produkcyjnym

    Rozpocznij używanie IronPDF w swoim projekcie już dziś z darmową wersją próbną

    arrow pointer

Jak dodać nagłówek/stopkę do tekstu?

Aby utworzyc naglowek/stopke z samym tekstem, zainicjuj obiekt TextHeaderFooter, dodaj pozadany tekst, a nastepnie dodaj obiekt do swojego PDF. Klasa TextHeaderFooter zapewnia prosta metode dodawania stalym elementow tekstowych na wszystkich stronach dokumentu. Ta metoda jest szczególnie przydatna w przypadku prostych nagłówków i stopek, które nie wymagają skomplikowanego formatowania ani stylizacji.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-add-textheaderfooter.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

// Create text footer
TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader);
pdf.AddTextFooters(textFooter);

pdf.SaveAs("addTextHeaderFooter.pdf");
Imports IronPdf

' Instantiate renderer and create PDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Create text header
Dim textHeader As New TextHeaderFooter With {
    .CenterText = "This is the header!"
}

' Create text footer
Dim textFooter As New TextHeaderFooter With {
    .CenterText = "This is the footer!"
}

' Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader)
pdf.AddTextFooters(textFooter)

pdf.SaveAs("addTextHeaderFooter.pdf")
$vbLabelText   $csharpLabel

Jak mogę dodać nagłówki/stopki podczas renderowania?

Alternatywnie, mozna bezposrednio dodac naglowek/stopke za pomoca RenderingOptions renderera. Spowoduje to dodanie nagłówka i stopki tekstu podczas renderowania, co jest bardziej wydajne niż dodawanie ich po utworzeniu pliku PDF. Takie podejście jest zalecane, gdy znasz z góry treść nagłówka i stopki, ponieważ skraca to czas przetwarzania i zapewnia spójne formatowanie od samego początku.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-textheaderfooter.cs
using IronPdf;

// Instantiate renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create header and add to rendering options
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
};


// Create footer and add to rendering options
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Render PDF with header and footer
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
pdf.SaveAs("renderWithTextHeaderFooter.pdf");
Imports IronPdf

' Instantiate renderer
Dim renderer As New ChromePdfRenderer()

' Create header and add to rendering options
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
    .CenterText = "This is the header!"
}

' Create footer and add to rendering options
renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
    .CenterText = "This is the footer!"
}

' Render PDF with header and footer
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
pdf.SaveAs("renderWithTextHeaderFooter.pdf")
$vbLabelText   $csharpLabel

Jak mogę dostosować właściwości tekstu i separatorów?

W klasie TextHeaderFooter mozna ustawic tekst w pozycjach lewa, srodkowa i prawa. Dodatkowo można dostosować rodzaj i rozmiar czcionki tekstu oraz dodać separator o niestandardowym kolorze, konfigurując odpowiednie właściwości. Te opcje dostosowywania pozwalają tworzyć nagłówki i stopki zgodne z wizerunkiem firmy lub wytycznymi dotyczącymi stylu dokumentów. Funkcja linii oddzielającej jest szczególnie przydatna do tworzenia wizualnego rozdzielenia nagłówka/stopki od głównej treści.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-options.cs
using IronPdf;
using IronPdf.Font;
using IronSoftware.Drawing;

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "Center text", // Set the text in the center
    LeftText = "Left text", // Set left-hand side text
    RightText = "Right text", // Set right-hand side text
    Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic, // Set font
    FontSize = 16, // Set font size
    DrawDividerLine = true, // Draw Divider Line
    DrawDividerLineColor = Color.Red, // Set color of divider line
};
Imports IronPdf
Imports IronPdf.Font
Imports IronSoftware.Drawing

' Create text header
Private textHeader As New TextHeaderFooter With {
	.CenterText = "Center text",
	.LeftText = "Left text",
	.RightText = "Right text",
	.Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic,
	.FontSize = 16,
	.DrawDividerLine = True,
	.DrawDividerLineColor = Color.Red
}
$vbLabelText   $csharpLabel

Jak wygląda dostosowany nagłówek tekstu?

Przykład wyrównania tekstu pokazujący opcje pozycjonowania tekstu po lewej, w środku i po prawej stronie

Jakie czcionki są dostępne domyślnie?

Dostępne domyślnie czcionki można sprawdzić w Dokumentacji API IronPDF. IronPDF obsługuje szeroką gamę standardowych czcionek, w tym Arial, Times New Roman, Helvetica, Courier i ich odmiany. Jeśli potrzebujesz niestandardowych czcionek, dowiedz się więcej o zarządzaniu czcionkami w IronPDF.

Jak ustawić marginesy dla nagłówków/stopek tekstu?

Domyślnie nagłówki i stopki tekstu w IronPDF mają predefiniowane marginesy. Jesli chcesz, aby naglowek tekstu zajmowal cala szerokosc dokumentu PDF, ustaw wartosci marginesow na 0. Mozna to osiagnac, ustawiajac marginesy bezposrednio w funkcjach AddTextHeaders i AddTextFooters lub poprzez RenderingOptions w ChromePdfRenderer. Zrozumienie kontroli marginesów ma kluczowe znaczenie dla uzyskania układów o idealnej rozdzielczości, zwłaszcza podczas pracy z niestandardowymi rozmiarami papieru.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-margins.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

pdf.AddTextHeaders(header, 35, 30, 25); // Left Margin = 35, Right Margin  = 30, Top Margin = 25
pdf.AddTextFooters(footer, 35, 30, 25); // Margin values are in mm
Imports IronPdf

' Instantiate renderer and create PDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

Dim header As New TextHeaderFooter With {
    .CenterText = "This is the header!"
}

Dim footer As New TextHeaderFooter With {
    .CenterText = "This is the footer!"
}

pdf.AddTextHeaders(header, 35, 30, 25) ' Left Margin = 35, Right Margin = 30, Top Margin = 25
pdf.AddTextFooters(footer, 35, 30, 25) ' Margin values are in mm
$vbLabelText   $csharpLabel

Jak zastosować marginesy za pomocą opcji renderowania?

Jesli dodasz wartosci marginesow w RenderingOptions z ChromePdfRenderer, marginesy te beda rowniez dotyczyly naglowka i stopki. Takie podejście zapewnia scentralizowany sposób zarządzania marginesami w całym dokumencie, w tym w nagłówkach, stopkach i głównej treści. Aby uzyskać więcej informacji na temat zaawansowanego dostosowywania marginesów, zapoznaj się z naszym przewodnikiem dotyczącym ustawiania niestandardowych marginesów.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-rendering-options-margins.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();

TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Margin values are in mm
renderer.RenderingOptions.MarginRight = 30;
renderer.RenderingOptions.MarginLeft = 30;
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Add header and footer to renderer
renderer.RenderingOptions.TextHeader = header;
renderer.RenderingOptions.TextFooter = footer;

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
Imports IronPdf

' Instantiate renderer and create PDF
Private renderer As New ChromePdfRenderer()

Private header As New TextHeaderFooter With {.CenterText = "This is the header!"}

Private footer As New TextHeaderFooter With {.CenterText = "This is the footer!"}

' Margin values are in mm
renderer.RenderingOptions.MarginRight = 30
renderer.RenderingOptions.MarginLeft = 30
renderer.RenderingOptions.MarginTop = 25
renderer.RenderingOptions.MarginBottom = 25

' Add header and footer to renderer
renderer.RenderingOptions.TextHeader = header
renderer.RenderingOptions.TextFooter = footer

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
$vbLabelText   $csharpLabel

Dlaczego powinienem unikac UseMarginsOnHeaderAndFooter?

Wlasnosc UseMarginsOnHeaderAndFooter w RenderingOptions nie jest odpowiednia dla tego przypadku uzycia. Zastosowano te same wartości marginesów dla nagłówka, stopki i głównej treści, co może spowodować, że nagłówek będzie zachodził na treść dokumentu. Ta wlasnosc jest glownie przeznaczona do stosowania naglowkow i stopek do istniejacych PDF za pomoca metody AddTextHeadersAndFooters. Aby uzyskać lepszą kontrolę nad układem, warto rozważyć użycie podziałów stron do zarządzania przepływem treści.

Czym jest dynamiczne dostosowywanie marginesów?

Problem stanowiły stałe marginesy, gdy zawartość nagłówków różniła się w poszczególnych dokumentach. Konieczne było dostosowanie nie tylko marginesów nagłówka i stopki, ale także głównego marginesu HTML, aby uwzględnić różne rozmiary nagłówków i stopek. W związku z tym wdrożyliśmy funkcję dynamicznego dostosowywania marginesów, w której wysokość nagłówka i stopki dostosowuje się dynamicznie w zależności od treści, a główny kod HTML odpowiednio się przesuwa. Ta funkcja jest szczególnie przydatna podczas pracy z responsywnymi układami CSS. Użyj poniższego kodu, aby wypróbować tę funkcję:

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-dynamic-marigns.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = @"<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>
                    <h1>Example header</h1> <br>
                    <p>Header content</p>
                    </div>",
    // Enable the dynamic height feature
    MaxHeight = HtmlHeaderFooter.FragmentHeight,
};

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");
pdf.SaveAs("dynamicHeaderSize.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
	.HtmlFragment = "<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>
                    <h1>Example header</h1> <br>
                    <p>Header content</p>
                    </div>",
	.MaxHeight = HtmlHeaderFooter.FragmentHeight
}

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")
pdf.SaveAs("dynamicHeaderSize.pdf")
$vbLabelText   $csharpLabel

Jak dodać metadane do nagłówków/stopek tekstu?

Możesz łatwo dodać metadane, takie jak numery stron, datę i tytuł pliku PDF, umieszczając w tekście ciągi znaków zastępczych. Te symbole zastępcze są automatycznie zastępowane odpowiednimi wartościami podczas renderowania pliku PDF. Ta funkcja jest niezbędna do tworzenia dynamicznych nagłówków i stopek, które aktualizują się automatycznie na podstawie właściwości dokumentu. Oto wszystkie dostępne opcje metadanych:

  • {page}: Aktualny numer strony.
  • {total-pages}: Caly numer strony.
  • {url}: Web URL, z ktorego dokument PDF zostal wyrenderowany.
  • {date}: Aktualna data.
  • {time}: Aktualny czas.
  • {html-title}: HTML title okreslony w tagu title w HTML.
  • {pdf-title}: PDF title okreslony w metadanych PDF.

Których symboli zastępczych powinienem używać najczęściej?

Aby dowiedziec sie wiecej o {page} i {total-pages}, odwiedz IronPDF Page Numbers Guide. Te substytuty sa najczesciej uzywane, poniewaz dostarczaja niezbednych informacji o nawigacji. Zastępcze symbole daty i godziny są szczególnie przydatne w dokumentach wymagających śledzenia znaczników czasu, takich jak raporty lub faktury.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-mail-merge.cs
using IronPdf;

// Create header and footer
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "{page} of {total-pages}",
    LeftText = "Today's date: {date}",
    RightText = "The time: {time}",
};

TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "Current URL: {url}",
    LeftText = "Title of the HTML: {html-title}",
    RightText = "Title of the PDF: {pdf-title}",
};
Imports IronPdf

' Create header and footer
Private textHeader As New TextHeaderFooter With {
	.CenterText = "{page} of {total-pages}",
	.LeftText = "Today's date: {date}",
	.RightText = "The time: {time}"
}

Private textFooter As New TextHeaderFooter With {
	.CenterText = "Current URL: {url}",
	.LeftText = "Title of the HTML: {html-title}",
	.RightText = "Title of the PDF: {pdf-title}"
}
$vbLabelText   $csharpLabel

Jak dodać nagłówki/stopki HTML?

Możesz dodatkowo dostosować nagłówek/stopkę, korzystając z HTML i CSS. Aby utworzyc naglowek/stopke HTML, uzyj klasy HtmlHeaderFooter. Takie podejście zapewnia maksymalną elastyczność, umożliwiając umieszczanie obrazów, złożonych układów oraz stylizowanych treści w nagłówkach i stopkach. Jesli chcesz zachowac style CSS z arkusza stylow CSS, ustaw LoadStylesAndCSSFromMainHtmlDocument = true w wlasciwosciach klasy. Jest to szczególnie przydatne podczas pracy z czcionkami internetowymi i ikonami.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter.cs
using IronPdf;

string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create header and footer
HtmlHeaderFooter htmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Add to PDF
pdf.AddHtmlHeaders(htmlHeader);
pdf.AddHtmlFooters(htmlFooter);
Imports IronPdf

Private headerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>"

Private footerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>"

' Instantiate renderer and create PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Create header and footer
Private htmlHeader As New HtmlHeaderFooter With {
	.HtmlFragment = headerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

Private htmlFooter As New HtmlHeaderFooter With {
	.HtmlFragment = footerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

' Add to PDF
pdf.AddHtmlHeaders(htmlHeader)
pdf.AddHtmlFooters(htmlFooter)
$vbLabelText   $csharpLabel

Jak kontrolować marginesy nagłówka/stopki w HTML?

Podobnie jak naglowki i stopki tekstowe, metody AddHtmlHeaders i AddHtmlFooters maja wstepnie zdefiniowane marginesy. Aby zastosować niestandardowe marginesy, należy użyć przeciążenia funkcji z określonymi wartościami marginesów. Aby zawrzeć całą treść bez żadnych marginesów, należy ustawić marginesy w funkcji overload na 0. Ten poziom kontroli jest niezbędny podczas tworzenia profesjonalnych dokumentów o określonych wymaganiach dotyczących układu.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter-margins.cs
// Add to PDF
pdf.AddHtmlHeaders(header, 0, 0, 0);
pdf.AddHtmlFooters(footer, 0, 0, 0);
' Add to PDF
pdf.AddHtmlHeaders(header, 0, 0, 0)
pdf.AddHtmlFooters(footer, 0, 0, 0)
$vbLabelText   $csharpLabel

Czy podczas renderowania mogę dodawać nagłówki/stopki HTML?

Dodawanie naglowkow i stopek mozna rowniez wykonac bezposrednio za pomoca RenderingOptions renderera. Dodaje to nagłówek i stopkę HTML podczas procesu renderowania, co jest bardziej wydajne niż przetwarzanie końcowe. Ta metoda jest szczególnie przydatna podczas generowania plików PDF z plików HTML lub konwersji adresów URL do formatu PDF.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter.cs
using IronPdf;

string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create header and footer
HtmlHeaderFooter htmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Add to PDF
pdf.AddHtmlHeaders(htmlHeader);
pdf.AddHtmlFooters(htmlFooter);
Imports IronPdf

Private headerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>"

Private footerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>"

' Instantiate renderer and create PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Create header and footer
Private htmlHeader As New HtmlHeaderFooter With {
	.HtmlFragment = headerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

Private htmlFooter As New HtmlHeaderFooter With {
	.HtmlFragment = footerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

' Add to PDF
pdf.AddHtmlHeaders(htmlHeader)
pdf.AddHtmlFooters(htmlFooter)
$vbLabelText   $csharpLabel

Kiedy należy używać nagłówków/stopek tekstowych, a kiedy HTML?

Przy podejmowaniu decyzji między nagłówkami/stopkami w formacie tekstowym a HTML należy rozważyć związane z tym kompromisy. Jeśli zależy Ci na szybszym renderowaniu plików PDF, wybierz opcję "Nagłówki/stopki tekstowe". Jeśli dostosowywanie i stylizacja są istotne, wybierz nagłówki/stopki HTML. Różnica w czasie renderowania między nagłówkami/stopkami w formacie tekstowym a HTML jest minimalna, gdy nagłówki/stopki HTML zawierają ograniczoną zawartość. Jednak rośnie wraz ze wzrostem rozmiaru i liczby zasobów w nagłówkach/stopkach HTML.

Jakie są konsekwencje dla wydajności?

Nagłówki i stopki tekstu renderują się szybciej, ponieważ nie wymagają analizy HTML ani przetwarzania CSS. Nagłówki/stopki HTML zapewniają większą elastyczność, ale wymagają dodatkowego czasu renderowania proporcjonalnego do ich złożoności. Podczas pracy z dużymi dokumentami lub przetwarzania wsadowego różnica w wydajności staje się bardziej zauważalna. Aby uzyskać optymalną wydajność w scenariuszach wymagających przetwarzania dużych ilości danych, zapoznaj się z naszym przewodnikiem dotyczącym asynchronicznego generowania plików PDF.

Gotowy, aby sprawdzić, co jeszcze możesz zrobić? Zapoznaj się z naszą stroną z samouczkami tutaj: Tworzenie plików PDF

Często Zadawane Pytania

How do I add text headers and footers to a PDF in C#?

With IronPDF, you can add text headers and footers using the AddTextHeaders and AddTextFooters methods. Simply instantiate a TextHeaderFooter object, add your desired text, and apply it to your PDF. This provides a straightforward way to add consistent text elements like page numbers or document titles across all pages.

Can I include page numbers in my PDF headers or footers?

Yes, IronPDF supports dynamic page numbering using special placeholders. You can use {page} for the current page number and {total-pages} for the total page count in your TextHeaderFooter objects. For example, setting RightText = "Page {page} of {total-pages}" will automatically display the correct page numbers on each page.

Is it possible to add HTML-based headers and footers with CSS styling?

Absolutely! IronPDF provides AddHtmlHeaders and AddHtmlFooters methods that allow you to add HTML content with full CSS styling support. This enables you to create complex headers and footers with formatted text, images, and custom styling to match your brand guidelines.

What's the most efficient way to add headers and footers to PDFs?

The most efficient approach is to add headers and footers during the rendering process using IronPDF's RenderingOptions. By configuring TextHeader and TextFooter properties in the ChromePdfRenderer before rendering, you reduce processing time compared to adding them after the PDF is created.

Can I add different content to the left, center, and right sections of headers/footers?

Yes, the TextHeaderFooter class in IronPDF provides LeftText, CenterText, and RightText properties, allowing you to place different content in each section. This gives you flexibility to organize information like dates on the left, titles in the center, and page numbers on the right.

How do I add a company logo to my PDF headers?

To add a company logo to your PDF headers, use the AddHtmlHeaders method in IronPDF. You can include an image tag in your HTML content pointing to your logo file, along with any additional styling or positioning using CSS to ensure it appears exactly where you want it.

Can I include dates in my PDF headers and footers?

Yes, IronPDF supports dynamic date insertion using the {date} placeholder in TextHeaderFooter objects. When you include {date} in your header or footer text, it will automatically be replaced with the current date when the PDF is generated.

Jordi Bardia
Inżynier oprogramowania
Jordi jest najbardziej biegły w Pythonie, C# i C++. Kiedy nie wykorzystuje swoich umiejętności w Iron Software, programuje gry. Dzieląc odpowiedzialność za testowanie produktów, rozwój produktów i badania, Jordi wnosi ogromną wartość do ciągłej poprawy produktów. Różnorodne doświadczenia ...
Czytaj więcej
Gotowy, aby rozpocząć?
Nuget Pliki do pobrania 18,135,201 | Wersja: 2026.4 just released
Still Scrolling Icon

Wciąż przewijasz?

Czy chcesz szybko dowodu? PM > Install-Package IronPdf
Uruchom przykład i zobacz, jak Twój kod HTML zamienia się w plik PDF.