Click here to Skip to main content
14,356,490 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I stuck in my Coding.My task is converting tif to pdf. below is my code and it works fine. also it gives me the output. But the problem i m facing is that the pdf which i m getting as an output is double in size as to tif. here first i split the tif into jpeg format and then merge that jpg files into pdf. but i m not able to get the proper size of pdf as it is in tif.

Can any one solved these issue or explain me where i m going to be wrong. i want the pdf size same as the tiff size or 10% increases size not the 50%.

// Load all pdf files.
string[] files = Directory.GetFiles(uxInput.Text, "*.tif");
string outputPath = tempDirectory;
if (!System.IO.Directory.Exists(tempDirectory))
    System.IO.Directory.CreateDirectory(tempDirectory);

foreach (string file in files)
{
    ImageCollection images = new ImageCollection();
    images.Add(file);
    String pdfName = Path.GetFileNameWithoutExtension(file);
    for (int pageNumber = 0; pageNumber < images.Count; pageNumber++)
    {
        images[pageNumber].Save(tempDirectory + "\\" + pageNumber.ToString() + ".jpg");
    }

    string[] files1 = System.IO.Directory.GetFiles(tempDirectory, "*.jpg");

    iTextSharp.text.Document myDocument = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
    //PdfWriter.GetInstance(myDocument, new System.IO.FileStream(uxOutput.Text + "\\" + pdfName + ".pdf", System.IO.FileMode.Create));
    PdfWriter writer = PdfWriter.GetInstance(myDocument, new System.IO.FileStream(uxOutput.Text + "\\" + pdfName + ".pdf", System.IO.FileMode.Create));
    writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_5);
    writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
    writer.SetFullCompression();
for (int i = 0; i <= files1.Length - 1; i++)
    {
        if (myDocument != null)
        {
            myDocument.Open();
            AddImageToPDF(myDocument, tempDirectory + "\\" + (i).ToString() + ".jpg");
        }
    }
    myDocument.Close();
    myDocument = null;
    System.IO.File.Delete(tempDirectory + "\\" + ".jpg");
}


Regards Maahi
Posted
Updated 2-Apr-12 5:28am
v2
Comments
Richard MacCutchan 2-Apr-12 10:29am
   
Corrected <code> tags to <pre> for formatting.
prahalad.gaggar 4-Dec-12 2:03am
   
your heading is very wrong!!!
it should be how to reduce the size of pdf while generating from TIF ?
people gets confused with the current header as how to reduce size of a pdf stored in disk!!!

and please link this question to Top Exports to find the solution as I am also having a similar question!!!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100