.NET (1322)
Children categories
Spire.PDF supports to embed image and grid into a grid cell. We've introduced how to embed an image into a grid cell in the article - How to Insert an Image to PDF Grid Cell in C#, this article is going to show you how to embed a grid into a grid cell in PDF using Spire.PDF.
Detail steps:
Step 1: Create a PDF document and add a page to it.
PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add();
Step 2: Create a PDF grid.
//Create a grid PdfGrid grid = new PdfGrid(); //Add two rows PdfGridRow row1 = grid.Rows.Add(); PdfGridRow row2 = grid.Rows.Add(); //Set the Top and Bottom cell padding grid.Style.CellPadding.Top = 5f; grid.Style.CellPadding.Bottom = 5f; //Add two columns grid.Columns.Add(2); //Set columns' width grid.Columns[0].Width = 120f; grid.Columns[1].Width = 120f;
Step 3: Create another PDF grid to embed.
//Create another grid PdfGrid embedGrid = new PdfGrid(); //Add a row PdfGridRow newRow = embedGrid.Rows.Add(); //Add two columns embedGrid.Columns.Add(2); //Set columns' width embedGrid.Columns[0].Width = 50f; embedGrid.Columns[1].Width = 50f;
Step 4: Assign values to the cells of the embed grid and the grid, and set formatting.
//Create a PDFStringFormat instance PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); //Assign values to the cells of the embedGrid and set formatting newRow.Cells[0].Value = "Spire.Doc"; newRow.Cells[0].StringFormat = stringFormat; newRow.Cells[1].Value = "Spire.PDF"; newRow.Cells[1].StringFormat = stringFormat; //Assign values to the cells of the grid and set formatting row1.Cells[0].Value = "Customer's Name"; row1.Cells[0].StringFormat = stringFormat; row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray; row1.Cells[1].Value = "Product(s)"; row1.Cells[1].StringFormat = stringFormat; row1.Cells[1].Style.BackgroundBrush = PdfBrushes.Gray; row2.Cells[0].Value = "Michael"; row2.Cells[0].StringFormat = stringFormat; //Assign the embedGrid to a cell of the grid row2.Cells[1].Value = embedGrid; row2.Cells[1].StringFormat = stringFormat;
Step 5: Draw the grid to the new added page.
grid.Draw(page, new PointF(0f, 50f));
Step 6: Save the document.
pdf.SaveToFile("EmbedGridInCell.pdf");
Screenshot:

