VB.NET PDF使用iTextsharp进行合并。 [英] VB.NET PDF Merge using iTextsharp.
本文介绍了VB.NET PDF使用iTextsharp进行合并。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了合并pdf文件的源代码,我不知道你是否知道这个模块 PdfManipulation2.vb 。这是合并pdf文件的代码表格
I found a source code for merging pdf files, I don't know if you know about this module PdfManipulation2.vb. This is the code form merging pdf files
''' <summary>
''' Merge multiple pdf files into a single pdf
''' </summary>
''' <param name="pdfFiles">string array containing full paths to the pdf files to be merged</param>
''' <param name="outputPath">full path to the merged output pdf</param>
''' <param name="authorName">Author's name.</param>
''' <param name="creatorName">Creator's name</param>
''' <param name="subject">Subject field</param>
''' <param name="title">Title field</param>
''' <param name="keywords">keywords field</param>
''' <returns>True if the merging is successful, False otherwise.</returns>
''' <remarks>All optional paramters are used for the output pdf metadata.
''' You can see a pdf metada by going to the PDF tab of the file's Property window.</remarks>
Public Shared Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String, _
Optional ByVal authorName As String = "", _
Optional ByVal creatorName As String = "", _
Optional ByVal subject As String = "", _
Optional ByVal title As String = "", _
Optional ByVal keywords As String = "") As Boolean
Dim result As Boolean = False
Dim pdfCount As Integer = 0 'total input pdf file count
Dim f As Integer = 0 'pointer to current input pdf file
Dim fileName As String = String.Empty 'current input pdf filename
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0 'cureent input pdf page count
Dim pdfDoc As iTextSharp.text.Document = Nothing 'the output pdf document
Dim writer As iTextSharp.text.pdf.PdfWriter = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
'Declare a variable to hold the imported pages
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim rotation As Integer = 0
'Declare a font to used for the bookmarks
Dim bookmarkFont As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, _
12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE)
Try
pdfCount = pdfFiles.Length
If pdfCount > 1 Then
'Open the 1st pad using PdfReader object
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fileName)
'Get page count
pageCount = reader.NumberOfPages
pageCount = frmMerger.GetNumberOfPdfPages(fileName)
'Instantiate an new instance of pdf document and set its margins. This will be the output pdf.
'NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location
'of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust
'the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The
'unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from
'left and 1/4 inch from top of the page.
pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
'Instantiate a PdfWriter that listens to the pdf document
writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, New System.IO.FileStream(outputPath, IO.FileMode.Create))
'Set metadata and open the document
With pdfDoc
.AddAuthor(authorName)
.AddCreationDate()
.AddCreator(creatorName)
.AddProducer()
.AddSubject(subject)
.AddTitle(title)
.AddKeywords(keywords)
.Open()
End With
'Instantiate a PdfContentByte object
cb = writer.DirectContent
'Now loop thru the input pdfs
frmMerger.ProgressBar1.Value = 0
frmMerger.ProgressBar1.Maximum = pdfCount
While f < pdfCount
Application.DoEvents()
frmMerger.ProgressBar1.Value = frmMerger.ProgressBar1.Value + 1
frmMerger.lblStatus.Text = "Status: Merging " & New IO.FileInfo(fileName).Name
'Declare a page counter variable
Dim i As Integer = 0
'Loop thru the current input pdf's pages starting at page 1
While i < pageCount
i += 1
'Get the input page size
pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
'Create a new page on the output document
pdfDoc.NewPage()
'If it is the 1st page, we add bookmarks to the page
'REMOVE FILENAME HEADER
'If i = 1 Then
' 'First create a paragraph using the filename as the heading
' Dim para As New iTextSharp.text.Paragraph(IO.Path.GetFileName(fileName).ToUpper(), bookmarkFont)
' 'Then create a chapter from the above paragraph
' Dim chpter As New iTextSharp.text.Chapter(para, f + 1)
' 'Finally add the chapter to the document
' pdfDoc.Add(chpter)
'End If
'----------------
'Now we get the imported page
page = writer.GetImportedPage(reader, i)
'Read the imported page's rotation
rotation = reader.GetPageRotation(i)
'Then add the imported page to the PdfContentByte object as a template based on the page's rotation
If rotation = 90 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
ElseIf rotation = 270 Then
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
End If
End While
'Increment f and read the next input pdf file
f += 1
If f < pdfCount Then
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fileName)
pageCount = reader.NumberOfPages
pageCount = frmMerger.GetNumberOfPdfPages(fileName)
End If
End While
'When all done, we close the document so that the pdfwriter object can write it to the output file
pdfDoc.Close()
result = True
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return result
End Function
我的问题是这个
My problem is this
pageCount = reader.NumberOfPages
此代码不是准确的,pdf中的实际页数是20,代码返回30.
有人能说出是什么原因导致的吗?
要临时解决问题,我使用其他代码获取总页数...
This code is not accurate, the actual page count in pdf is 20, the code returns 30.
Can someone tell what causes that?
To temporary solve the problem I use another code the get the Total Pages...
pageCount = frmMerger.GetNumberOfPdfPages(fileName)
Public Function GetNumberOfPdfPages(ByVal fileName As String) As Integer
Using sr As New StreamReader(File.OpenRead(fileName))
Dim regex As New System.Text.RegularExpressions.Regex("/Type\s*/Page[^s]")
Dim matches As System.Text.RegularExpressions.MatchCollection = regex.Matches(sr.ReadToEnd())
Return matches.Count
End Using
End Function
推荐答案
一个主题词,一个多样性的门户网站。
Segue comacorreção:
公共函数MergePdfFiles(ByVal pdfFiles()As String,ByVal outputPath As String,_
可选ByVal authorName As String =,_
可选ByVal creatorName As String =,_
可选ByVal主题As String =,_
可选ByVal标题As String =,_
可选ByVal关键字As String =)As Boolean
Dim result As Boolean = False
Dim pdfCount As Integer = 0'总输入pdf文件数
Dim f As Integer = 0'指向当前输入pdf文件的指针
Dim fileName As String = String.Empty'当前输入pdf文件名
Dim reader因为iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0'cumpentent input pdf page count
Dim pdfDoc正如iTextSharp.text.Document =没有'输出pdf文件
Dim writer As iTextSharp.text.pdf.PdfWriter = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
'声明一个变量来保存导入的页面
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
昏暗旋转As Integer = 0
'声明用于书签的字体
Dim bookmarkFont As iTextSharp.text.Font = iTextSharp.text .FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA,_
12,iTextSharp.text.Font.BOLD,iTextSharp.text.BaseColor.BLUE)
'尝试
pdfCount = pdfFiles.Length
如果pdfCount> 1然后
'使用PdfReader对象打开第一个垫子
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader (fileName)
'获取页数
pageCount = reader.NumberOfPages
'实例化pdf文档的新实例并设置其边距。这将是输出pdf。
'注意:书签将使用其文件名添加到非常原始的pdf文件的第一页。此书签的位置
'将放置在文档的左上角。因此,您需要调整
'左边距和边距顶部值,以便书签不会覆盖在合并的pdf页面上。使用的
'单位是点数(72点= 1英寸),因此在这个例子中,书签的位置距离
'左1/4英寸页面顶部1/4英寸。
pdfDoc =新的iTextSharp.text.Document(reader.GetPageSizeWithRotation(1),18,18,18,18)
'实例化一个监听pdf文档的PdfWriter
writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc,New System.IO.FileStream(outputPath,IO.FileMode.Create))
'设置元数据并打开文档
使用pdfDoc
.AddAuthor(authorName)
.AddCreationDate()
.AddCreator(creatorName)
.AddProducer()
.AddSubject(subject)
.AddTitle(title)
.AddKeywords(关键字)
。打开()
结束
'实例化一个PdfContentByte对象
cb = writer.DirectContent
'现在循环通过输入pdf
当f< pdfCount
Application.DoEvents()
'声明页面计数器变量
'递增f并读取下一个输入的pdf文件
'如果IsNothing(fileName)那么
fileName = pdfFiles(f)
如果fileName<> 那么
如果f< pdfCount然后
reader =新的iTextSharp.text.pdf.PdfReader(fileName)
pageCount = reader.NumberOfPages
结束如果
Dim i As Integer = 0
'循环通过当前输入pdf的页面从第1页开始
当i< pageCount
i + = 1
'获取输入页面大小
pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
'在输出文件上创建新页面
pdfDoc.NewPage()
'如果是第1页,我们添加书签到页面
'删除文件头标题
'如果我= 1那么
''首先使用文件名作为标题创建一个段落
'Dim para As New iTextSharp.text.Paragraph(IO.Path.GetFileName(fileName).ToUpper(),bookmarkFont)
''然后从上一段创建章节
'Dim chpter As New iTextSharp.text.Chapter(para,f + 1)
''最后将章节添加到文档中
'pdfDoc.Add(chpter)
'结束如果
'--- -------------
'现在我们得到导入的页面
page = writer.GetImportedPage(reader,i)
'读取导入页面的旋转
rotation = reader.GetPageRotation(i)
'然后将导入的页面添加到PdfContentByte对象作为模板,基于页面的旋转
如果rotation = 90那么
cb.AddTemplate(第0页,-1.0F,1.0F,0,0,reader.GetPageSizeWithRotation(i).Height )
ElseIf rotation = 270然后
cb.AddTemplate(第0,1.0F,-1.0F,0,reader.GetP ageSizeWithRotation(i).Width + 60,-30)
Else
cb.AddTemplate(page,1.0F,0,0,1.0F,0,0)
结束如果
结束时
结束如果
f + = 1
'结束如果
结束时
'完成后,我们关闭文档,以便pdfwriter对象可以将其写入输出文件
pdfDoc.Close()
result = True
结束如果
'Catch ex As Exception
'抛出新例外(ex.Message)
'结束尝试
返回结果
结束函数
A primeira função ajuda muito , porém muito erro de logica.
Segue com a correção:
Public Function MergePdfFiles(ByVal pdfFiles() As String, ByVal outputPath As String, _
Optional ByVal authorName As String = "", _
Optional ByVal creatorName As String = "", _
Optional ByVal subject As String = "", _
Optional ByVal title As String = "", _
Optional ByVal keywords As String = "") As Boolean
Dim result As Boolean = False
Dim pdfCount As Integer = 0 'total input pdf file count
Dim f As Integer = 0 'pointer to current input pdf file
Dim fileName As String = String.Empty 'current input pdf filename
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim pageCount As Integer = 0 'cureent input pdf page count
Dim pdfDoc As iTextSharp.text.Document = Nothing 'the output pdf document
Dim writer As iTextSharp.text.pdf.PdfWriter = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
'Declare a variable to hold the imported pages
Dim page As iTextSharp.text.pdf.PdfImportedPage = Nothing
Dim rotation As Integer = 0
'Declare a font to used for the bookmarks
Dim bookmarkFont As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, _
12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLUE)
'Try
pdfCount = pdfFiles.Length
If pdfCount > 1 Then
'Open the 1st pad using PdfReader object
fileName = pdfFiles(f)
reader = New iTextSharp.text.pdf.PdfReader(fileName)
'Get page count
pageCount = reader.NumberOfPages
'Instantiate an new instance of pdf document and set its margins. This will be the output pdf.
'NOTE: bookmarks will be added at the 1st page of very original pdf file using its filename. The location
'of this bookmark will be placed at the upper left hand corner of the document. So you'll need to adjust
'the margin left and margin top values such that the bookmark won't overlay on the merged pdf page. The
'unit used is "points" (72 points = 1 inch), thus in this example, the bookmarks' location is at 1/4 inch from
'left and 1/4 inch from top of the page.
pdfDoc = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1), 18, 18, 18, 18)
'Instantiate a PdfWriter that listens to the pdf document
writer = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfDoc, New System.IO.FileStream(outputPath, IO.FileMode.Create))
'Set metadata and open the document
With pdfDoc
.AddAuthor(authorName)
.AddCreationDate()
.AddCreator(creatorName)
.AddProducer()
.AddSubject(subject)
.AddTitle(title)
.AddKeywords(keywords)
.Open()
End With
'Instantiate a PdfContentByte object
cb = writer.DirectContent
'Now loop thru the input pdfs
While f < pdfCount
Application.DoEvents()
'Declare a page counter variable
'Increment f and read the next input pdf file
'If IsNothing(fileName) Then
fileName = pdfFiles(f)
If fileName <> "" Then
If f < pdfCount Then
reader = New iTextSharp.text.pdf.PdfReader(fileName)
pageCount = reader.NumberOfPages
End If
Dim i As Integer = 0
'Loop thru the current input pdf's pages starting at page 1
While i < pageCount
i += 1
'Get the input page size
pdfDoc.SetPageSize(reader.GetPageSizeWithRotation(i))
'Create a new page on the output document
pdfDoc.NewPage()
'If it is the 1st page, we add bookmarks to the page
'REMOVE FILENAME HEADER
'If i = 1 Then
' 'First create a paragraph using the filename as the heading
' Dim para As New iTextSharp.text.Paragraph(IO.Path.GetFileName(fileName).ToUpper(), bookmarkFont)
' 'Then create a chapter from the above paragraph
' Dim chpter As New iTextSharp.text.Chapter(para, f + 1)
' 'Finally add the chapter to the document
' pdfDoc.Add(chpter)
'End If
'----------------
'Now we get the imported page
page = writer.GetImportedPage(reader, i)
'Read the imported page's rotation
rotation = reader.GetPageRotation(i)
'Then add the imported page to the PdfContentByte object as a template based on the page's rotation
If rotation = 90 Then
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
ElseIf rotation = 270 Then
cb.AddTemplate(page, 0, 1.0F, -1.0F, 0, reader.GetPageSizeWithRotation(i).Width + 60, -30)
Else
cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
End If
End While
End If
f += 1
'End If
End While
'When all done, we close the document so that the pdfwriter object can write it to the output file
pdfDoc.Close()
result = True
End If
'Catch ex As Exception
' Throw New Exception(ex.Message)
'End Try
Return result
End Function
这篇关于VB.NET PDF使用iTextsharp进行合并。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文