PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 30, 2024 11:18 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: TIFF to PDF Problems
PostPosted: Wed Jul 15, 2009 1:03 pm 
Offline

Joined: Wed Jul 15, 2009 1:01 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Wed Jul 15, 2009 2:16 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Wed Jul 15, 2009 2:24 pm 
Offline

Joined: Wed Jul 15, 2009 1:01 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Wed Jul 15, 2009 3:20 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 7:32 am 
Offline

Joined: Wed Jul 15, 2009 1:01 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 7:51 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 8:03 am 
Offline

Joined: Wed Jul 15, 2009 1:01 pm
Posts: 6
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 8:04 am 
Offline

Joined: Wed Jul 15, 2009 1:01 pm
Posts: 6
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


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 8:27 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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+.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 8:29 am 
Offline

Joined: Wed Jul 15, 2009 1:01 pm
Posts: 6
Thanks for your help thomas, so im wasting my time if PDFSharp cannot convert all types of tiff file?


Top
 Profile  
Reply with quote  
 Post subject: Re: TIFF to PDF Problems
PostPosted: Thu Jul 16, 2009 8:57 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 57 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