文本文件到PDF转换代码切断文本vb.net

时间:2017-07-22 06:28:40

标签: vb.net pdf pdfsharp

我的文本文件上的文字不适合以下代码生成的单个pdf页面。

请注意,我的程序写入文本文件的文本长度不同,因此当导入到WORD环境时,可能是1,2或3(等等)页面文档。

如何在必要时调整代码以自动添加多个pdf页面,以便在生成pdf文件时,文本文件中的文本不会丢失/删除?

请参阅以下代码:

Imports System.IO
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Pdf

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try
        Dim line As String
        Dim readFile As System.IO.TextReader = New StreamReader("Text.txt")
        Dim yPoint As Integer = 0

        Dim pdf As PdfDocument = New PdfDocument
        pdf.Info.Title = "Text File to PDF"
        Dim pdfPage As PdfPage = pdf.AddPage
        Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
        Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Regular)

        While True
            line = readFile.ReadLine()
            If line Is Nothing Then
                Exit While
            Else
                graph.DrawString(line, font, XBrushes.Black, _
                New XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                yPoint = yPoint + 40
            End If
        End While


        Dim pdfFilename As String = "txttopdf.pdf"
        pdf.Save(pdfFilename)
        readFile.Close()
        readFile = Nothing
        Process.Start(pdfFilename)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

  • 我想从最近的经验来看,文本流中的水平坐标被定义为yPoint=840,总计21行。

在此基础上,您应该在每个阈值=跳过21行后动态添加页面。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Dim line As String
            Dim readFile As System.IO.TextReader = New StreamReader("Text.txt")
            Dim yPoint As Integer = 0

            Dim pdf As PdfDocument = New PdfDocument
            pdf.Info.Title = "Text File to PDF"
            Dim pdfPage As PdfPage = pdf.AddPage
            Dim graph As XGraphics = XGraphics.FromPdfPage(pdfPage)
            Dim font As XFont = New XFont("Verdana", 20, XFontStyle.Regular)

            While True
                line = readFile.ReadLine()
                If line Is Nothing Then
                    Exit While
                Else
                    graph.DrawString(line, font, XBrushes.Black, New XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft)
                    yPoint = yPoint + 40

'--------------------------- here is the part added ----------------------------------
                    If yPoint = 840 Then
                        pdfPage = pdf.AddPage
                        graph = XGraphics.FromPdfPage(pdfPage)
                        yPoint = 0
                    End If
'-------------------------------------------------------------------------------------

                End If
            End While


            Dim pdfFilename As String = "txttopdf.pdf"
            pdf.Save(pdfFilename)
            readFile.Close()
            readFile = Nothing
            System.Diagnostics.Process.Start(pdfFilename)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

End Sub