【问题标题】:How to add a string to multiple string for printing external如何将一个字符串添加到多个字符串以打印外部
【发布时间】:2014-01-18 20:32:02
【问题描述】:

这将是一个很长的问题,但很容易解决。

所以我设法将 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

【问题讨论】:

  • 您有已知的文件名模式吗?例如,假设它们都是 8 个字符或以 ASM 之类的字符串开头?另外,如果你的输入文件名是 1 个文件,你怎么会得到很多文件名?

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


【解决方案1】:

已更新。

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

【讨论】:

  • 我会试一试,看看会发生什么。
  • 我单步执行程序,代码失败,您告诉我将 rawprintfunction 放在 rawrich4 文本之后....等等 这就是我所做的,不知道这是否正确。 RawRich4.Text = String.Join(Environment.NewLine, words.ToArray) = RawPrintFunction("c:\temp\" &amp; RawRich4.Text &amp; ".pdf") 我在步骤中收到此错误。从字符串“ASM00200207 ASM00200208”到类型“Double”的转换无效。也得到这个:Warning 1 Function 'RawPrintFunction' 不会在所有代码路径上返回值。您是否缺少“退货”声明?
  • 当然,但它只是 PDF 中的文本,假设您从 PDF 复制所有内容并将其粘贴到 Richtextbox 中。但是,只提取可选择的文本。上面的代码根据正则表达式中定义的关键字提取某些文本。 随处可见的随机文本 ASM00200207 1 1 此处相同 bah boom 12303 doh hel232 ASM00200208 1 2
  • 我使正则表达式不区分大小写,并使用 for each 循环来处理函数中的字符串。它将输出文件字符串。您可能需要对打印功能进行一些重新编码,因为我没有检查它。
  • 谢谢蒂姆,您的代码运行良好。完美:),稍作改动。 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() psi.FileName = (rawtoprint) Process.Start(psi) ' This will be uncomment. Return 0 End Function
【解决方案2】:

如果我没有误解代码 -因为我无法在此处测试和运行它- 您可以遍历存储在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多