page 171

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.

C#/VB.NET: Add a Header to an Existing PDF Document

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();
        }
    }
}

C#/VB.NET: Add a Header to an Existing PDF Document

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.

How to align the shape on word document in C#

How to align the shape on word document in C#

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);
        }
    }
}

Adding shadow effect is one of the good ways to make a data label stand out on your chart. This article is going to show you how to add shadow effect to a chart data label in PowerPoint using Spire.Presentation.

Detail steps:

Step 1: Initialize a Presentation object and load the PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"test.pptx");

Step 2: Get the chart.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Add a data label to the first chart series.

ChartDataLabelCollection dataLabels = chart.Series[0].DataLabels;            
ChartDataLabel Label = dataLabels.Add();
Label.LabelValueVisible = true;

Step 4: Add outer shadow effect to the data label.

Label.Effect.OuterShadowEffect = new OuterShadowEffect();
//Set shadow color
Label.Effect.OuterShadowEffect.ColorFormat.Color = Color.Yellow;
//Set blur
Label.Effect.OuterShadowEffect.BlurRadius = 5;
//Set distance
Label.Effect.OuterShadowEffect.Distance = 10;
//Set angle
Label.Effect.OuterShadowEffect.Direction = 90f;

Step 5: Save the file.

ppt.SaveToFile("Shadow.pptx", FileFormat.Pptx2010);

Screenshot:

Add Shadow Effect to Chart DataLabels in PowerPoint in C#

Full code:

using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Collections;
using Spire.Presentation.Drawing;

namespace Add_Shadow_Effect_to_Chart_Datalabel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize a Presentation object
            Presentation ppt = new Presentation();
            //Load the PowerPoint file
            ppt.LoadFromFile(@"test.pptx");

            //Get the chart
            IChart chart = ppt.Slides[0].Shapes[0] as IChart;

            //Add a data label to the first chart series
            ChartDataLabelCollection dataLabels = chart.Series[0].DataLabels;            
            ChartDataLabel Label = dataLabels.Add();
            Label.LabelValueVisible = true;
            
            //Add outer shadow effect to the data label

            Label.Effect.OuterShadowEffect = new OuterShadowEffect();
            //Set shadow color
            Label.Effect.OuterShadowEffect.ColorFormat.Color = Color.Yellow;
            //Set blur
            Label.Effect.OuterShadowEffect.BlurRadius = 5;
            //Set distance
            Label.Effect.OuterShadowEffect.Distance = 10;
            //Set angle
            Label.Effect.OuterShadowEffect.Direction = 90f;            

            //Save the file
            ppt.SaveToFile("Shadow.pptx", FileFormat.Pptx2010);
        }
    }
}
page 171