[英]Adding “Page 1 of **” in PDF using ItextSharp
This my code works, but it shows only the numbers. 我的代码可以使用,但它只显示数字。 The company needs a standard that they would be needing something like "Page 1 of 1" , "Page 1 of 2", in series till it gets to the last Number, I appear to be missing something I know, i just needs a few headsup here
该公司需要一个标准,他们需要像“第1页,共1页”,“第1页,共2页”,系列直到它到达最后一个数字,我似乎缺少我知道的东西,我只需要一些抬头在这里
My code looks something like this 我的代码看起来像这样
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();
}
}
}
So i fixed this and its fine now , in case someone needs it here is the source code 所以我现在解决了这个问题,以防有人需要它,这是源代码
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.