iTextSharp PDF一次性合并和添加页码 [英] iTextSharp PDF Merging and adding page number in one go
问题描述
我使用以下代码合并页面编号并将其添加到文档中:
I use this code to merge and add page number to the document:
Using stream As FileStream = New FileStream(targetPDF, FileMode.Create)
Dim myDoc As Document
If (ExportOption.Orientation = Global.GenerateReport.GenerateReport.Orientation.Portrait) Then
myDoc = New Document(New iTextSharp.text.Rectangle(PAGE_WIDTH, PAGE_HEIGHT), MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM)
myDoc.SetPageSize(PageSize.A4)
Else
myDoc = New Document(New iTextSharp.text.Rectangle(PAGE_WIDTH, PAGE_HEIGHT), MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM)
myDoc.SetPageSize(PageSize.A4.Rotate())
End If
Dim pdfCopy As PdfCopy = New PdfCopy(myDoc, stream)
myDoc.Open()
Dim files() As String = Directory.GetFiles(sourceDir)
For Each pdffile As String In files
Dim reader As PdfReader = New PdfReader(pdffile)
pdfCopy.AddDocument(reader)
reader.Close()
Next
If (Not myDoc Is Nothing) Then
myDoc.Close()
pdfCopy.Close()
End If
End Using
Dim bytes As Byte() = File.ReadAllBytes(targetPDF)
Dim blackFont As iTextSharp.text.Font = FontFactory.GetFont("HELVETICA", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)
Using stream As New MemoryStream()
Dim reader As New PdfReader(bytes)
Using stamper As New PdfStamper(reader, stream)
Dim bottom As Single = 10.0F
Dim left As Single = 550.0F
Dim pages As Integer = reader.NumberOfPages
For i As Integer = 1 To pages
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, New Phrase("Page " & i.ToString() & " of " & reader.NumberOfPages, blackFont), left, bottom, 0)
Next
End Using
bytes = stream.ToArray()
End Using
File.WriteAllBytes(targetPDF, bytes)
我怎么能一口气做到呢?
How can I do it in just one go?
有关我的情况的更多详细信息:我需要创建一个大文件,正常情况下,代码运行了几天,并抛出了内存不足异常,因此我将其拆分为较小的文档,效果很好,但是页码是都错了,那么我来介绍这一部分.
Some more details about my situation: I need to create a big file, in normal way the code ran for several days and threw Out of memory exception, so I split it to smaller documents, it works great but the page number is all wrong, then I come to this part.
推荐答案
要合并文档并同时在文档上标记戳记,可以使用PdfCopy.PageStamp
.样本 ConcatenateStamp.cs (对应于Java示例 ConcatenateStamp.java 本书"iText in Action-2nd Edition"中的内容)演示了PdfCopy.PageStamp
的使用:
To merge documents and stamp something on them at the same time, you can use a PdfCopy.PageStamp
. The sample ConcatenateStamp.cs (corresponding to the java sample ConcatenateStamp.java from the book "iText in Action - 2nd Edition") demonstrates the use of a PdfCopy.PageStamp
:
using (Document document = new Document()) {
using (PdfCopy copy = new PdfCopy(document, OUTPUT_STREAM)) {
document.Open();
// reader for document 1
PdfReader reader1 = new PdfReader(r1);
int n1 = reader1.NumberOfPages;
// reader for document 2
PdfReader reader2 = new PdfReader(r2);
int n2 = reader2.NumberOfPages;
// initializations
PdfImportedPage page;
PdfCopy.PageStamp stamp;
// Loop over the pages of document 1
for (int i = 0; i < n1; ) {
page = copy.GetImportedPage(reader1, ++i);
stamp = copy.CreatePageStamp(page);
// add page numbers
ColumnText.ShowTextAligned(
stamp.GetUnderContent(), Element.ALIGN_CENTER,
new Phrase(string.Format("page {0} of {1}", i, n1 + n2)),
297.5f, 28, 0
);
stamp.AlterContents();
copy.AddPage(page);
}
// Loop over the pages of document 2
for (int i = 0; i < n2; ) {
page = copy.GetImportedPage(reader2, ++i);
stamp = copy.CreatePageStamp(page);
// add page numbers
ColumnText.ShowTextAligned(
stamp.GetUnderContent(), Element.ALIGN_CENTER,
new Phrase(string.Format("page {0} of {1}", n1 + i, n1 + n2)),
297.5f, 28, 0
);
stamp.AlterContents();
copy.AddPage(page);
}
}
}
(该示例使用C#而不是VB,但应该可以提供一个想法...)
(The sample is in C#, not VB but it should serve to give an idea...)
这篇关于iTextSharp PDF一次性合并和添加页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!