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

TIFF to PDF Problems
http://forum.pdfsharp.de/viewtopic.php?f=2&t=790
Page 1 of 1

Author:  mphills [ Wed Jul 15, 2009 1:03 pm ]
Post subject:  TIFF to PDF Problems

Hi all,

I have just recently started using PDFSharp to convert TIFF to PDF with some good results.

But i am having problems with certain compression types of PDF, can anyone help? Is there a problem with certain types of compression?

THANks in advance

Martyn

Author:  Thomas Hoevel [ Wed Jul 15, 2009 2:16 pm ]
Post subject:  Re: TIFF to PDF Problems

Hi, Martyn!

TIFF is not an image format, it's a file system.
PDFsharp relies on the operating system to read the image files.
So we need to know if you use a GDI+ or a WPF build.
We need to know the PDFsharp version you are using.

And we need a sample TIFF file that is not displayed correctly to examine this.

Author:  mphills [ Wed Jul 15, 2009 2:24 pm ]
Post subject:  Re: TIFF to PDF Problems

hi ya,

here is the code i am using

Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Dim tiff As TiffImageSplitter = New TiffImageSplitter()

Public Sub tiff2PDF(ByVal fileName As String)
Dim doc As New PdfDocument()
Dim pageCount As Integer = tiff.getPageCount(fileName)
For i As Integer = 0 To pageCount - 1
Dim page As New PdfPage()
Dim tiffImg As System.Drawing.Image = tiff.getTiffImage(fileName, i)
Dim img As XImage = XImage.FromGdiPlusImage(tiffImg)
page.Width = img.PointWidth
page.Height = img.PointHeight
doc.Pages.Add(page)
Dim xgr As XGraphics = XGraphics.FromPdfPage(doc.Pages(i))
xgr.DrawImage(img, 0, 0)
Next

Dim stream As New MemoryStream()
doc.Save(stream, False)
doc.Close()
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-disposition", "attachment; filename=download.pdf")
Response.AddHeader("content-length", stream.Length.ToString())
Response.BinaryWrite(stream.ToArray())
Response.Flush()
stream.Close()
Response.End()

end sub

here is the class file

Imports Microsoft.VisualBasic
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Public Class TiffImageSplitter
' Retrive PageCount of a multi-page tiff image
Public Function getPageCount(ByVal fileName As [String]) As Integer
Dim pageCount As Integer = -1
Try
Dim img As Image = Bitmap.FromFile(fileName)
pageCount = img.GetFrameCount(FrameDimension.Page)

img.Dispose()
Catch ex As Exception
pageCount = 0
End Try
Return pageCount
End Function

Public Function getPageCount(ByVal img As Image) As Integer
Dim pageCount As Integer = -1
Try
pageCount = img.GetFrameCount(FrameDimension.Page)
Catch ex As Exception
pageCount = 0
End Try
Return pageCount
End Function

' Retrive a specific Page from a multi-page tiff image
Public Function getTiffImage(ByVal sourceFile As [String], ByVal pageNumber As Integer) As Image
Dim returnImage As Image = Nothing

Try
Dim sourceIamge As Image = Bitmap.FromFile(sourceFile)
returnImage = getTiffImage(sourceIamge, pageNumber)
sourceIamge.Dispose()
Catch ex As Exception
returnImage = Nothing
End Try

' String splittedImageSavePath = "X:\\CJT\\CJT-Docs\\CJT-Images\\result001.tif";
' returnImage.Save(splittedImageSavePath);

Return returnImage
End Function

Public Function getTiffImage(ByVal sourceImage As Image, ByVal pageNumber As Integer) As Image
Dim ms As MemoryStream = Nothing
Dim returnImage As Image = Nothing

Try
ms = New MemoryStream()
Dim objGuid As Guid = sourceImage.FrameDimensionsList(0)
Dim objDimension As New FrameDimension(objGuid)
sourceImage.SelectActiveFrame(objDimension, pageNumber)
sourceImage.Save(ms, ImageFormat.Tiff)
returnImage = Image.FromStream(ms)
Catch ex As Exception[url][/url]
returnImage = Nothing
End Try
Return returnImage
End Function
End Class

i downloaded the latest verision of sharpdf

