Combine TIFF files on a multi-page TIFF C#

Asked

Viewed 309 times

1

I’m making a program to convert and concatenate files. I wanted, after converting the PDF files into TIFF, to combine them into a single multi-page TIFF file.

I’ve been looking for a long time and I still haven’t found any code that worked like I wanted.

Then I convert the PDF files to TIFF and store them in a temporary folder. Then I have an array of strings that will contain all these files from the temporary folder (through a function GetFiles()).

I want you to combine the files from that array into a single multi-page TIFF. Someone has the solution or can direct me to it?

EDIT - My code:

Convert PDF to TIFF (with Imagemagick)

public void PDFToTIFF(string output)
{
    MagickReadSettings settings = new MagickReadSettings();          
    settings.Density = new Density(500);

    string[] ficheiros = GetFiles();
    foreach (string fich in ficheiros)
    {
        string fichwithout = Path.GetFileNameWithoutExtension(fich);
        string path = Path.Combine(output, fichwithout);
        using (MagickImageCollection images = new MagickImageCollection())
        {
            images.Read(fich);
            foreach (MagickImage image in images)
            {
                settings.Height = image.Height;
                settings.Width = image.Width;
                image.Format = MagickFormat.Tiff;
                image.Write(path + ".tiff");
            }
        }
     }
 }

Function GetFiles():

 public string[] GetFiles()
 {
     if (!Directory.Exists(@"C:\Users\srodrigues\Documents\ProjetoPAP-SofiaRodrigues\Main\temporario"))
     {

         Directory.CreateDirectory(@"C:\Users\srodrigues\Documents\ProjetoPAP-SofiaRodrigues\Main\temporario");
     }

     DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Users\srodrigues\Documents\ProjetoPAP-SofiaRodrigues\Main\temporario");
     FileInfo[] fileInfos = dirInfo.GetFiles("*.pdf");
     ArrayList list = new ArrayList();
     foreach (FileInfo info in fileInfos)
     {
         if(info.Name != caminho1)
         {
             // HACK: Just skip the protected samples file...
             if (info.Name.IndexOf("protected") == -1)
             list.Add(info.FullName);
         }
     }
     return (string[])list.ToArray(typeof(string));
  }

Thanks in advance.

  • 1

    Maybe it is easier to merge before the conversion, to convert only one file later not? Take a look at this topic: https://stackoverflow.com/questions/808670/combine-two-or-more-pdfs

  • I already have the code to merge PDF. I want to convert to TIFF and merge these files...

  • Good! It would be possible to provide your code or what you have already done to have a base and help you?

  • Hello, I’ve already included the code I’ve already done. There’s something else I can improve on my question since I have -2 negative votes @Mikev?

  • 1

    I think the question is a good one now. In the case of this issue I do not think that negative would be necessary, I think just asking the rest of the information that was 'missing' and waiting would be enough (+1). But do what, a lot of people on the site and many do not think so and are even a little boring.

  • I think that instead of immediately denying, proposing amendments that will help to improve the issue, helps those who ask the question how to improve their issues, and helps those who want to help, to better understand the problem.

Show 1 more comment

1 answer

1


To merge TIFF files into a multi-page TIFF, no reference or some library and third parties are required, just use this function.

I removed the function of this site: Split/Merge TIFF

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

public void mergeTiffPages(string str_DestinationPath)
{
    string[] sourceFiles = GetFiles(); //deve criar uma, tenho o exemplo no código da minha questão
    ImageCodecInfo codec = null;

    foreach (System.Drawing.Imaging.ImageCodecInfo cCodec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
    {
        if (cCodec.CodecName == "Built-in TIFF Codec")
        codec = cCodec;
    }
    foreach (string file in sourceFiles)//para cada ficheiro no array de strings
    {
        try
        {
            EncoderParameters imagePararms = new EncoderParameters(1);
            imagePararms.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);

             if (sourceFiles.Length == 1)
             {
                 File.Copy((string)sourceFiles[0], str_DestinationPath, true);
             }
             else if (sourceFiles.Length > 2)
             {
                 using (System.Drawing.Image DestinationImage = (System.Drawing.Image)(new Bitmap((string)sourceFiles[0])))
                 {
                     DestinationImage.Save(str_DestinationPath, codec, imagePararms); 
                     imagePararms.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);

                      for (int i = 1; i < sourceFiles.Length; i++)
                      {
                           System.Drawing.Image img = (System.Drawing.Image)(new Bitmap((string)sourceFiles[i]));

                           DestinationImage.SaveAdd(img, imagePararms);
                           img.Dispose();
                       }

                       imagePararms.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush);
                       DestinationImage.SaveAdd(imagePararms);
                       imagePararms.Dispose();
                       DestinationImage.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Erro", MessageBoxButtons.OK);
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.