Page 1 of 1

Merging TIFF files in multi page with C#

Posted: 2019-03-20T09:51:18-07:00
by SofiaRodrigues25
Hi there! My name is Sofia and I'm trying to merge various TIFF files into a multi page one.
What I want to do is convert PDF to TIFF and then, these TIFF files, merge them.
I already can convert PDF to TIFF with this code (using ImageMagick):

Code: Select all

public void PDFToTIFF(string output)
        {
            MagickReadSettings settings = new MagickReadSettings();
            // Settings the density to 500 dpi will create an image with a better quality
            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");
                        image.Dispose();
                    }
                    images.Dispose();
                }
            }
        }
Now I want to merge those files into a multi page TIFF one.
I know how to do it using command line, but I don't know how to do it in C#. If someone knows or can help me, please answer this post.

Thank you in advance!
Sofia Rodrigues

Re: Merging TIFF files in multi page with C#

Posted: 2019-03-20T11:24:58-07:00
by fmw42
I do not not C#, but you should be able to convert from a multipage PDF to a multipage TIFF in command line directly as

Code: Select all

convert -density XX image.pdf image.tif
No need to extract individual tiff files from the PDF

Re: Merging TIFF files in multi page with C#

Posted: 2019-03-21T02:29:29-07:00
by SofiaRodrigues25
@fmw42 but when I merge TIFF files, the output it's one image above another and not multiple pages.
I convert PDF in TIFF and the merge the TIFFs.

There is the link to the result: https://ibb.co/PmrFdRP

The code is this one:

Code: Select all

       public void MergeTIFF(string output)
         {
            string[] ficheiros = GetFilesTemp();
            using (MagickImageCollection images = new MagickImageCollection())
            {
                foreach (string fich in ficheiros)
                {
                    // Add the first image
                    MagickImage image = new MagickImage(fich);
                    image.Format = MagickFormat.Tif;
                    image.Depth = 8;
                    images.Add(image);
                }

                // Create a mosaic from both images
                using (IMagickImage result = images.Mosaic())
                {
                    // Save the result
                    result.Write(output + "\\FicheiroMultiPage.tiff");
                }
                images.Dispose();
            }           
        }

Re: Merging TIFF files in multi page with C#

Posted: 2019-03-21T02:50:01-07:00
by dlemstra
You should call Write on the images instead of doing the mosaic.

Re: Merging TIFF files in multi page with C#

Posted: 2019-03-21T04:35:00-07:00
by SofiaRodrigues25
@dlemstra how should I do that?
I cannot do it this way:

Code: Select all

                using (IMagickImage result = images.Write())
                {
                    // Save the result
                    result.Write(output + "\\FicheiroMultiPage.tif");
                }
Because no overload takes 0 arguments, and I cannot use

Code: Select all

images.Write(output + "\\FicheiroMultiPage.tif")
, because it says I cannot use void in IMagickImage...
So, how should I do it?

Re: Merging TIFF files in multi page with C#

Posted: 2019-03-22T23:01:30-07:00
by dlemstra
It should be images.Write(output + "\\FicheiroMultiPage.tif") instead of the using block that you have now.

Re: Merging TIFF files in multi page with C#

Posted: 2019-03-25T02:04:30-07:00
by SofiaRodrigues25
Thank you dlemstra.
I already figure it out with another library but I think that would work either.

Thanks for your time and answers!