How to Insert PDF Header
Insert PDF a header can help readers to get more information about the whole content with a fast speed. PDF header can be simply a title, author’s name, file’s name, a date, page numbering, short description or other text content or even an image. This post will show you how to insert PDF header in .NET with C#. This sample will show you insert PDF header with text and image both.
Tools We Need:
Tool Introduction:
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat.
Download Spire.PDF Here
How to Insert PDF Header with Spire.Doc by Using C#
Step 1 Create a Project
Create a C# project in Visual Studio and add Spire.PDF.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using Spire.Pdf;
namespace SplitPDF
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Create a PDF Document
Spire.PDF enables developers to create PDF documents at will. Here we can create a PDF document as a text document.
//create a pdfDocument
PdfDocument doc = new PdfDocument();
//set the margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
Step 3 Insert PDF Header
Spire.PDF allows users to insert PDF header with both text and image. Use the code below we can easily insert PDF header with text and image. Furthermore, we can use Spire.PDF to set the text styles.
//insert text header in PDF
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
String label = "Demo of Spire.Pdf";
SizeF size = font.MeasureString(label, format);
PdfPageTemplateElement topSpace
= new PdfPageTemplateElement(PdfPageSize.A4.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
float y = topSpace.Height - font.Height - 1;
PdfPen pen = new PdfPen(Color.Black, 0.75f);
topSpace.Graphics.SetTransparency(0.5f);
topSpace.Graphics.DrawLine(pen, margin.Left, y, PdfPageSize.A4.Width - margin.Right, y);
y = y - 1 - size.Height;
topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, PdfPageSize.A4.Width - margin.Right, y, format);
doc.Template.Stamps.Add(topSpace);
//insert image header in PDF
PdfImage headerImage
= PdfImage.FromFile(@"..\..\Header.png");
PointF pageLeftTop = new PointF(-margin.Left, -margin.Top);
PdfPageTemplateElement header = new PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension);
header.Foreground = false;
header.Graphics.SetTransparency(0.5f);
header.Graphics.DrawImage(headerImage, 0, 0);
doc.Template.Stamps.Add(header);
Step 4 Save and Launch
At last, we can use the code below to save the PDF file with PDF header and launch the project to check the effect.
//save file
doc.SaveToFile("Sample.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("FromHTML.pdf");
Effective Screenshot
Tools We Need:
- Visual Studio
- Spire.PDF
Tool Introduction:
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat.
Download Spire.PDF Here
How to Insert PDF Header with Spire.Doc by Using C#
Step 1 Create a Project
Create a C# project in Visual Studio and add Spire.PDF.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using Spire.Pdf;
namespace SplitPDF
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Create a PDF Document
Spire.PDF enables developers to create PDF documents at will. Here we can create a PDF document as a text document.
//create a pdfDocument
PdfDocument doc = new PdfDocument();
//set the margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
Step 3 Insert PDF Header
Spire.PDF allows users to insert PDF header with both text and image. Use the code below we can easily insert PDF header with text and image. Furthermore, we can use Spire.PDF to set the text styles.
//insert text header in PDF
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
String label = "Demo of Spire.Pdf";
SizeF size = font.MeasureString(label, format);
PdfPageTemplateElement topSpace
= new PdfPageTemplateElement(PdfPageSize.A4.Width, margin.Top);
topSpace.Foreground = true;
doc.Template.Top = topSpace;
float y = topSpace.Height - font.Height - 1;
PdfPen pen = new PdfPen(Color.Black, 0.75f);
topSpace.Graphics.SetTransparency(0.5f);
topSpace.Graphics.DrawLine(pen, margin.Left, y, PdfPageSize.A4.Width - margin.Right, y);
y = y - 1 - size.Height;
topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, PdfPageSize.A4.Width - margin.Right, y, format);
doc.Template.Stamps.Add(topSpace);
//insert image header in PDF
PdfImage headerImage
= PdfImage.FromFile(@"..\..\Header.png");
PointF pageLeftTop = new PointF(-margin.Left, -margin.Top);
PdfPageTemplateElement header = new PdfPageTemplateElement(pageLeftTop, headerImage.PhysicalDimension);
header.Foreground = false;
header.Graphics.SetTransparency(0.5f);
header.Graphics.DrawImage(headerImage, 0, 0);
doc.Template.Stamps.Add(header);
Step 4 Save and Launch
At last, we can use the code below to save the PDF file with PDF header and launch the project to check the effect.
//save file
doc.SaveToFile("Sample.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("FromHTML.pdf");
Effective Screenshot
More about Spire.PDF
Spire.PDF can be used on the server-side (ASP.NET or any other environment) or with Windows Forms applications. Click to learn more...
Spire.PDF can be used on the server-side (ASP.NET or any other environment) or with Windows Forms applications. Click to learn more...