How to split multipage TIF into byte arrays for each page to convert TIF to PDF?

245 Views Asked by At

I am using Libtiff.net and PdfSharp to convert multi-page TIF to PDF. The problem I have is that I cannot appear to get the data of a current page/directory and convert it to a byte array in order to pass to XImage.FromStream(myStream). Instead, the code I have writes the same page multiple times rather than each individual page. I cannot use System.Drawing.Common because it is deprecated in the version of .NET I am using. Thanks!

byte[] data = System.IO.File.ReadAllBytes("img.tif");
PdfDocument d = new PdfDocument();

using (MemoryStream ms = new MemoryStream(data))
using (Tiff t = Tiff.ClientOpen("test", "r", ms, new TiffStream()))
{
  int c = t.NumberOfDirectories();
  for (int i = 0; i < c; i++)
  {
    t.SetDirectory((short)i);
    PdfPage p = d.AddPage();
    XGraphics g = XGraphics.FromPdfPage(p);
    XImage img = XImage.FromStream(ms);
    p.Width = img.PointWidth;
    p.Height = img.PointHeight;
    g.DrawImage(img, 0, 0);
  }
}
0

There are 0 best solutions below