0

I'm having an issue where rather small tiff files (< 300 KB) gets blown up to like 74 MB when converted into a PDF.

using (var img_collection = new MagickImageCollection())
{
      using (var tif_collection = new MagickImageCollection(Convert.FromBase64String(base64_string_of_tif_image), MagickFormat.Tif))
      {
            foreach (MagickImage tif in tif_collection)
            {
                  img_collection.Add(new MagickImage(tif.ToByteArray())
                  {
                       Format = MagickFormat.Pdf                                    
                  });
             }    
             tif_collection.Dispose();
       }

       img_collection.Write("output.pdf", MagickFormat.Pdf);
       img_collection.Dispose();
 }

Any ideas as to how I can prevent that from happening? The tif files are actually just scans of text.

Aidal
  • 799
  • 4
  • 8
  • 33
  • Can you post a link to input TIFF image and output PDF file so we can take a look at them? – iPDFdev Apr 28 '23 at 08:20
  • I can't do that. They are company invoices, so I'm not allowed to do that. – Aidal Apr 28 '23 at 09:01
  • See if you can find a non-confidential TIFF image that exhibits the same behavior. Check the image with IrfanView to make sure it has the same bits per pixel/compression/etc like your problematic TIFFs – iPDFdev Apr 28 '23 at 13:39
  • They are all invoices, so that isn't really possible for me to do. So what if I wanted to compress or whatever the PDF, if I could sense that it was above a certain size (bytes) prior to actually writing it or after, which ever is possible? - what would be the best way to do that? – Aidal Apr 29 '23 at 17:17
  • Nobody knows a way/ways to somehow compress (decrease in size) resulting PDFs with this library without me providing a TIFF (which I can't) ? – Aidal May 04 '23 at 08:13
  • @Aidal I know a couple ways of doing this, working on a solution - will post it once I'm happy with it myself – jarodsmk Jul 17 '23 at 12:45
  • 1
    I think I actually might have found a solution or alternative myself, but I don't remember at this time what I did. Currently I'm on vacation but I'll will check when I'm back. – Aidal Jul 19 '23 at 01:24
  • Awesome - I'll post mine anyway in the next few days, otherwise enjoy your vacation! – jarodsmk Jul 31 '23 at 13:59

1 Answers1

0

I was not able to resolve the issues with the library I was using ImageMagick.NET, so the solution I found was to switch to another library which I had previous been using for other tasks, PDFSharp (pdfsharp-migradoc-gdi to be exact).

The new code to perform the same task with PDFSharp, goes like this:

using (MemoryStream strm = new MemoryStream(Convert.FromBase64String(base64EncodedString)))
                    {
                        strm.Position = 0;

                        using (Image MyImage = Image.FromStream(strm))
                        {
                            using (PdfSharp.Pdf.PdfDocument doc = new PdfSharp.Pdf.PdfDocument())
                            {
                                for (int PageIndex = 0; PageIndex < MyImage.GetFrameCount(FrameDimension.Page); PageIndex++)
                                {
                                    MyImage.SelectActiveFrame(FrameDimension.Page, PageIndex);

                                    PdfSharp.Drawing.XImage img = PdfSharp.Drawing.XImage.FromGdiPlusImage(MyImage);

                                    var page = new PdfSharp.Pdf.PdfPage();

                                    if (img.PixelWidth > img.PixelHeight)
                                    {
                                        page.Orientation = PdfSharp.PageOrientation.Landscape;
                                    }
                                    else
                                    {
                                        page.Orientation = PdfSharp.PageOrientation.Portrait;
                                    }
                                    doc.Pages.Add(page);

                                    PdfSharp.Drawing.XGraphics xgr = PdfSharp.Drawing.XGraphics.FromPdfPage(doc.Pages[PageIndex]);

                                    xgr.DrawImage(img, 0, 0);
                                }

                                doc.Save(outputFilePath);
                                doc.Close();
                            }
                        }

                        strm.Close();
                    }

I hope this can help someone else experiencing the same issues with large PDF files when converting from TIF.

Aidal
  • 799
  • 4
  • 8
  • 33