Cabeçalho ou Rodapé da Imagem Sem Aumento do Tamanho do Arquivo
Adicionar um logotipo Base64 a um cabeçalho ou rodapé de PDF pode aumentar o tamanho do arquivo de saída, porque a imagem é reembutida em cada página. Divida o cabeçalho ou rodapé em uma camada de imagem estática e uma camada de texto dinâmica para que a imagem seja incorporada uma vez enquanto o conteúdo por página permanece pequeno.
Solução
1. Divida o Cabeçalho ou Rodapé em Dois Fragmentos HTML
Separe o conteúdo pela frequência com que ele muda. Coloque a imagem e qualquer texto que nunca mude em um fragmento estático e coloque conteúdo por página, como {page}, em um fragmento dinâmico. Dê a ambos os fragmentos o mesmo layout de tabela, com larguras de célula correspondentes, para que o texto dinâmico chegue à posição correta sobre a camada estática.
2. Adicione o Fragmento Estático Primeiro
Chame AddHtmlHeadersAndFooters apenas com o fragmento estático. A imagem Base64 é renderizada e embutida uma vez.
3. Adicione o Fragmento Dinâmico em uma Segunda Passagem
Chame AddHtmlHeadersAndFooters novamente apenas com o fragmento dinâmico. Esta passagem não contém imagem, então o conteúdo por página permanece pequeno. Renderizar as partes estáticas e dinâmicas juntas faz com que o mecanismo trate todo o HTML como dinâmico e reembuta a imagem em cada página, o que faz o arquivo crescer.
using IronPdf;
var pdf = PdfDocument.FromFile("input.pdf");
// Static layer: holds the Base64 image. The page-number cell is left empty.
string staticFooter = @"
<table style='width:100%; table-layout:fixed;'>
<tr>
<td style='width:20%;'>
<img src='data:image/png;base64,iVBORw0KGg...' style='width:150px;' />
</td>
<td style='width:80%; text-align:right;'></td>
</tr>
</table>";
// Dynamic layer: holds only {page}. Same layout, but no image.
string dynamicFooter = $@"
<table style='width:100%; table-layout:fixed;'>
<tr>
<td style='width:20%;'></td>
<td style='width:80%; text-align:right; font-size:10px;'>Page {{page}} of {pdf.PageCount}</td>
</tr>
</table>";
// Pass 1: add the static image footer (image embedded once).
pdf.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlFooter = new HtmlHeaderFooter { HtmlFragment = staticFooter, MaxHeight = 40 }
});
// Pass 2: add the dynamic page-number footer (no image, stays small).
pdf.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlFooter = new HtmlHeaderFooter { HtmlFragment = dynamicFooter, MaxHeight = 40 }
});
pdf.SaveAs("output.pdf");Imports IronPdf
Dim pdf = PdfDocument.FromFile("input.pdf")
' Static layer: holds the Base64 image. The page-number cell is left empty.
Dim staticFooter As String = "
<table style='width:100%; table-layout:fixed;'>
<tr>
<td style='width:20%;'>
<img src='data:image/png;base64,iVBORw0KGg...' style='width:150px;' />
</td>
<td style='width:80%; text-align:right;'></td>
</tr>
</table>"
' Dynamic layer: holds only {page}. Same layout, but no image.
Dim dynamicFooter As String = $"
<table style='width:100%; table-layout:fixed;'>
<tr>
<td style='width:20%;'></td>
<td style='width:80%; text-align:right; font-size:10px;'>Page {{page}} of {pdf.PageCount}</td>
</tr>
</table>"
' Pass 1: add the static image footer (image embedded once).
pdf.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlFooter = New HtmlHeaderFooter With {.HtmlFragment = staticFooter, .MaxHeight = 40}
})
' Pass 2: add the dynamic page-number footer (no image, stays small).
pdf.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlFooter = New HtmlHeaderFooter With {.HtmlFragment = dynamicFooter, .MaxHeight = 40}
})
pdf.SaveAs("output.pdf")Imports IronPdf
Dim pdf = PdfDocument.FromFile("input.pdf")
' Static layer: holds the Base64 image. The page-number cell is left empty.
Dim staticFooter As String = "
<table style='width:100%; table-layout:fixed;'>
<tr>
<td style='width:20%;'>
<img src='data:image/png;base64,iVBORw0KGg...' style='width:150px;' />
</td>
<td style='width:80%; text-align:right;'></td>
</tr>
</table>"
' Dynamic layer: holds only {page}. Same layout, but no image.
Dim dynamicFooter As String = $"
<table style='width:100%; table-layout:fixed;'>
<tr>
<td style='width:20%;'></td>
<td style='width:80%; text-align:right; font-size:10px;'>Page {{page}} of {pdf.PageCount}</td>
</tr>
</table>"
' Pass 1: add the static image footer (image embedded once).
pdf.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlFooter = New HtmlHeaderFooter With {.HtmlFragment = staticFooter, .MaxHeight = 40}
})
' Pass 2: add the dynamic page-number footer (no image, stays small).
pdf.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlFooter = New HtmlHeaderFooter With {.HtmlFragment = dynamicFooter, .MaxHeight = 40}
})
pdf.SaveAs("output.pdf")A primeira passagem carrega a imagem Base64 em staticFooter; a segunda passagem carrega apenas Page {page} of ... em dynamicFooter. Ambas as tabelas utilizam larguras de célula idênticas para que o número da página se alinhe sobre a camada estática.
4. (Opcional) Referencie uma Imagem Externa ou SVG
Para reduzir ainda mais o resultado, aponte a tag <img> para um arquivo de imagem externo ou use um SVG em vez de incorporar a imagem como uma string Base64.
Notas e Limitações
- Layout correspondente necessário: ambos os fragmentos devem compartilhar a mesma estrutura de tabela e larguras de célula, ou o texto dinâmico, como o número da página, não se alinhará sobre a camada estática.
- Menor que Base64: um arquivo de imagem externo ou um SVG pode reduzir ainda mais o tamanho do arquivo do que uma string Base64.
- Cabeçalhos também: a mesma técnica se aplica aos cabeçalhos. Mantenha a imagem na passagem estática e coloque qualquer texto de cabeçalho dinâmico na segunda passagem.
v2026.6.1, o IronPDF trata o cabeçalho e o rodapé separadamente. Se a imagem estiver no cabeçalho e {page} estiver apenas no rodapé, ou vice-versa, a imagem é incorporada uma vez e você não precisa de duas passagens. Use o método de duas passagens acima quando a imagem e {page} estiverem no mesmo cabeçalho ou no mesmo rodapé.Curtis Chau é bacharel em Ciência da Computação (Universidade Carleton) e se especializa em desenvolvimento front-end, com experiência em Node.js, TypeScript, JavaScript e React. Apaixonado por criar interfaces de usuário intuitivas e esteticamente agradáveis, Curtis gosta de trabalhar com frameworks modernos e criar manuais bem estruturados e visualmente atraentes.