Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Oct 19, 2020 8:21 am

hello
i added header to pdf doc but it added it to 2 pages .
how can i add to only first page ?

felix13579
 
Posts: 2
Joined: Mon Oct 19, 2020 8:20 am

Mon Oct 19, 2020 9:16 am

Hello,

Thanks for your inquiry.
Please refer to the following code to add header to only first page. If this does not meet your needs well, please provide your input file as well as your desired output for further investigation.
Code: Select all
            PdfDocument doc = new PdfDocument();
            //Load a two-page file
            doc.LoadFromFile("Two-page file.pdf");
            PdfBrush brush = PdfBrushes.Black;
            PdfPen pen = new PdfPen(brush, 0.75f);
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Italic), true);
            PdfStringFormat rightAlign = new PdfStringFormat(PdfTextAlignment.Right);
            rightAlign.MeasureTrailingSpaces = true;
            PdfMargins margin = doc.PageSettings.Margins;

            float space = font.Height * 0.75f;
            float x = 0;
            float y = 0;
            float width = 0;

            //Create a new pdf document
            PdfDocument newPdf = new PdfDocument();
            PdfPageBase newPage;

            for (int i =0; i< doc.Pages.Count;i++)
            {
                PdfPageBase page = doc.Pages[i];
                //Add new page
                newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));

                newPage.Canvas.SetTransparency(0.5f);
                x = margin.Left;
                width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;
                y = margin.Top - space;

                //If it is the first page
                if (i == 0)
                {
                    //Draw header line
                    newPage.Canvas.DrawLine(pen, x, y + 15, x + width, y + 15);
                    y = y + 10 - font.Height;

                    //Draw header image into newPage
                    newPage.Canvas.SetTransparency(0.5f);
                    PdfImage headerImage = PdfImage.FromFile(@"HeaderImage.png");
                    newPage.Canvas.DrawImage(headerImage, new PointF(0, 0));

                    //Draw header text into newPage
                    newPage.Canvas.DrawString("Header text", font, brush, x + width, y, rightAlign);
                }
               
                //Draw the page into newPage
                page.CreateTemplate().Draw(newPage.Canvas, new PointF(0, 0));
            }

            string output = "Output.pdf";

            //Save the document
            newPdf.SaveToFile(output, FileFormat.PDF);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Oct 27, 2020 1:21 am

Hello,

Hope you are doing well.
Has your issue been resolved? Any feedback will be greatly appreciated!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF