Merging TIFF files in multi page with C#

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Merging TIFF files in multi page with C#

Post by SofiaRodrigues25 » 2019-03-20T09:51:18-07:00

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
Sofia Rodrigues

User avatar
fmw42
Posts: 25748
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Merging TIFF files in multi page with C#

Post by fmw42 » 2019-03-20T11:24:58-07:00

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

SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Re: Merging TIFF files in multi page with C#

Post by SofiaRodrigues25 » 2019-03-21T02:29:29-07:00

@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();
            }           
        }
Sofia Rodrigues

User avatar
dlemstra
Posts: 1585
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Merging TIFF files in multi page with C#

Post by dlemstra » 2019-03-21T02:50:01-07:00

You should call Write on the images instead of doing the mosaic.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Re: Merging TIFF files in multi page with C#

Post by SofiaRodrigues25 » 2019-03-21T04:35:00-07:00

@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?
Sofia Rodrigues

User avatar
dlemstra
Posts: 1585
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Merging TIFF files in multi page with C#

Post by dlemstra » 2019-03-22T23:01:30-07:00

It should be images.Write(output + "\\FicheiroMultiPage.tif") instead of the using block that you have now.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

SofiaRodrigues25
Posts: 7
Joined: 2019-03-20T09:27:22-07:00
Authentication code: 1152

Re: Merging TIFF files in multi page with C#

Post by SofiaRodrigues25 » 2019-03-25T02:04:30-07:00

Thank you dlemstra.
I already figure it out with another library but I think that would work either.

Thanks for your time and answers!
Sofia Rodrigues

Post Reply