PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 6:15 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue May 30, 2023 1:26 pm 
Offline

Joined: Tue May 30, 2023 1:19 pm
Posts: 4
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue May 30, 2023 2:02 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue May 30, 2023 2:36 pm 
Offline

Joined: Tue May 30, 2023 1:19 pm
Posts: 4
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 2300 times ]


Last edited by radarmeegs on Tue May 30, 2023 2:47 pm, edited 1 time in total.
Top
 Profile  
Reply with quote  
PostPosted: Tue May 30, 2023 2:39 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue May 30, 2023 2:48 pm 
Offline

Joined: Tue May 30, 2023 1:19 pm
Posts: 4
I have attached an image that meets your size requirements and still fails. It only has three pages.


Top
 Profile  
Reply with quote  
PostPosted: Wed May 31, 2023 1:03 pm 
Offline

Joined: Tue May 30, 2023 1:19 pm
Posts: 4
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 142 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group