Full code:
using Spire.Pdf.Grid;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.Graphics;
namespace Embed_a_Grid_in_a_Grid_Cell_in_PDF
{
class Program
{
static void Main(string[] args)
{
//Create a pdf document
PdfDocument pdf = new PdfDocument();
//Add a page
PdfPageBase page = pdf.Pages.Add();
//Create a pdf grid
PdfGrid grid = new PdfGrid();
//Add two rows
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
//Set Top and Bottom cell padding of the grid
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;
//Add two columns
grid.Columns.Add(2);
//Set the columns’ width
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 120f;
//Create another grid to embed
PdfGrid embedGrid = new PdfGrid();
//Add a row
PdfGridRow newRow = embedGrid.Rows.Add();
//Add two columns
embedGrid.Columns.Add(2);
//Set the columns’ width
embedGrid.Columns[0].Width = 50f;
embedGrid.Columns[1].Width = 50f;
//Create a PDFStringFormat instance
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
//Assign values to the cells of the embedGrid and set formatting
newRow.Cells[0].Value = "Spire.Doc";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = "Spire.PDF";
newRow.Cells[1].StringFormat = stringFormat;
//Assign values to the cells of the grid and set formatting
row1.Cells[0].Value = "Customer's Name";
row1.Cells[0].StringFormat = stringFormat;
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Gray;
row1.Cells[1].Value = "Product(s)";
row1.Cells[1].StringFormat = stringFormat;
row1.Cells[1].Style.BackgroundBrush = PdfBrushes.Gray;
row2.Cells[0].Value = "Michael";
row2.Cells[0].StringFormat = stringFormat;
//Assign the embedGrid to the cell of the grid
row2.Cells[1].Value = embedGrid;
row2.Cells[1].StringFormat = stringFormat;
//Draw the grid to the new added page
grid.Draw(page, new PointF(0f, 50f));
//Save the pdf document
pdf.SaveToFile("EmbedGridInCell.pdf");
}
}
}
VBA (Visual Basic for Applications) macros are small programs that can be embedded within Microsoft Word documents to automate repetitive tasks, add interactivity to documents, and perform other useful functions. While macros can be beneficial in many situations, they can also pose a security risk if the code is malicious or contains malware. By removing VBA macros from Word documents, you can reduce the risk of security breaches and malware infections. In this article, you will learn how to detect and remove VBA macros from Word documents in C# and VB.NET using Spire.Doc for .NET library.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Detect and Remove VBA Macros from Word Documents in C# and VB.NET
You can use the Document.IsContainMacro property to detect whether a Word document contains VBA macros. If any macros are detected, you can use the Document.ClearMacros() method to easily remove them from the document.
The following steps show how to detect and remove VBA macros from a Word document using Spire.Doc for .NET:
- Initialize an instance of the Document class.
- Load a Word document using the Document.LoadFromFile(string fileName) method.
- Detect if the document contains VBA macros using the Document.IsContainMacro property.
- If any macros are detected, remove them from the document using Document.ClearMacros() method.
- Save the result document using Document.SaveToFile(string fileName, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Doc;
namespace RemoveVBAMacros
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Document class
Document document = new Document();
//Load a Word document
document.LoadFromFile("Input.docm");
//Detect if the document contains macros
if (document.IsContainMacro)
{
//Remove the macros from the document
document.ClearMacros();
}
//Save the result document
document.SaveToFile("RemoveMacros.docm", FileFormat.Docm);
document.Close();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Vary the Colors of Same-series Data Markers in a Chart in C#, VB.NET
2018-06-12 06:38:00 Written by KoohjiIf you have a line, (xy) scatter, or radar chart, you can change the look of the data markers to make them easier to distinguish. In this article, you will learn how to set different colors for different data markers, by using Spire.Presentation with C# and VB.NET.
Step 1: Load a sample PowerPoint file.
Presentation ppt = new Presentation();
ppt.LoadFromFile("ScatterChart.pptx");
Step 2: Get the chart from the presentation.
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
Step 3: Create a ChartDataPoint object and specify the index.
ChartDataPoint dataPoint = new ChartDataPoint(chart.Series[0]); dataPoint.Index = 0;
Step 4: Set the fill color of the data marker.
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid; dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Red;
Step 5: Set the line color of the data marker.
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid; dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Red;
Step 6: Add the data point to the point collection of a series.
chart.Series[0].DataPoints.Add(dataPoint);
Step 7: Save to file.
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
Source File:

Result:

Full Code:
Presentation ppt = new Presentation();
ppt.LoadFromFile("ScatterChart.pptx");
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
ChartDataPoint dataPoint = new ChartDataPoint(chart.Series[0]);
dataPoint.Index = 0;
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Red;
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Red;
chart.Series[0].DataPoints.Add(dataPoint);
dataPoint = new ChartDataPoint(chart.Series[0]);
dataPoint.Index = 1;
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Yellow;
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Yellow;
chart.Series[0].DataPoints.Add(dataPoint);
dataPoint = new ChartDataPoint(chart.Series[0]);
dataPoint.Index = 2;
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Blue;
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Blue;
chart.Series[0].DataPoints.Add(dataPoint);
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace VaryColor
{
class Program
{
static void Main(string[] args)
{
Presentation ppt = new Presentation();
ppt.LoadFromFile("ScatterChart.pptx");
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
ChartDataPoint dataPoint = new ChartDataPoint(chart.Series[0]);
dataPoint.Index = 0;
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Red;
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Red;
chart.Series[0].DataPoints.Add(dataPoint);
dataPoint = new ChartDataPoint(chart.Series[0]);
dataPoint.Index = 1;
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Yellow;
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Yellow;
chart.Series[0].DataPoints.Add(dataPoint);
dataPoint = new ChartDataPoint(chart.Series[0]);
dataPoint.Index = 2;
dataPoint.MarkerFill.Fill.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Fill.SolidColor.Color = Color.Blue;
dataPoint.MarkerFill.Line.FillType = FillFormatType.Solid;
dataPoint.MarkerFill.Line.SolidFillColor.Color = Color.Blue;
chart.Series[0].DataPoints.Add(dataPoint);
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
}
}
}
At some point, programmers may need to determine if an Excel file contains VBA macros. This article is going to show you how to programmatically determine if an Excel file contains VBA macros in C# and VB.NET using Spire.XLS.
Detail steps:
Step 1: Instantiate a Workbook object and load the Excel file.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Macro.xlsm");
Step 2: Determine if the Excel file contains VBA macros.
bool hasMacros = false;
hasMacros = workbook.HasMacros;
if (hasMacros)
{
Console.WriteLine("The file contains VBA macros");
}
else
{
Console.WriteLine("The file doesn't contain VBA macros");
}
Screenshot:

Full code:
using System;
using Spire.Xls;
namespace Determine_if_Excel_file_contains_macros
{
class Program
{
static void Main(string[] args)
{
//Instantiate a Workbook object
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile("Macro.xlsm");
bool hasMacros = false;
//Determine if the Excel file contains VBA macros
hasMacros = workbook.HasMacros;
if (hasMacros)
{
Console.WriteLine("The file contains VBA macros");
}
else
{
Console.WriteLine("The file doesn't contain VBA macros");
}
Console.ReadKey();
}
}
}
Imports System
Imports Spire.Xls
Namespace Determine_if_Excel_file_contains_macros
Class Program
Private Shared Sub Main(ByVal args As String())
Dim workbook As Workbook = New Workbook()
workbook.LoadFromFile("Macro.xlsm")
Dim hasMacros As Boolean = False
hasMacros = workbook.HasMacros
If hasMacros Then
Console.WriteLine("The file contains VBA macros")
Else
Console.WriteLine("The file doesn't contain VBA macros")
End If
Console.ReadKey()
End Sub
End Class
End Namespace
Adding footers to a PDF document is a useful way to provide additional information and context to the content within the document. Footers typically appear at the bottom of each page and can include elements such as page numbers, dates, copyright information, or any other relevant details. By incorporating footers, you can enhance the professionalism and organization of your PDF files, making them more informative and easier to navigate for readers. In this article, you will learn how to add a footer to an existing PDF document in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLLs included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
When an existing PDF document is manipulated by Spire.PDF for .NET, the origin of the coordinate system is located at the top left corner of the page, with the x-axis extending to the right and the y-axis extending downward. Adding a footer to a page means adding content, such as text, images, automatic fields and shapes, to a specified location in the bottom blank area of the page.

If the blank area is not large enough to accommodate the content you want to add, you can consider increasing the PDF page margins.
Add a Footer to an Existing PDF Document in C#, VB.NET
Spire.PDF for .NET offers the PdfCanvas.DrawString() method, PdfCanvas.DrawImage() method, PdfCanvas.DrawLine() method and its similar methods, allowing users to draw text, images and shapes on a PDF page at the specified location. To add dynamic data to the footer, such as page numbers, sections, dates, you need to use the automatic fields. Spire.PDF for .NET provides the PdfPageNumberField class, PdfPageCountField calss, PdfSectionNumberField class etc. to achieve the addition of dynamic information.
The following are the steps to add a footer consisting of an image and page number to a PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Load an image using PdfImage.FromFile() method.
- Draw the image on the bottom blank area of a page using PdfPageBase.Canvas.DrawImage() method.
- Create a PdfPageNumberField object, a PdfPageCountField object, and combine them in a PdfCompositefield object to return the string "Page X of Y".
- Draw page number on the bottom blank area of a page using PdfCompositeField.Draw() method.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddHeaderToExistingPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Load an image
PdfImage footerImage = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\bg.jpg");
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12f, FontStyle.Bold), true);
//Create a brush
PdfBrush brush = PdfBrushes.White;
//Create a page number field
PdfPageNumberField pageNumberField = new PdfPageNumberField();
//Create a page count field
PdfPageCountField pageCountField = new PdfPageCountField();
//Create a composite field to combine page count field and page number field in a single string
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
//Get the text size
SizeF fontSize = font.MeasureString(compositeField.Text);
//Get the page size
SizeF pageSize = doc.Pages[0].Size;
//Set the position of the composite field
compositeField.Location = new Point((int)(pageSize.Width - fontSize.Width) / 2, (int)pageSize.Height - 45);
//Loop through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{
//Get a specific page
PdfPageBase page = doc.Pages[i];
//Draw the image on the bottom blank area
page.Canvas.DrawImage(footerImage, 55, pageSize.Height - 65, pageSize.Width - 110, 50);
//Draw the composite field on the bottom blank area
compositeField.Draw(page.Canvas);
}
//Save to file
doc.SaveToFile("AddFooter.pdf");
doc.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Spire.XLS supports to hide or unhide certain shapes in Excel worksheet through IShape.Visible property. This article demonstrates the detail steps to hide or unhide a shape using Spire.XLS and C#.
Below is the screenshot of the example Excel file:

