2

ASP:

<asp:Label ID="Label1" runat="server" Text="Folder Location: "></asp:Label> <asp:TextBox ID="tbFolder" runat="server"></asp:TextBox> 
<br /><br />
<asp:Label ID="Label2" runat="server" Text="Destination Folder: "></asp:Label> <asp:TextBox ID="tbDestination" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="btnExecute" runat="server" Text="Button" OnClick="btnExecute_Click" />

code-behind:

public void btnExecute_Click(object sender, EventArgs e)
{
    try
    {
        strFolder = tbFolder.Text;
    }
    catch (Exception)
    {
        strFolder = "";
    }
    try
    {
        strDestination = tbDestination.Text;
    }
    catch (Exception)
    {
        strDestination = "";
    }
    try
    {
        strFileArray = Directory.GetFiles(strFolder, "*.tif");
        meregTiff(strFileArray);
    }
    catch (Exception)
    {
    }
}
public void meregTiff(string[] files)
{
    // Create the PDF with the proper parameters (change as you please)
    iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

    // Ensure the path to the folder is located where all the merged TIFF files will be saved as a PDF
    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(strDestination + "/result2.pdf", System.IO.FileMode.Create));

    // Load each tiff files and convert it into a BITMAP and save it as a PDF
    // Recommendation: Use TRY/CATCH method to ensure any errors are handled properly...
    foreach (string image in files)
    {
        try
        {
            str = image.Substring(image.LastIndexOf("\\"));
            bm = new System.Drawing.Bitmap(Server.MapPath("~/TiffImages" + str)); //modify this to ensure the file exists (can be same as the page_load method)
            total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
        }
        catch (Exception ce) //getting error here... Parameter is invalid.
        {
        }

        document.Open();
        iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
        for (int k = 0; k < total; ++k)
        {
            bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
            // scale the image to fit in the page 
            img.ScalePercent(72f / img.DpiX * 100);
            img.SetAbsolutePosition(0, 0);
            cb.AddImage(img);
            document.NewPage();
        }
    }
    document.Close();
}

Getting an error here: catch (Exception ce) //getting error here... Parameter is invalid.

How can I resolve it so the files are taken from the tbFolder folder and saved as one PDF to the tbDestination folder.

SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122
  • 2
    FYI: Assigning a value to a string (as you do in `strFolder = tbFolder.Text;`) will never ever throw an exception. so there' absolutly no need to embrace the string assignment with try catch! – Peter Schneider May 12 '15 at 17:49
  • What happens when you set breakpoints in your try? Can you narrow it down to one line? – Ross Bush May 12 '15 at 17:52
  • @PeterSchneider I was testing but then realized what you just said. Thank you. – SearchForKnowledge May 12 '15 at 17:53
  • I updated the line to this: `bm = new System.Drawing.Bitmap(strFolder + str.Split('\\')[0]); //modify this to ensure the file exists (can be same as the page_load method)` but I keep getting `Parameters are not valid` error. – SearchForKnowledge May 12 '15 at 17:54

2 Answers2

3

First of all in your case the mergeTiff method should have a Document property, where you pass in the document you create once, because right at the moment you are creating several documents where each document contains all tiffs - at least the are all saved in result2.pdf.

