Pergunta

I have a utility that converts batches of TIFF images to PDFs using the PDFSharp library. The following code performs the actual conversion. When I open the resulting PDF files in Acrobat Reader, I receive an error message for certain ones stating "Insufficient Data for Image." Others are fine.

What could be causing this? Is there anything I'm missing in the code that could prevent this?

Public Shared Function ConvertImageToPDF(ByVal img As Image) As Byte()
    Using ms As New MemoryStream()
        Using pdf As New PdfDocument()
            Dim pageCount = GetPageCount(img)

            For index = 0 To (pageCount - 1)
                Dim page = New PdfPage()

                Using sourceImage = GetPage(img, index)
                    Using pageImage = XImage.FromGdiPlusImage(sourceImage)
                        page.Width = pageImage.PointWidth
                        page.Height = pageImage.PointHeight

                        pdf.Pages.Add(page)

                        Using xgr = XGraphics.FromPdfPage(pdf.Pages(index))
                            xgr.DrawImage(pageImage, 0, 0)
                        End Using
                    End Using
                End Using
            Next

            pdf.Save(ms, False)
            pdf.Close()
        End Using

        Return ms.ToArray()
    End Using
End Function

Public Shared Function GetPageCount(ByVal img As Image) As Integer
    If (img Is Nothing) Then
        Return -1
    End If

    Return img.GetFrameCount(FrameDimension.Page)
End Function

Public Shared Function GetPage(ByVal img As Image, ByVal pageNumber As Integer) As Image
    img.SelectActiveFrame(FrameDimension.Page, pageNumber)

    Dim ms = New MemoryStream()
    img.Save(ms, ImageFormat.Tiff)

    Return Image.FromStream(ms)
End Function

UPDATE:

If I run the same code over the same TIFF files, then the PDF files that were corrupt before are now OK, and ones that were OK before are now corrupt.

UPDATE 2:

After reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7. Can anyone confirm this or offer a workaround?

Foi útil?

Solução

As stated in my update, after reviewing this connect issue (https://connect.microsoft.com/VisualStudio/feedback/details/584681/system-drawing-image-flags-has-different-value-in-vista-and-windows-7) and the community comment on this MSDN page (http://msdn.microsoft.com/en-us/library/system.drawing.image.save.aspx), it appears the issue is related to an operating system level bug in Windows 7.

This is supported by the comment from PDFsharpTeam.

In addition, when the images are read in Windows XP, the flags property on the image object is set to 77888. On Win7, it is set to 77840. After reviewing the MSDN documentation for the flags property (http://msdn.microsoft.com/en-us/library/system.drawing.image.flags.aspx), the difference is that WinXP flagged the image as a grayscale image (which mine are), but Win7 flagged it as an RGB image. This appears to be a symptom of the problem, but I don't know enough about image formats and color spaces to speak with authority on this.

UPDATE (2014-06-13):

After continuing to experience this problem, I researched a bit further and found a post on the PDFSharp forums mentioning this issue and linking to another post with a fix.

http://forum.pdfsharp.net/viewtopic.php?f=2&t=2729

http://forum.pdfsharp.net/viewtopic.php?p=5967#p5967

Basically, there are two methods in the PdfImage.FaxEncode.cs file that need to be updated.

In both the CountOneBits() and CountZeroBits() methods, replace the following code:

return found + hits;

with

found += hits;
if (found >= bitsLeft)
  return bitsLeft;
return found;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top