Click here to Skip to main content
16,016,562 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All
Since I am new to this site I am hoping someone will be able to assist me in this are.
Here my dilemma.

The following code allows me to access multiple.pdf files and then merge them into 1 pdf file. At this point I would like to save the file to a virtual directory. The merge part works great but all I can get accomplished is to transmit the file for download.
Any and all idea are welcome.


Code is below page one Merge cs
C#
public static void MergeFiles(string destinationFile, string[] sourceFiles)
        {
            try
            {
                int f = 0;
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(sourceFiles[f]);
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;
                Console.WriteLine("There are " + n + " pages in the original file.");
                // step 1: creation of a document-object
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
                // step 3: we open the document
                document.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage page;
                int rotation;
                // step 4: we add content
                while (f < sourceFiles.Length)
                {
                    int i = 0;
                    while (i < n)
                    {
                        i++;
                        document.SetPageSize(reader.GetPageSizeWithRotation(i));
                        document.NewPage();
                        page = writer.GetImportedPage(reader, i);
                        rotation = reader.GetPageRotation(i);
                        if (rotation == 90 || rotation == 270)
                        {
                            cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                        }
                        else
                        {
                            cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                        }
                        Console.WriteLine("Processed page " + i);
                    }
                    f++;
                    if (f < sourceFiles.Length)
                    {
                        reader = new PdfReader(sourceFiles[f]);
                        // we retrieve the total number of pages
                        n = reader.NumberOfPages;
                        Console.WriteLine("There are " + n + " pages in the original file.");
                    }
                }
                // step 5: we close the document
                document.Close();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                Console.Error.WriteLine(e.StackTrace);
            }
        }
    }

End file
Posted
Updated 6-Jul-10 19:30pm
v2
Comments
Sandeep Mewara 7-Jul-10 1:31am    
Use PRE tags for code formatting. It's readable then!

Why would you want to save the file to a directory on the server ? What have you tried, and what errors do you get ?
 
Share this answer
 
It difficult to explain without some background but here goes.
I am the owner of this website but not the original programmer. He gone and I have to pickup the pieces

This website has members that access a specific folder via ftp from the iphone and Ipad. What I am trying to accomplish is to take the merged file that is being created via the merge.cs and save it to that folder so that they may access it via FTP. Then once the file is created and if the member opts in I also want to email it as an attachment and send it via SMTP. But right now One thing at a time. I am not very experienced in programming in C# asp.net or any other language. So just call me a newbie. Any help would be appreciated. This is the rest of the page.
I have put bits and piece from other webpage that are currently working on the site but I am stuck on this tempfile issue.

C#
private void SaveFile(string tempFile)
       {
           long userId = -1;
           userId = Convert.ToInt64(Session["userId"]);
           foreach (UploadedFile file in tempFile)
               if (file.ContentLength < 20000000)
               {
                   if (userId > 0)
                   {
                       UserDAO objUserDAO = new UserDAO();
                       User objUser = objUserDAO.GetUser(userId);
                       if (objUser != null)
                           strTargetFolder = "~/" + strDataDir + "/" + objUser.UserName;
                       string targetFolder = Server.MapPath(strTargetFolder);
                       string targetFileName = Path.Combine(targetFolder, file.GetName());
                       targetFileName = getFileName(strTargetFolder, targetFileName, targetFileName, 0);
                       if (!Directory.Exists(targetFolder))
                       {
                           Directory.CreateDirectory(targetFolder);
                       }
                       file.SaveAs(targetFileName);
                   }
               }


       }


       protected void RadUpload1_FileExists(object sender, Telerik.Web.UI.Upload.UploadedFileEventArgs e)
       {
           int counter = 1;
           UploadedFile file = e.UploadedFile;
           string targetFolder = Server.MapPath(RadUpload1.TargetFolder);
           string targetFileName = Path.Combine(targetFolder, file.GetNameWithoutExtension()) + counter.ToString() + file.GetExtension();

           while (File.Exists(targetFileName))
           {
               counter++;
               targetFileName = Path.Combine(targetFolder, file.GetNameWithoutExtension()) + counter.ToString() + file.GetExtension();
           }

           file.SaveAs(targetFileName);
           /*if (userId > 0)
           {
               FileCabinetDAO objFileCabinetDAO = new FileCabinetDAO();
               FLMapping.FLLibrary.Model.FileCabinet objFileCabinet = objFileCabinetDAO.getFileCabinet
               if (objBGC != null)
               {
                   BGCdocsDAO objBGCdocsDAO = new BGCdocsDAO();
                   BGCdocs objBGCdocs = new BGCdocs();

                   objBGCdocs.BGCdetails = objBGC;
                   objBGCdocs.FilePath = file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension();//file.GetName();
                   objBGCdocs.IsCSA = false;
                   objBGCdocsDAO.save(objBGCdocs);
               }
           }*/
       }

       protected string getFileName(string targetFolder, string originalFilePath, string filePath, int counter)
       {
           if (File.Exists(filePath))
           {
               counter++;
               filePath = Server.MapPath(Path.Combine(targetFolder, Path.GetFileNameWithoutExtension(originalFilePath)) + counter.ToString() + Path.GetExtension(filePath));
               filePath = getFileName(targetFolder, originalFilePath, filePath, counter);
           }
           return filePath;
       }



       // Need to keep working here on attachment and then also saving it in member ftp Directory BA 070510
       private void SendEmail(string strFileName)
       {
           long userId = long.Parse(Session["UserID"].ToString());
           UserDAO objUserDAO = new UserDAO();
           User objUser = objUserDAO.GetUser(Convert.ToInt64(userId));
           string strSendEmail = null;
           if(objUser.ProflightEmail != null)
               strSendEmail = objUser.ProflightEmail.Trim();
           else
               strSendEmail = objUser.Email.Trim();

           string UsName = objUser.LastName;

           MailMessage mailMessage = new MailMessage ();
           mailMessage.From = new MailAddress("NoReply@Proflightlog.com");
           mailMessage.To.Add(strSendEmail);


           mailMessage.Subject = "ProFlightLog Charting Service -TPP Procedures Attached";
           mailMessage.Body = FormatMessage1(UsName);

            if (strFileName != null)
            {

                // Attachment at = new Attachment(Server.MapPath("~/Uploaded/txt.doc"));
                    Attachment attach = new Attachment(Server.MapPath(strFileName));
                    /* Attach the newly created email attachment */
                    mailMessage.Attachments.Add(attach);
                    /* Store the attach filename so we can delete it later */
                   string attach1 = strFileName;

            }
       }
 
Share this answer
 
v2
Comments
Christian Graus 6-Jul-10 20:18pm    
So, the question remains, why does this not work ? What sort of errors do you get ? What happens when you step through the code ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900