从列表框中读取文件时文件被复制 [英] files are duplicated while reading from listbox
问题描述
我有一个包含子目录的目录,每个子目录都有xml文件.
I have a directory that contains sub-directories, each subdirectory has xml files .
我试图进入目录,并获取XML文件并读取特定节点"Page_number"的xml文件.并且 找到了,获取xml文件名,删除.xml并将其替换为.pdf,然后 然后添加 页号链接到PDF文件名的列表框,然后将列表框插入数组并使用itextsharp,我能够将PDF文件合并到数组中,如果我只有一个文件夹,它可以正常工作,但是如果我有2个文件夹 将2个文件夹的pdf合并为一个PDF,我想要的是单独合并每个包含xml的目录的每个内容.
Im trying to go inside the directory ,and get the XML Files and read the xml files for a specific node "Page_number" and if its found , im getting the xml file name , removing the .xml and replacing it by .pdf , then im adding the the page number concatenatated to the PDF file name to a list box , then im inserting the listbox into array and using itextsharp i am able to combine the PDF files in the array , and its working fine if i only have one folder , but if i have 2 folders the pdfs of the 2 folders are merged into one PDF , what i want is to merge each contents of each directory containing xmls by itself .
下面是我完整的代码,感谢您的帮助.....
below is my complete code , any help is appreciated .....
Imports System.Text.RegularExpressions
Imports itextsharp.text.pdf
Imports System.IO
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
Dim thepath As String = FolderBrowserDialog1.SelectedPath
If thepath = "" Then
MessageBox.Show("NO PATH NO WORK FOR YOU !")
Return
End If
Dim X As String
For Each X In Directory.GetFiles(thepath, "*.xml", SearchOption.AllDirectories)
Dim completepath As String
Dim fileandextension As String = Path.GetFileName(X)
Dim fileNoExtension As String = Path.GetFileNameWithoutExtension(X)
Dim fileextensionAdded As String = fileNoExtension & ".pdf"
completepath = Path.GetDirectoryName(X)
Dim outputpath As String = completepath.Substring(completepath.LastIndexOf("\") + 1)
Dim namingthePDF As String = outputpath & ".pdf"
Dim completenaming As String = completepath & "\" & namingthePDF
TextBox1.Text = completenaming
Dim doc As New XmlDocument()
doc.Load(X)
' Declaring what to search for :
Dim linksnodetofind As XmlNode
Dim root As XmlElement = doc.DocumentElement
linksnodetofind = root.SelectSingleNode("Page_Number")
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' It was found, Read its Inner XML.
If linksnodetofind IsNot Nothing Then
ListBox1.Items.Add(linksnodetofind.InnerXml & "-" & completepath & "\" & fileextensionAdded)
End If
' ListBox1.Items.Add(linksnodetofind.InnerXml & "-" & completepath & "\" & fileextensionAdded)
' ListBox1.Sorted = True
Next
For Each item As String In ListBox1.Items
Dim output As String = item.Substring(item.IndexOf("-") + 1)
ListBox2.Items.Add(output)
Next
Dim source_files As String() = New String(ListBox2.Items.Count - 1) {}
For i As Integer = 0 To ListBox2.Items.Count - 1
Dim s As Object = ListBox2.Items(i)
source_files(i) = s.ToString()
Next
Dim result As [String] = TextBox1.Text
'create Document object
Dim document As New iTextSharp.text.Document()
'create PdfCopy object
Dim copy As New PdfCopy(document, New FileStream(result, FileMode.Create))
'open the document
document.Open()
'PdfReader variable
Dim reader As PdfReader
For nafi As Integer = 0 To source_files.Length - 1
'create PdfReader object
reader = New PdfReader(source_files(nafi))
'merge combine pages
For page As Integer = 1 To reader.NumberOfPages
copy.AddPage(copy.GetImportedPage(reader, page))
Next
Next
'close the document object
document.Close()
Process.Start(TextBox1.Text.ToString())
Threading.Thread.Sleep(0)
End Sub
Georges Naffah
Georges Naffah
推荐答案
George,
George,
我没有遵循,但是您知道Sleep(0)是什么吗?
I'm not following that, but do you know what Sleep(0) is and does?
https://msdn. microsoft.com/en-us/library/d00bd51t(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/d00bd51t(v=vs.110).aspx
*****
您可以发布XML文件的内容之一吗?
Can you post one of the XML file's contents?
这篇关于从列表框中读取文件时文件被复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!