Detail steps:
Step 1: Instantiate a Workbook object and load the Excel file.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Input.xlsx");
Step 2: Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Hide the second shape in the worksheet.
//Hide the second shape in the worksheet sheet.PrstGeomShapes[1].Visible = false; //Show the second shape in the worksheet //sheet.PrstGeomShapes[1].Visible = true;
Step 4: Save the file.
workbook.SaveToFile("HideShape.xlsx", ExcelVersion.Version2013);
Output:

Full code:
using Spire.Xls;
namespace HideShape
{
class Program
{
static void Main(string[] args)
{
//Instantiate a Workbook object
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile("Input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Hide the second shape in the worksheet
sheet.PrstGeomShapes[1].Visible = false;
//Show the second shape in the worksheet
sheet.PrstGeomShapes[1].Visible = true;
//Save the file
workbook.SaveToFile("HideShape.xlsx", ExcelVersion.Version2013);
}
}
}
We have demonstrated how to use Spire.Presentation to add text watermark and image watermark to the presentation slides. This article will show how to remove text and image watermarks in presentation slides in C#.
Firstly, view the sample document contains the text and image watermark.

Step 1: Create a presentation document and load the document from the file
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2013);
Step 2: Remove the text and image watermark.
Remove text watermark by removing the shape with contains the text string "Confidential".
for (int i = 0; i < ppt.Slides.Count; i++)
{
for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++)
{
if (ppt.Slides[i].Shapes[j] is IAutoShape)
{
IAutoShape shape = ppt.Slides[i].Shapes[j] as IAutoShape;
if (shape.TextFrame.Text.Contains("Confidential"))
{
ppt.Slides[i].Shapes.Remove(shape);
}
}
}
}
Remove image watermark by setting SlideBackground.Fill.FillType as none.
for (int i = 0; i < ppt.Slides.Count; i++)
{
ppt.Slides[i].SlideBackground.Fill.FillType = FillFormatType.None;
}
Step 3: Save the document to file.
ppt.SaveToFile("RemoveWartermark.pptx", FileFormat.Pptx2013);
Effective screenshot after removing the text and image watermark:


