C# code sample to convert multi-page TIFF to PDF using Aspose.PDF for .NET

Hi
I want to convert tiff multi pages files to pdf.
I need a code sample for that.
(I work with Aspose.Pdf.Kit 6.6.)

Thank you

@partnercomm

Please use following code snippet to convert multi-page TIFF into PDF:

Document pdf1 = new Document();
MemoryStream ms = new MemoryStream();
new FileStream(dataDir + @"TestTifFile.tif", FileMode.Open).CopyTo(ms);
Bitmap myimage = new Bitmap(ms);

FrameDimension dimension = new FrameDimension(myimage.FrameDimensionsList[0]);
int frameCount = myimage.GetFrameCount(dimension);

for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
 Page sec = pdf1.Pages.Add();
 myimage.SelectActiveFrame(dimension, frameIdx);
 MemoryStream currentImage = new MemoryStream();
 myimage.Save(currentImage, ImageFormat.Tiff);
 if (myimage.Width > myimage.Height)
 {
  sec.PageInfo.IsLandscape = true;
 }
 else
 {
   sec.PageInfo.IsLandscape = false;
 }

 Aspose.Pdf.Image imageht = new Aspose.Pdf.Image();
 imageht.ImageStream = currentImage;
 sec.Paragraphs.Add(imageht);
}
pdf1.Save(dataDir + "TifftoPDF.pdf");

Hello
Thank you very much.
The code is not fully supported in version 6.6
I do not have Page.PageInfo property, and also no Aspose.Pdf.Image object.
What is the alternatives in 6.6?

@partnercomm

Please note that it is recommended to use latest version always as it contains more fixes and features to offer. Furthermore, the support is provided on the basis of latest available version. We request you to please upgrade your API to latest version and let us know in case you face any issue.

I’m trying my best to purchase the latest version. I hope it will succeed.
Anyway, meanwhile I have to support my clients with the version we’ve purchase in the past.
I know, of course, that we are not under support, and this support is voluntary and free.
Even-though, if there is any working sample, or information about the alternatives I have, it can be very kind.

@partnercomm

We always intend to assist our customers in the best way. Actually, there are a lot of changes in Classes and Methods which have been made since the version you are using. We have to investigate for the code snippet which could be compatible with the version that you are using.

For the purpose, we have created an investigation ticket as PDFNET-47651 in our issue tracking system. We will surely investigate the scenario and will share our feedback as soon as the ticket is resolved. Meanwhile, would you kindly share a piece of code snippet which are using in your environment. This would help us in preparing the code snippet accordingly.

Thank you.
Here is my code, but the results are very poor,
because it is not conversion, but kind of copy paste of the image into new pdf.

    public static void convertTifToPdf()
    {
        // Get a list of tiff image files
        string[] files = System.IO.Directory.GetFiles(Global.GlbScannedFilesDirectory, Global.GlbScannedFileId + "*Merged.tif");

        //Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
        Aspose.Pdf.Generator.Pdf pdfDoc = new Aspose.Pdf.Generator.Pdf();
        
        
        // Navigate through the files and them in the pdf file
        foreach (string myFile in files)
        {

            //Create a section
            Aspose.Pdf.Generator.Section pdfSection = new Aspose.Pdf.Generator.Section(pdfDoc);

            //Create an image
            Aspose.Pdf.Generator.Image pdfImage = new Aspose.Pdf.Generator.Image(pdfSection);

            //Add a paragraph to the section
            pdfSection.Paragraphs.Add(pdfImage);

            //Get the tiff file path
            pdfImage.ImageInfo.File = files[0];

            //Get the image from the tiff file
            pdfImage.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
            
            // Load all tiff files in byte array
            FileStream fs = new FileStream(myFile, FileMode.Open, FileAccess.Read);
            byte[] tmpBytes = new byte[fs.Length];
            fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

            MemoryStream mystream = new MemoryStream(tmpBytes);
            Bitmap b = new Bitmap(mystream);

            FrameDimension dimension = new FrameDimension(b.FrameDimensionsList[0]);
            Int32 framesCount = b.GetFrameCount(dimension);

            //Add image to document
            pdfDoc.Sections.Add().Paragraphs.Add(pdfImage);

            // Set margins so image will fit, etc.
            pdfDoc.Sections[0].PageInfo.Margin.Top = 5;

            pdfDoc.Sections[0].PageInfo.Margin.Bottom = 5;

            pdfDoc.Sections[0].PageInfo.Margin.Left = 5;

            pdfDoc.Sections[0].PageInfo.Margin.Right = 5;

            pdfDoc.PageSetup.PageWidth = (b.Width / b.HorizontalResolution) * 72;

            pdfDoc.PageSetup.PageHeight = (b.Height / b.VerticalResolution) * 72;



            // Set IsBlackWhite property to true for performance improvement
            pdfImage.ImageInfo.IsBlackWhite = true;
            pdfImage.ImageInfo.ImageStream = mystream;
            pdfImage.ImageScale = 0.95F;
        }

        // Save the Pdf
        Global.GlbFileToSavePath = Global.GlbScannedFilesDirectory + Global.GlbScannedFileId + ".pdf";
        pdfDoc.Save(Global.GlbFileToSavePath);
    }

@partnercomm

Thanks for sharing sample code snippet.

We will investigate the ticket on the basis of this code snippet and let you know as soon as there are some additional updates.

@partnercomm

We would like to share with you that new generator uses following code snippet for tiff, please try to use it and let us know about your feedback:

string outFile = "output.pdf";
Document doc = new Document();
Page page = doc.Pages.Add();
Image image1 = new Image();
image1.File = "input.tif";
page.Paragraphs.Add(image1);
doc.Save(outFile);