2017-07-22 158 views
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 
+0

你的代碼需要呼叫參考'process' – Abra001

+1

爲「[pdfsharp]第二頁」一個簡單的搜索發現這一點:https://stackoverflow.com/a/21143712/1015447您可以使用MigraDoc哪些會自動處理分頁符.http://forum.pdfsharp.net/viewtopic.php?p = 9426#p9426 –

回答

0
  • 我從最近的earnt經驗猜測,水平座標這在文本流被切斷被定義爲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