Full codes:
Remove text watermark in presentation slides:
using Spire.Presentation;
namespace RemoveWatermark
{
class Program
{
static void Main(string[] args)
{
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2013);
for (int i = 0; i < ppt.Slides.Count; i++)
{
for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++)
{
if (ppt.Slides[i].Shapes[j] is IAutoShape)
{
IAutoShape shape = ppt.Slides[i].Shapes[j] as IAutoShape;
if (shape.TextFrame.Text.Contains("Confidential"))
{
ppt.Slides[i].Shapes.Remove(shape);
}
}
}
}
ppt.SaveToFile("RemoveTextWartermark.pptx", FileFormat.Pptx2013);
}
}
Remove image watermark in presentation slides:
using Spire.Presentation;
using Spire.Presentation.Drawing;
namespace RemoveWatermark
{
class Program
{
static void Main(string[] args)
{
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample2.pptx", FileFormat.Pptx2013);
for (int i = 0; i < ppt.Slides.Count; i++)
{
ppt.Slides[i].SlideBackground.Fill.FillType = FillFormatType.None;
}
ppt.SaveToFile("RemovePicWatermak.pptx", FileFormat.Pptx2013);
}
}
}
By default, the data source of PivotTable won't be refreshed automatically. If we update the data source of our PivotTable, the PivotTable that was built on that data source needs to be refreshed. This article is going to elaborate how to refresh PivotTable in Excel programmatically in c# using Spire.XLS.
Below is the screenshot of the example Excel file:

Detail steps:
Step 1: Instantiate a Workbook object and load the Excel file.
Workbook workbook = new Workbook(); workbook.LoadFromFile(@"Sample.xlsx");
Step 2: Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: update the data source of PivotTable.
sheet.Range["C2"].Value = "199";
Step 4: Get the PivotTable that was built on the data source.
XlsPivotTable pt = workbook.Worksheets[0].PivotTables[0] as XlsPivotTable;
Step 5: Refresh the data of PivotTable.
pt.Cache.IsRefreshOnLoad = true;
Step 6: Save the file.
workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2013);
Output after updating data source and refreshing the PivotTable:

Full code:
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.PivotTables;
namespace Refresh_Pivot_Table_in_Excel
{
class Program
{
static void Main(string[] args)
{
//Instantiate a Workbook object
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile(@"Sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Update the data source of PivotTable
sheet.Range["C2"].Value = "199";
//Get the PivotTable that was built on the data source
XlsPivotTable pt = workbook.Worksheets[0].PivotTables[0] as XlsPivotTable;
//Refresh the data of PivotTable
pt.Cache.IsRefreshOnLoad = true;
//Save the file
workbook.SaveToFile("Output.xlsx", ExcelVersion.Version2013);
}
}
}
A header can provide useful information about the document's content, such as its title, author, date, and page numbers. This helps readers understand the document's purpose and navigate it more easily. In addition, including a logo or company name in the header reinforces brand identity and helps readers associate the document with a particular organization or business. In this article, you will learn how to add a header to an existing PDF document in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
When an existing PDF document is manipulated by Spire.PDF for .NET, the origin of the coordinate system is located at the top left corner of the page, with the x-axis extending to the right and the y-axis extending downward. Adding a header to a page means adding content, such as text, images, automatic fields and shapes, to a specified location in the upper blank area of the page.

If the blank area is not large enough to accommodate the content you want to add, you can consider increasing the PDF page margins.
Add a Header to an Existing PDF Document in C# and VB.NET
Spire.PDF for .NET allows users to draw text, images and shapes on a PDF page using PdfCanvas.DrawString() method, PdfCanvas.DrawImage() method, PdfCanvas.DrawLine() and other similar methods. To add dynamic information to the header, such as page numbers, sections, dates, you need to resort to automatic fields. Spire.PDF for .NET provides the PdfPageNumberField class, PdfSectionNumberField class, PdfCreationDateField class, etc. to achieve the dynamic addition of these data.
The following are the steps to add a header consisting of text, an image, a date, and a line to a PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Create font, pen and brush objects that will be used to draw text or shapes.
- Draw text on the top blank area of a page using PdfPageBase.Canvas.DrawString() method.
- Draw a line on the top blank area of a page using PdfPageBase.Canvas.DrawLine() method.
- Load an image using PdfImage.FromFile() method.
- Draw the image on the top blank area of a page using PdfPageBase.Canvas.DrawImage() method.
- Create a PdfCreationDateField object that reflects the creation time of the document.
- Draw the creation time on the top blank area of a page using PdfCreationDateField.Draw() method.
- Save the document to another PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddHeaderToExistingPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Load a PDF file
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\TargetMarket.pdf");
//Load an image for the header
PdfImage headerImage = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\logo.png");
//Get image width in pixel
float width = headerImage.Width;
//Convert pixel to point
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
float pointWidth = unitCvtr.ConvertUnits(width, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
//Specify text for the header
string headerText = "E-iceblue Tech\nwww.e-iceblue.com";
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12f, FontStyle.Bold), true);
//Create a brush
PdfBrush brush = PdfBrushes.Purple;
//Create a pen
PdfPen pen = new PdfPen(brush, 1.0f);
//Create a creation date field
PdfCreationDateField creationDateField = new PdfCreationDateField(font, brush);
creationDateField.DateFormatString = "yyyy-MM-dd";
//Create a composite field to combine static string and date field
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "creation time: {0}", creationDateField);
compositeField.Location = new Point(55, 48);
//Loop through the pages in the document
for (int i = 0; i < doc.Pages.Count; i++)
{
//Get specific page
PdfPageBase page = doc.Pages[i];
//Draw the image on the top blank area
page.Canvas.DrawImage(headerImage, page.ActualSize.Width - pointWidth - 55, 20);
//Draw text on the top blank area
page.Canvas.DrawString(headerText, font, brush, 55, 20);
//Draw a line on the top blank area
page.Canvas.DrawLine(pen, new PointF(55, 70), new PointF(page.ActualSize.Width - 55, 70));
//Draw the composite field on the top blank area
compositeField.Draw(page.Canvas);
}
//Save to file
doc.SaveToFile("AddHeader.pdf");
doc.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Spire.Doc offers the property ShapeObject. HorizontalAlignment and ShapeObject. Vertical Alignment to enable developers to align the shapes horizontally or vertically. This article will show you to how to align the shapes on the word document in C#.
Step 1: Create a new instance of word document and load the document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
Step 2: Get the first section from file.
Section section = doc.Sections[0];
Step 3: Traverse the word document and set the horizontal alignment as left.
foreach (Paragraph para in section.Paragraphs)
{
foreach (DocumentObject obj in para.ChildObjects)
{
if (obj is ShapeObject)
{
(obj as ShapeObject).HorizontalAlignment = ShapeHorizontalAlignment.Left;
}
}
}
Step 4: Save the document to file.
doc.SaveToFile("Result.docx", FileFormat.Docx);
Effective screenshot after setting the alignment of the shapes.


Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace AlignShape
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx");
Section section = doc.Sections[0];
foreach (Paragraph para in section.Paragraphs)
{
foreach (DocumentObject obj in para.ChildObjects)
{
if (obj is ShapeObject)
{
(obj as ShapeObject).HorizontalAlignment = ShapeHorizontalAlignment.Left;
////Set the vertical alignment as top
//(obj as ShapeObject).VerticalAlignment = ShapeVerticalAlignment.Top;
}
}
}
doc.SaveToFile("Result.docx", FileFormat.Docx2013);
}
}
}