PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Convert Multi-Page TIFF to PDF
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4447
Page 1 of 1

Author:  radarmeegs [ Tue May 30, 2023 1:26 pm ]
Post subject:  Convert Multi-Page TIFF to PDF

I've reviewed a number of samples but was unable to find the documentation on your latest website. I have the following code:
Code:
        private string ConvertTifToBase64ViaPdf3(string docUrl)
        {
            var filePath = Server.MapPath(docUrl);
            using (var myImage = Image.FromFile(filePath))
            {
                var doc = new PdfDocument();

                var totalPages = myImage.GetFrameCount(FrameDimension.Page);

                for (var pageIndex = 0; pageIndex < totalPages; ++pageIndex)
                {
                    myImage.SelectActiveFrame(FrameDimension.Page, pageIndex);
                    using (var img = XImage.FromGdiPlusImage(myImage))
                    {
                        var page = new PdfPage
                        {
                            Orientation = img.PixelWidth > img.PixelHeight ?
                                PageOrientation.Landscape : PageOrientation.Portrait
                        };

                        doc.Pages.Add(page);

                        var xgr = XGraphics.FromPdfPage(doc.Pages[pageIndex], XGraphicsPdfPageOptions.Replace);

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

                var memStream = new MemoryStream();
                doc.Save(memStream);

                memStream.Seek(0, SeekOrigin.Begin);
                var fileBytes = memStream.ToArray();

                doc.Close();

                return Convert.ToBase64String(fileBytes);
            }
        }


The call to `myImage.SelectActiveFrame(FrameDimension.Page, pageIndex)` throws a System.ArgumentException on the 2nd page of a 22 page document; i.e., totalPages is 22 for my sample document. I can get this to work by recreating the image for every page.

What am I missing?

Author:  TH-Soft [ Tue May 30, 2023 2:02 pm ]
Post subject:  Re: Convert Multi-Page TIFF to PDF

radarmeegs wrote:
The call to `myImage.SelectActiveFrame(FrameDimension.Page, pageIndex)` throws a System.ArgumentException on the 2nd page of a 22 page document; i.e., totalPages is 22 for my sample document.
SelectActiveFrame is Windows code and not part of our library.
Beside that, we do not even have the image file where the exception occurs.

Maybe try a different library to read the TIFF files.

Author:  radarmeegs [ Tue May 30, 2023 2:36 pm ]
Post subject:  Re: Convert Multi-Page TIFF to PDF

TH-Soft wrote:
radarmeegs wrote:
The call to `myImage.SelectActiveFrame(FrameDimension.Page, pageIndex)` throws a System.ArgumentException on the 2nd page of a 22 page document; i.e., totalPages is 22 for my sample document.
SelectActiveFrame is Windows code and not part of our library.
Beside that, we do not even have the image file where the exception occurs.

Maybe try a different library to read the TIFF files.


I realize that Image.SelectActiveFrame is not part of PdfSharp. I am basically doing what you suggest here: viewtopic.php?f=2&t=4318.

Does the PDFSharp team have any guidelines/recommendations for handling multi-page TIFF files?

Attachments:
File comment: Here is the file I am testing with.
multi-page.tif
multi-page.tif [ 61.36 KiB | Viewed 3430 times ]

Author:  TH-Soft [ Tue May 30, 2023 2:39 pm ]
Post subject:  Re: Convert Multi-Page TIFF to PDF

radarmeegs wrote:
Does the PDFSharp team have any guidelines/recommendations for handling multi-page TIFF files?
Your code looks OK, but maybe there is something unusual about your TIFF image.

Author:  radarmeegs [ Tue May 30, 2023 2:48 pm ]
Post subject:  Re: Convert Multi-Page TIFF to PDF

I have attached an image that meets your size requirements and still fails. It only has three pages.

Author:  radarmeegs [ Wed May 31, 2023 1:03 pm ]
Post subject:  Re: Convert Multi-Page TIFF to PDF

I got it. I used a MemoryStream to disconnect the Image from the XImage/XGraphics work. This allowed me to move through the original multi-page image using SelectActiveFrame without any problems or reloading the image for each page. I based this on some earlier work I did with iTextSharp.

Here is my new method:

Code:
        private string ConvertTifToBase64ViaPdf3(string docUrl)
        {
            var filePath = Server.MapPath(docUrl);
            using (var myImage = Image.FromFile(filePath))
            {
                var doc = new PdfDocument();

                var totalPages = myImage.GetFrameCount(FrameDimension.Page);

                for (var pageIndex = 0; pageIndex < totalPages; ++pageIndex)
                {
                    myImage.SelectActiveFrame(FrameDimension.Page, pageIndex);

                    XImage img;
                    using (var mem = new MemoryStream())
                    {
                        myImage.Save(mem, ImageFormat.Png);
                        img = XImage.FromStream(mem);
                    }

                    var page = new PdfPage
                    {
                        Orientation = img.PixelWidth > img.PixelHeight ?
                            PageOrientation.Landscape : PageOrientation.Portrait,
                        /*
                         * Set width/height to scale page to image.
                         */
                        Width = XUnit.FromInch(img.PixelWidth / img.HorizontalResolution),
                        Height = XUnit.FromInch(img.PixelHeight / img.VerticalResolution)
                    };

                    doc.Pages.Add(page);
                    var xgr = XGraphics.FromPdfPage(doc.Pages[pageIndex], XGraphicsPdfPageOptions.Replace);
                    xgr.DrawImage(img, 0, 0);
                }

                var memStream = new MemoryStream();
                doc.Save(memStream);

                memStream.Seek(0, SeekOrigin.Begin);
                var fileBytes = memStream.ToArray();

                doc.Close();

                return Convert.ToBase64String(fileBytes);
            }
        }


I need to return a Base64 encoded string in this particular application, but this would work without doing the final bits to accomplish that.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/