Just to get you started (didn't test it and it clearly should be further optimized)...

public void btnExecute_Click(object sender, EventArgs e)
{   
    strFolder = tbFolder.Text;  
    strDestination = tbDestination.Text;
    strFileArray = Directory.GetFiles(strFolder, "*.tif");

    // Create the PDF with the proper parameters (change as you please)
    iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

    // Ensure the path to the folder is located where all the merged TIFF files will be saved as a PDF  
    iTextSharp.text.pdf.PdfWriter writer = 
       iTextSharp.text.pdf.PdfWriter.GetInstance(document, 
       new System.IO.FileStream(strDestination + "/result2.pdf", System.IO.FileMode.Create));

    meregTiff(document, writer, strFileArray);

}

public void meregTiff(iTextSharp.text.Document document, iTextSharp.text.PdfWriter pdfWriter, string[] files)
{ 
// Load each tiff files and convert it into a BITMAP and save it as a PDF
// Recommendation: Use TRY/CATCH method to ensure any errors are handled properly...
foreach (string image in files)
{
    try
    {
        str = image.Substring(image.LastIndexOf("\\"));
        bm = new System.Drawing.Bitmap(Server.MapPath("~/TiffImages" + str)); //modify this to ensure the file exists (can be same as the page_load method)
        total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
    }
    catch (Exception ce) //getting error here... Parameter is invalid.
    {
    }

    document.Open();
    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
    for (int k = 0; k < total; ++k)
    {
        bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
        // scale the image to fit in the page 
        img.ScalePercent(72f / img.DpiX * 100);
        img.SetAbsolutePosition(0, 0);
        cb.AddImage(img);
        document.NewPage();
    }
   }
   document.Close();
 }
Peter Schneider
  • 2,879
  • 1
  • 14
  • 17
  • Can you please help me modify it? – SearchForKnowledge May 12 '15 at 17:36
  • I have updated the question because I want to use two textbox to get the folder and the destination folder to save the pdf to. – SearchForKnowledge May 12 '15 at 17:42
  • Thank you. But in the line `bm = new System.Drawing.Bitmap(Server...` is asking for the file twice. – SearchForKnowledge May 12 '15 at 17:58
  • I see what you are doing but it is asking for the folder location again in the `mergeTiff` function. Here is what I have: http://textsaver.flap.tv/lists/llg (I tried to save three TIF file size 3 MB total and the PDF was 63MB. Maybe you can help me edit that code? I am only using the folder location once. – SearchForKnowledge May 12 '15 at 18:00
  • Hmm.. i don't get you right at the moment. Outside the mergeTiff method you are querying the directory to get a list of tiffs. Inside you map this filename to the server path.. should be ok, where's the problem? – Peter Schneider May 12 '15 at 18:03
  • I want to use the files from the folder that I specified for this line: `bm = new System.Drawing.Bitmap(Server.MapPath("~/TiffImages" + str));` and not call another Server.MapPath. – SearchForKnowledge May 12 '15 at 18:04
  • 1
    You're usually inside a Virtual Directory in your ASP.NET application... you are supposed to map the Path.... at least you will need further ajustments, because you are not allowed to read from every folder automatically... – Peter Schneider May 12 '15 at 18:09
  • I understand. But I am already populating the files with the variable `strFileArray`. – SearchForKnowledge May 12 '15 at 18:15
  • Inside Visual Studio yes... but normally that would not work out if you are publishing the application to a webserver... unless the Application Pool Account is allowed to access this path... – Peter Schneider May 12 '15 at 18:19
  • Thank you. What the code is doing taking those files from the `strFileArray` and saving it to the virtual path and then converting it to PDf?I just want to run it once and that's all. – SearchForKnowledge May 12 '15 at 18:22
0
public void btnExecute_Click(string strFolder, string strDestination, string[] strFileArray)
    {
        //strFolder = tbFolder.Text;
        //strDestination = tbDestination.Text;
        //strFileArray = Directory.GetFiles(strFolder, "*.tif");

        // Create the PDF with the proper parameters (change as you please)
        iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);

        // Ensure the path to the folder is located where all the merged TIFF files will be saved as a PDF  
        iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document,
           new System.IO.FileStream(strDestination + "/result2.pdf", System.IO.FileMode.Create));

        meregTiff(document, writer, strFileArray);

    }
    public void meregTiff(iTextSharp.text.Document document, iTextSharp.text.pdf.PdfWriter pdfWriter, string[] files)
    {
        // Load each tiff files and convert it into a BITMAP and save it as a PDF
        // Recommendation: Use TRY/CATCH method to ensure any errors are handled properly...
        foreach (string image in files)
        {
            Bitmap bm = null;
            int total = 0;
            string str = string.Empty;
            try
            {
                str = image.Substring(image.LastIndexOf("\\"));
                bm = new System.Drawing.Bitmap("TiffFolder"+ str); //modify this to ensure the file exists (can be same as the page_load method)
                total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
            }
            catch (Exception ce) //getting error here... Parameter is invalid.
            {
            }

            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = pdfWriter.DirectContent;
            for (int k = 0; k < total; ++k)
            {
                bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
                // scale the image to fit in the page 
                img.ScalePercent(72f / img.DpiX * 100);
                img.SetAbsolutePosition(0, 0);
                cb.AddImage(img);
                document.NewPage();
            }
        }
        document.Close();
    }
Ansvel
  • 1
  • 2
    Hi Ansvel. Please elaborate on your solution a bit more. A plain sample of code is often not that useful. How did it solve the issue? – Noel Widmer May 18 '17 at 18:43