VB.NET 2015 - 提取具有不同方向的页面

时间:2016-07-21 15:22:44

标签: pdf itext extract

我发现this post使用itextsharp lib从pdf文件中提取页面。但是我的文档对每个页面都有不同的方向,有时页面大小不同。但首先我尝试用页面方向来解决这个问题。这是我的代码和我的想法也许有人可以告诉我我是如何做到的。

Public Function ExtractPdfPages(ByVal SourceFile As String, ByVal TargetFile As String, ByVal IntStr As Integer, ByVal IntEnd As Integer)
    Try
        Dim impPage As PdfImportedPage = Nothing
        Dim Reader As New PdfReader(SourceFile)
        Dim PageRotation As String = ""


        Dim srcDoc As New Document(Reader.GetPageSizeWithRotation(IntStr))
        Dim PdfCopyProvider As New PdfCopy(srcDoc, New System.IO.FileStream(TargetPath & "\" & TargetFile, System.IO.FileMode.Create))

        srcDoc.Open()
        For X = IntStr To IntEnd
            impPage = PdfCopyProvider.GetImportedPage(Reader, X)
            Dim myDocOr As Rectangle = Reader.GetPageSize(X)

            If myDocOr.Width >= myDocOr.Height Then
                PageRotation = "land"
            Else
                PageRotation = "port"
            End If
            ' --- fix orientation

            ' --- add page
            PdfCopyProvider.AddPage(impPage)

        Next

        srcDoc.Close()
        Reader.Close()
    Catch ex As Exception
        Throw ex
    End Try


End Function

1 个答案:

答案 0 :(得分:0)

这样做对我来说。对于其他任何人来说,解决方案中存在同样的问题:

Public Function ExtractPdfPages(ByVal SourceFile As String, ByVal TargetFile As String, ByVal IntStr As Integer, ByVal IntEnd As Integer)
    Try
        Dim impPage As PdfImportedPage = Nothing
        Dim Reader As New PdfReader(SourceFile)
        Dim PageRotation As String = ""


        Dim srcDoc As New Document(Reader.GetPageSizeWithRotation(IntStr))
        Dim PdfCopyProvider As New PdfCopy(srcDoc, New System.IO.FileStream(TargetPath & "\" & TargetFile, System.IO.FileMode.Create))

        srcDoc.Open()
        For X = IntStr To IntEnd
            impPage = PdfCopyProvider.GetImportedPage(Reader, X)

            PdfCopyProvider.SetPageSize(New Rectangle(0.0F, 0.0F, impPage.Width, impPage.Height))

            PdfCopyProvider.AddPage(impPage)

        Next

        srcDoc.Close()
        Reader.Close()
    Catch ex As Exception
        Throw ex
    End Try


End Function

魔术是设置PageSize。

PdfCopyProvider.SetPageSize(New Rectangle(0.0F, 0.0F, impPage.Width, impPage.Height))