使用 ItextSharp 在 PDF 中添加"Page 1 of **"



这是我的代码工作的,但仅显示数字。该公司需要一个标准,他们需要像"第1页","第1页",第2页,共2页,直到达到最后一个数字,我似乎缺少我知道的东西,我只需要一些headsup在这里

我的代码看起来像这样

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Font = iTextSharp.text.Font;
namespace ConsoleApp1
{
    class Program
    {
        public static void AddPageNumber(string fileIn, string fileOut)
        {
            byte[] bytes = File.ReadAllBytes(fileIn);
            Font blackFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
            using (MemoryStream stream = new MemoryStream())
            {
                PdfReader reader = new PdfReader(bytes);
                using (PdfStamper stamper = new PdfStamper(reader, stream))
                {
                    int pages = reader.NumberOfPages;
                    for (int i = 1; i <= pages; i++)
                    {
                        ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                    }
                }
                bytes = stream.ToArray();
            }
            File.WriteAllBytes(fileOut, bytes);
        }
        static void Main(string[] args)
        {
            string inputPdfString = @"C:Users***DesktopDoc1.pdf";
            string outputResultString = @"C:Users***DesktopDoc2Out.pdf";
            AddPageNumber(inputPdfString,outputResultString);
            Console.WriteLine("Finished!");
            Console.Read();
        }
    }
}

,所以我已经修复了此问题,如果有人需要它,这是源代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Font = iTextSharp.text.Font;
namespace ConsoleApp1
{
    class Program
    {
         public static void AddPageNumber(string fileIn, string fileOut)
         {
             byte[] bytes = File.ReadAllBytes(fileIn);
             Font blackFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
             using (MemoryStream stream = new MemoryStream())
             {
                 PdfReader reader = new PdfReader(bytes);
                 using (PdfStamper stamper = new PdfStamper(reader, stream))
                 {
                     int pages = reader.NumberOfPages;
                     for (int i = 1; i <= pages; i++)
                     {
                         //ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
                         ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(String.Format("Page " +i+ " of " + pages.ToString())), 568f, 15f, 0);
                     }
                 }
                 bytes = stream.ToArray();
             }
             File.WriteAllBytes(fileOut, bytes);
         }
        static void Main(string[] args)
        {
            string inputPdfString = @"C:Users*************DesktopDoc1.pdf";
            string outputResultString = @"C:Users************DesktopDoc2Out.pdf";
            AddPageNumber(inputPdfString,outputResultString);
            Console.WriteLine("Finished!");
            Console.Read();
        }
    }
}

相关内容

最新更新