PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Tue Nov 04, 2025 3:48 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Feb 05, 2014 11:07 pm 
Offline

Joined: Wed Feb 05, 2014 9:08 pm
Posts: 2
I am using PdfSharp 1.32.3057.0 GDI+ in a .Net service on Windows 2008 R2 to convert 1-6 page tiff files to pdf. A few of the output pdf files get an "insufficient data for an image" error in Adobe reader. If I reconvert the same image then it will render with no problem.

I have reproduced the issue in test code that just loops converting the same file in my dev environment - I normally get 1-4% errors. Any ideas what may be wrong? A workaround or even a quick way of detecting a problem image would help.

Code:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using PdfSharp.Drawing;
using PdfSharp.Pdf;

namespace TestPdfConvert
{

    class Program
    {
        static void Main(string[] args)
        {
            for (int loop = 1; loop <= 100; loop++)
            {
                System.Console.WriteLine(loop);
                ConvertImage();
            }

            System.Console.ReadLine();
        }

        private static void ConvertImage()
        {
            using (Image inputImage = Image.FromFile(@"D:\test\input\test.tif"))
            {
                using (PdfDocument doc = new PdfDocument())
                {
                    for(int frameNo =0; frameNo<inputImage.GetFrameCount(FrameDimension.Page); frameNo++)
                    {
                        inputImage.SelectActiveFrame(FrameDimension.Page,frameNo);
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            inputImage.Save(memoryStream, ImageFormat.Bmp);
                            using (XImage outputImage = Image.FromStream(memoryStream))
                            {
                                PdfPage page = new PdfPage();
                                page.Width = outputImage.PointWidth;
                                page.Height = outputImage.PointHeight;
                                doc.AddPage(page);
                                using (XGraphics xgraphics = XGraphics.FromPdfPage(page))
                                {
                                    xgraphics.DrawImage(outputImage, 0, 0);
                                }
                            }
                        }
                    }

                    doc.Save( string.Format(@"D:\test\output\test_{0}.pdf",System.DateTime.Now.ToString("HH_mm_ss.fff")) );
                }
            }
        }
    }
}


Attachments:
File comment: Input test file - can get the problem with many files.
test.tif
test.tif [ 14.25 KiB | Viewed 7631 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 06, 2014 9:11 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Hi!

Please try the fix described here:
viewtopic.php?p=5967#p5967

Or go back to version 1.31.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Feb 07, 2014 3:12 am 
Offline

Joined: Wed Feb 05, 2014 9:08 pm
Posts: 2
Hi Thomas

1.31 did not help, but the code patch you pointed me to fixed the problem.
My output images are a little larger now but that is better than having an occasional failure.

Thanks a lot for your help!


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

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider] and 108 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