here is the file i am trying to convert too
http://www.ashford.gov.uk/downloads/221656_1.tif

Author:  Thomas Hoevel [ Wed Jul 15, 2009 3:20 pm ]
Post subject:  Re: TIFF to PDF Problems

Thanks for the code.
But which error occurs where?

Windows tells me this is not a valid file so I presume the error occurs here
Code:
Dim tiffImg As System.Drawing.Image = tiff.getTiffImage(fileName, i)

or even earlier.

I'd say (w/o trying your code in a debugger): nothing wrong with PDFsharp here, because the image is not even yet passed to PDFsharp.

BTW: Adobe Photoshop also says it's invalid.
Microsoft Office Picture Manager can show it.

Author:  mphills [ Thu Jul 16, 2009 7:32 am ]
Post subject:  Re: TIFF to PDF Problems

Hi Thomas,

Is it possible to post some code so we can try and work out why it is not working?

Im still very new to PDFSharp so any help will be appreciated and im sure it will help others too

Thanks in advance

Author:  Thomas Hoevel [ Thu Jul 16, 2009 7:51 am ]
Post subject:  Re: TIFF to PDF Problems

You have the program - but you didn't say what you get (a PDF with incorrect picture or no PDF at all) or what error message you get.

If you get no PDF: run the code in a Debugger or add try/catch (whatever that may be in VB) and return Exception.ToString.

It seems there is nothing wrong with PDFsharp here.

Author:  mphills [ Thu Jul 16, 2009 8:03 am ]
Post subject:  Re: TIFF to PDF Problems

thanks with the quick response, here is what i get if i use a try around my code

Cannot save a PDF document with no pages.

Author:  mphills [ Thu Jul 16, 2009 8:04 am ]
Post subject:  Re: TIFF to PDF Problems

sorry, this might be more helpful

System.InvalidOperationException: Cannot save a PDF document with no pages. at PdfSharp.Pdf.PdfDocument.DoSave(PdfWriter writer) at PdfSharp.Pdf.PdfDocument.Save(Stream stream, Boolean closeStream) at Default4.tiff2PDF(String fileName) in c:\inetpub\wwwroot\martyndev\MyTestBed2\Default4.aspx.vb:line 40

Author:  Thomas Hoevel [ Thu Jul 16, 2009 8:27 am ]
Post subject:  Re: TIFF to PDF Problems

mphills wrote:
Cannot save a PDF document with no pages.


So
Code:
Dim pageCount As Integer = tiff.getPageCount(fileName)

returns 0 and AddPage is never called.

It's a TiffImageSplitter problem, not a PDFsharp problem.

Rule of thumb: if the tools that come with Windows (Paint, Image preview) can't show an image, then the format is most likely not supported by GDI+.

Author:  mphills [ Thu Jul 16, 2009 8:29 am ]
Post subject:  Re: TIFF to PDF Problems

Thanks for your help thomas, so im wasting my time if PDFSharp cannot convert all types of tiff file?

Author:  Thomas Hoevel [ Thu Jul 16, 2009 8:57 am ]
Post subject:  Re: TIFF to PDF Problems

You are using Windows to read the TIFF files.
And Windows cannot read all sorts of TIFF files.

Find a library to get an Image from the TIFF, and PDFsharp will be able to handle it.

mphills wrote:
Thanks for your help thomas, so im wasting my time if PDFSharp cannot convert all types of tiff file?

This is not a PDFsharp problem - you don't come to the point where you call PDFsharp to convert the image.

I don't know where that TIFF file comes from.
Adobe created TIFF.
Adobe created PhotoShop.
PhotoShop cannot read the TIFF file you gave me.
So either it's a new-standard TIFF file or a non-standard TIFF file.

Find a suitable TIFF library that can read this file - and PDFsharp will be able to convert it ...

It looks like a scanned document. Maybe you can have it saved in a different format (e. g. TIFF with a smaller block size). TIFF has several parameters one can play with.

It looks like a JPEG compressed TIFF file. If it's JPEG, than better ask for a JPEG file. I'd use TIFF only with lossless compression.
PDF supports JPEG. If you have a JPEG file, it will only be stored inside the PDF. All other formats will be converted to the LZW format of PDF.

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