16,553
社区成员
发帖
与我相关
我的任务
分享
Private Sub MergePdfFiles(ByVal strSrcFilePath As String, ByVal strSrcPdfFiles As String(), ByVal strDestFilePath As String, ByVal strDestPdfFile As String)
Dim intIdx As Integer = 0
Try
If Not strSrcFilePath.EndsWith("\") Then
strSrcFilePath = strSrcFilePath + "\"
End If
If Not strDestFilePath.EndsWith("\") Then
strDestFilePath = strDestFilePath + "\"
End If
'先將橫向的pdf文件轉換成縱向的pdf文件
For intIdx = 0 To strSrcPdfFiles.Length - 1
ChangePdfDirection(strSrcFilePath, strSrcPdfFiles(intIdx), 1)
Next
Dim strDestFullName As String = strDestFilePath + strDestPdfFile
Dim reader As PdfReader
Dim doc As New Document(PageSize.A4, 5, 5, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(strDestFullName, FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
Dim newpage As PdfImportedPage
For intIdx = 0 To strSrcPdfFiles.Length - 1
Dim strSrcFullName As String = strSrcFilePath + strSrcPdfFiles(intIdx)
reader = New PdfReader(strSrcFullName.Trim())
Dim intPageCnt As Integer = reader.NumberOfPages
For intPageIdx As Integer = 1 To intPageCnt
doc.NewPage()
newpage = writer.GetImportedPage(reader, intPageIdx)
Dim truePageHeight As Double = doc.PageSize.Height - doc.TopMargin - doc.BottomMargin
Dim truePageWidth As Double = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin
cb.AddTemplate(newpage, truePageWidth / newpage.Width, 0, 0, truePageHeight / newpage.Height, doc.LeftMargin, doc.TopMargin)
Next
Next
doc.Close()
doc.Dispose()
Catch ex As Exception
Throw New Exception("Error:" + intIdx.ToString().Trim())
End Try
End Sub
Private Sub ChangePdfDirection(ByVal strPath As String, ByVal strPdfFile As String, ByVal intMode As Integer)
If intMode = 1 Then '橫向轉換成縱向
If Not strPath.EndsWith("\") Then
strPath = strPath + "\"
End If
Dim strPdfFullName As String = strPath + strPdfFile
Dim strpdfTmpName As String = strPath + "tmp_" + strPdfFile
Dim doc As Document = Nothing
Dim reader As New PdfReader(strPdfFullName.Trim())
Dim intPageCnt As Integer = reader.NumberOfPages
Dim psize As Rectangle = reader.GetPageSize(1)
If psize.Width > psize.Height Then '當pdf的寬度大於高度,當作橫向pdf處理
doc = New Document(PageSize.A4.Rotate, 5, 5, 0, 0)
Else
Exit Sub
End If
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(strpdfTmpName, FileMode.Create))
doc.Open()
Dim cb As PdfContentByte = writer.DirectContent
For intIdx As Integer = 1 To intPageCnt
doc.NewPage()
Dim page1 As PdfImportedPage = writer.GetImportedPage(reader, intIdx)
Dim truePageWidth As Double = doc.PageSize.Width - doc.LeftMargin - doc.RightMargin
Dim truePageHeight As Double = doc.PageSize.Height - doc.TopMargin - doc.BottomMargin
cb.AddTemplate(page1, truePageWidth / page1.Width, 0, 0, truePageHeight / page1.Height, doc.LeftMargin, doc.TopMargin)
Next
doc.Close()
doc.Dispose()
writer.Close()
writer.Dispose()
reader.Close()
If File.Exists(strPdfFullName) Then
File.Delete(strPdfFullName)
End If
If File.Exists(strpdfTmpName) Then
File.Copy(strpdfTmpName, strPdfFullName)
File.Delete(strpdfTmpName)
End If
'If File.Exists(strpdfTmpName) Then
'End If
Else '縱向轉換成橫向
End If
End Sub