【发布时间】: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
【问题讨论】:
-
您的代码需要调用
process的参考 -
简单搜索“[pdfsharp] 第二页”会发现:stackoverflow.com/a/21143712/1015447 您可以使用 MigraDoc,它会自动处理分页符。forum.pdfsharp.net/viewtopic.php?p=9426#p9426