PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Multi-Page tiff to PDF Page size problem
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1774
Page 1 of 1

Author:  gfahd [ Tue Sep 06, 2011 5:51 pm ]
Post subject:  Multi-Page tiff to PDF Page size problem

Hello,
I am using PDFSharp Library 1.3 with vb.net 2008. I am dealing with a multi-page tiff file with B5 page size and 8BPP resolution.
PDFSharp converts the file to pdf no problem, but with strange results: the first page is cut off to A5 size. The second page width is squished together resulting in very skinny tall page. The third page weird color. I am reading the tiff page size and setting PDFDoc. if I do the conversion again, i don't get same results. very strange. File too big to attach. Thanks for any help. It is driving me crazy. here is the function:
Code:
Public Function OpenImage(ByVal vsImgSrcPath As String, Optional ByRef vsError As String = "") As Boolean

        Dim loImage As Image = Nothing 'Used to store image information
        Dim loFrameDimension As System.Drawing.Imaging.FrameDimension = Nothing 'Used to store Image frame dimension information
        Dim liFrameCount As Integer = 0 'It is used to store Image frame count information
        Dim loXGraphics As PdfSharp.Drawing.XGraphics = Nothing
        Dim loXImage As PdfSharp.Drawing.XImage = Nothing
        Dim liImgWidth As Integer
        Dim liImgHeight As Integer


        If Not System.IO.File.Exists(vsImgSrcPath) Then
            vsError = "Image File not found: " & vsImgSrcPath
            Return False
        End If

        If moPDFDoc IsNot Nothing Then
            vsError = "Another document already open. Close it before opening a new one."
            Return False
        End If

        Try

            loImage = Image.FromFile(vsImgSrcPath)
            loFrameDimension = New System.Drawing.Imaging.FrameDimension(loImage.FrameDimensionsList(0))
            liFrameCount = loImage.GetFrameCount(loFrameDimension)

            moPDFDoc = New PdfDocument()

            For index As Integer = 0 To liFrameCount - 1
                'Select active frame
                loImage.SelectActiveFrame(loFrameDimension, index)
                moPDFDoc.Pages.Add(New PdfPage())
                liImgWidth = (loImage.Width * 72 / loImage.HorizontalResolution)
                liImgHeight = (loImage.Height * 72 / loImage.VerticalResolution)
                moPDFDoc.Pages(index).Width = liImgWidth
                moPDFDoc.Pages(index).Height = liImgHeight
                loXGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(moPDFDoc.Pages(index))

                loXImage = loImage
                loXGraphics.DrawImage(loXImage, 0, 0)
            Next

            miNumOfPages = moPDFDoc.PageCount

            Return True

        Catch ex As Exception
            vsError = ex.Message
            Return False
        Finally
            If loXGraphics IsNot Nothing Then
                loXGraphics.Dispose()
                loXGraphics = Nothing
            End If
            If loFrameDimension IsNot Nothing Then
                loFrameDimension = Nothing
            End If
            If loImage IsNot Nothing Then
                loImage.Dispose()
                loImage = Nothing
            End If


        End Try

    End Function

Author:  Thomas Hoevel [ Wed Sep 07, 2011 7:40 am ]
Post subject:  Re: Multi-Page tiff to PDF Page size problem

Hi!

I don't see anything in your code that is obviously wrong.

A quote from the help file on SelectActiveFrame:
"Selects the frame specified by the dimension and index. The possible values of the dimensionID parameter are properties of the FrameDimension. These can be used to identify an image by its time, resolution, or page number."

Are you sure the first dimension is page number?

BTW: You could make all page a fixed size (e.g. A4) and use DrawImage with size parameters to scale the image accordingly (it takes three lines of code to scale the image while maintaining the aspect ratio). But that should make no difference with respect to weird colours.

Author:  gfahd [ Thu Sep 08, 2011 7:57 pm ]
Post subject:  Re: Multi-Page tiff to PDF Page size problem

Thomas Hoevel wrote:
Hi!

I don't see anything in your code that is obviously wrong.

A quote from the help file on SelectActiveFrame:
"Selects the frame specified by the dimension and index. The possible values of the dimensionID parameter are properties of the FrameDimension. These can be used to identify an image by its time, resolution, or page number."

Are you sure the first dimension is page number?

BTW: You could make all page a fixed size (e.g. A4) and use DrawImage with size parameters to scale the image accordingly (it takes three lines of code to scale the image while maintaining the aspect ratio). But that should make no difference with respect to weird colours.


Thanks for your Reply Thomas.
It appears that the first dimension is page number and that is working fine.
I dont want to fix the page size, the file could have mix page sizes as well as resolution and colour mode (B&W, colour, Gray).
so basically, i need to read each page size and drawimage with the same dimensions. but i am getting strange results. for example, my A4 sample has 3 pages, Gray (8BIT 200 DPI), Colour (24BIT 200DPI) and B&W (1BIT 300DPI). the gray and B&W are ok, proper dimension. the width of the colour page is squished together very Narrow. Any ideas why that might be happening? is there a way i can send you the tiff sample and the PDF results?

Thanks for your help, it is driving me crazy.

Author:  Thomas Hoevel [ Tue Sep 13, 2011 9:31 am ]
Post subject:  Re: Multi-Page tiff to PDF Page size problem

Hi!

I tried the TIFF file you sent.
GDI+ cannot handle the second page correctly - PDFsharp receives incorrect data from GDI+ and stores this incorrect data (an incomplete, distorted image) into PDF.

Here's the C# code that stores the second page in a BMP file:
Code:
Image image = Image.FromFile(@"F:\$$$PDFsharp\98.tif");
FrameDimension frameDimension = new FrameDimension(image.FrameDimensionsList[0]);
image.SelectActiveFrame(frameDimension, 1);
image.Save(@"F:\$$$PDFsharp\98_2.bmp", ImageFormat.Bmp);

Under Windows 7 I get a BMP with a distorted image - just like the image you have in your PDF. Didn't try Windows XP yet (some images work under XP but not unter W7 :cry: ).

You could use TIFF lib to read the TIFF files (currently I don't have a better idea).

Author:  Thomas Hoevel [ Tue Sep 13, 2011 9:48 am ]
Post subject:  Re: Multi-Page tiff to PDF Page size problem

I found a workaround:
Code:
Image image = Image.FromFile(@"F:\$$$PDFsharp\98.tif");
FrameDimension frameDimension = new FrameDimension(image.FrameDimensionsList[0]);

image.SelectActiveFrame(frameDimension, 1);
image.Save(@"F:\$$$PDFsharp\98_2.tif", ImageFormat.Tiff);

image = Image.FromFile(@"F:\$$$PDFsharp\98_2.tif");
image.Save(@"F:\$$$PDFsharp\98_2a.bmp", ImageFormat.Bmp);

The first call to "image.Save" creates a single-page TIFF (saving as BMP or PNG does not work here).
The second call to "image.Save" then creates a correct BMP.

The workaround: create single-page TIFF files from the multi-page TIFF file, then add the single-page files to the PDF.
This will increase CPU usage (use MemoryStream for the temporary images), but can be achieved with just a few lines of code.

Re: PDF file size
Images are stored using LZW compression. If TIFF file is smaller than PDF file, this could be due to advanced compression in TIFF (line differentiation) which AFAIK is not supported in PDF.
But do not compare sizes while you don't get correct PDF files.

Author:  gfahd [ Tue Sep 13, 2011 5:38 pm ]
Post subject:  Re: Multi-Page tiff to PDF Page size problem

Thanks very much Thomas for all your work. I will implement these workaround this week sometimes.

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