如何将字符串添加到多个字符串以进行外部打印

时间:2014-01-01 03:16:07

标签: vb.net string pdf for-loop arraylist

这将是一个漫长的过程,但很容易解决。

所以我设法将pdf转换为字符串,然后只需将文件名放在文本框中即可打印外部pdf。

我还想过如何从pdf字符串中提取某些文本,现在某些文本也是位于外部位置的文件(我使用c:\​​ temp \进行测试)。

这让我有一个问题,我提取的文字,我使用shellexecute打印,如果它的一个字符串工作正常。但是,如果我提取的文件名不止一个,它会将其计为单个字符串,从而将位置和.pdf添加到该字符串中。而不是两个或更多的字符串。会做这样的事情:

如您所见,它会将其发送到打印机。我想一次发送一个到打印机。像这样:

我尝试过使用Arraylist和各种方法。但是我自己缺乏知识,我无法弄明白。

我在想“for循环”会帮助我。有任何想法吗? 以下是我的代码。

        Dim pdffilename As String = Nothing
        pdffilename = RawTextbox.Text
        Dim filepath = "c:\temp\" & RawTextbox.Text & ".pdf"
        Dim thetext As String
        thetext = GetTextFromPDF(filepath)        ' converts pdf to text from a function I didnt show.

        Dim re As New Regex("[\t ](?<w>((asm)|(asy)|(717)|(ssm)|(715)|(818))[a-z0-9]*)[\t ]", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)               ' This filters out and extract certain keywords from the PDF

        Dim Lines() As String = {thetext}
        Dim words As New List(Of String)
        For Each s As String In Lines
            Dim mc As MatchCollection = re.Matches(s)
            For Each m As Match In mc
                words.Add(m.Groups("w").Value)
            Next
            RawRich4.Text = String.Join(Environment.NewLine, words.ToArray)
        Next
        'This is where I need help with the code. how to have "words" putout  "c:\temp\" & RawRich4.Text & ".pdf" with each keyword name

        Dim rawtoprint As String = String.Join(Environment.NewLine, words.ToArray)
        Dim defname As String = Nothing
        defname = RawRich4.Text
        rawtoprint = "c:\temp\" & RawRich4.Text & ".pdf"

        Dim psi As New System.Diagnostics.ProcessStartInfo()
        psi.UseShellExecute = True
        psi.Verb = "print"
        psi.WindowStyle = ProcessWindowStyle.Hidden
        psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
        psi.FileName = (rawtoprint) ' this is where the error occurs it doesn't  send both files separately to the printer, it tries to send it as one name
        MessageBox.Show(rawtoprint) ' This is just to test the output, this will be removed. 
        'Process.Start(psi)

    End Sub

2 个答案:

答案 0 :(得分:0)

更新

Imports System.Text.RegularExpressions

Module Program
Sub Main()      
    Dim pdffilename As String = RawTextbox.Text
    Dim filepath = "c:\temp\" & RawTextbox.Text & ".pdf"
    Dim thetext As String
    thetext = GetTextFromPDF(filepath)        ' converts pdf to text from a function I didnt show.
    'thetext = "Random text here and everywhere ASM00200207 1 1 same here bah boom 12303 doh hel232 ASM00200208 1 2 "

    Dim pattern As String = "(?i)[\t ](?<w>((asm)|(asy)|(717)|(ssm)|(715)|(818))[a-z0-9]*)[\t ]"

    For Each m As Match In rgx.Matches(thetext, pattern)
        'Console.WriteLine("C:\temp\" & Trim(m.ToString) & ".pdf")
        RawPrintFunction("C:\temp\" & Trim(m.ToString) & ".pdf")
    Next

End Sub

Function RawPrintFunction(ByVal rawtoprint As String) As Integer
    Dim psi As New System.Diagnostics.ProcessStartInfo()
    psi.UseShellExecute = True
    psi.Verb = "print"
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    MessageBox.Show(rawtoprint) This will be removed, this is just for testing to see what files will be printed
    'Process.Start(psi) This will be uncomment.
    return 0
End Function

End Module

答案 1 :(得分:0)

如果我没有误解代码 - 因为我无法在此处测试和运行 - 您可以遍历存储在words变量中的文件名并将其发送到打印机。以下是如何执行此操作的示例:

....
....
Dim Lines() As String = {thetext}
Dim words As New List(Of String)
For Each s As String In Lines
    Dim mc As MatchCollection = re.Matches(s)
    For Each m As Match In mc
        words.Add(m.Groups("w").Value)
    Next
    RawRich4.Text = String.Join(Environment.NewLine, words.ToArray)
Next

For Each fileName As String In words
    Dim rawtoprint As String 
    rawtoprint = "c:\temp\" & fileName & ".pdf"

    Dim psi As New System.Diagnostics.ProcessStartInfo()
    psi.UseShellExecute = True
    psi.Verb = "print"
    psi.WindowStyle = ProcessWindowStyle.Hidden
    psi.Arguments = PrintDialog1.PrinterSettings.PrinterName.ToString()
    psi.FileName = (rawtoprint) ' this is where the error occurs it doesn't  send both files separately to the printer, it tries to send it as one name
    MessageBox.Show(rawtoprint) ' This is just to test the output, this will be removed. 
    'Process.Start(psi)
Next