我有许多大型pdf文件,其中包含许多页面。我必须将那些较大的pdf拆分为多个小的pdf文件。
我有一个代码,可用于将大型pdf拆分为多个小的pdf文件。 下面的代码,我用于拆分。
private void SplitAndSaveInterval(string pdfFilePath, string outputPath, int startPage, int interval, string pdfFileName)
{
using (PdfReader reader = new PdfReader(pdfFilePath))
{
Document document = new Document();
PdfCopy copy = new PdfCopy(document, new FileStream(outputPath + "\\" + pdfFileName + ".pdf", FileMode.Create));
document.Open();
for (int pagenumber = startPage; pagenumber < (startPage + interval); pagenumber++)
{
if (reader.NumberOfPages >= pagenumber)
{
copy.AddPage(copy.GetImportedPage(reader, pagenumber));
}
else
{
break;
}
}
document.Close();
}
}
此代码取自https://www.c-sharpcorner.com/article/splitting-pdf-file-in-c-sharp-using-itextsharp/
我有一个添加页码的例程。例行程序:
void AddPageNumber(string fileIn, string fileOut)
{
byte[] bytes = File.ReadAllBytes(fileIn);
Font blackFont = FontFactory.GetFont("Arial", 12, 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);
}
现在我的问题是如何从SplitAndSaveInterval()例程中合并或调用AddPageNumber()例程,以最终在新生成的pdf文件的右上部分的每一页上添加页码。
寻求帮助和建议。谢谢
答案 0 :(得分:0)
您似乎在使用iTextSharp,是否尝试过他们的文档中的以下内容:
public void onEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
switch(writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, header[0],
rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, header[1],
rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
https://developers.itextpdf.com/question/how-add-text-left-and-right-header