簡體   English   中英

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

[英]Adding “Page 1 of **” in PDF using ItextSharp

我的代碼可以使用,但它只顯示數字。 該公司需要一個標准,他們需要像“第1頁,共1頁”,“第1頁,共2頁”,系列直到它到達最后一個數字,我似乎缺少我知道的東西,我只需要一些抬頭在這里

我的代碼看起來像這樣

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\***\Desktop\Doc1.pdf";
            string outputResultString = @"C:\Users\***\Desktop\Doc2Out.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\*************\Desktop\Doc1.pdf";
            string outputResultString = @"C:\Users\************\Desktop\Doc2Out.pdf";
            AddPageNumber(inputPdfString,outputResultString);
            Console.WriteLine("Finished!");
            Console.Read();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM