Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

itext - file list from ftp and join pdf with itextSharp in C#

I have the following problem I am listing files from an ftp server and filling them to a list:

public IActionResult listarPdf(CarrierRequest carrier)
        {
            
            var ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://tiendashipi.com/W9/");

            ftpRequest.Credentials = new NetworkCredential("[email protected]", "xxx");
            
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            
            
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            
            var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            
            var ftpStream = ftpResponse.GetResponseStream();

            StreamReader streamReader = new StreamReader(ftpResponse.GetResponseStream());
           

            List<string> directories = new List<string>();

            string line = streamReader.ReadLine();
            
            while (!string.IsNullOrEmpty(line))
            {
                directories.Add(line);
                line = streamReader.ReadLine();
            }

            streamReader.Close();

            Console.WriteLine("Estatus al listar el contenido del folter {0}", 
            ftpResponse.StatusDescription);

           
            UnirListaPdf(directories, "Hola.pdf");

            ftpResponse.Close();

            //return File(ftpStream, contentType, carrier.DocRef);

            return Ok();
        }

and then I try to join the list of my pdf files with itextsharp with this method:

public void UnirListaPdf(List<String> InFiles, String OutFile)
{
    using (FileStream stream = new FileStream(OutFile, FileMode.Create)) 
    using (Document doc = new Document()) 
    using (PdfCopy pdf = new PdfCopy(doc, stream))
    {
        doc.Open();
        iTextSharp.text.pdf.PdfReader reader = null; 
        PdfImportedPage page = null; 
        InFiles.ForEach(file => 
        { 
            reader = new iTextSharp.text.pdf.PdfReader(file); 
            for (int i = 0; i < reader.NumberOfPages; i++) 
            { 
                page = pdf.GetImportedPage(reader, i + 1); 
                pdf.AddPage(page); 
            }
            pdf.FreeReader(reader); 
            reader.Close(); 
        });
        
    }
}

but when trying to read the files I get the following error message:

System.IO.IOException: '1.pdf not found as file or resource.'

but the file does exist inside my ftp, what am I doing wrong?

question from:https://stackoverflow.com/questions/66049118/file-list-from-ftp-and-join-pdf-with-itextsharp-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...