0
我的文本文件上的文本不適合下面代碼生成的單個PDF頁面。將文本文件轉換爲PDF轉換代碼將文本截斷vb.net
請注意,我的程序寫入文本文件的文本長短不一,這樣當導入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' – Abra001
爲「[pdfsharp]第二頁」一個簡單的搜索發現這一點:https://stackoverflow.com/a/21143712/1015447您可以使用MigraDoc哪些會自動處理分頁符.http://forum.pdfsharp.net/viewtopic.php?p = 9426#p9426 –