【问题标题】:Text file to PDF conversion code cuts off text vb.net文本文件到PDF转换代码截断文本 vb.net
【发布时间】:2017-12-28 07:40:49
【问题描述】:

我的文本文件中的文本不适合以下代码生成的单个 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

【问题讨论】:

标签: vb.net pdf pdfsharp


【解决方案1】:
  • 我猜根据最近的经验,剪切文本流的水平坐标定义为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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多