Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Aug 11, 2014 1:09 am

I have a document which is a TIFF (just some text).
I am trying to convert the TIFF to PDF.

The basic function
Public Function SpireTifToPDF(ByVal imgByte() As Byte) As Byte()

Dim doc As New PdfDocument()
Dim section As PdfSection = doc.Sections.Add()
Dim page As PdfPageBase = doc.Pages.Add()

Dim ms As New System.IO.MemoryStream(imgByte)
Dim image As Spire.Pdf.Graphics.PdfImage = Spire.Pdf.Graphics.PdfImage.FromStream(ms)
Dim widthFitRate As Single = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width
Dim heightFitRate As Single = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height
Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
Dim fitWidth As Single = image.PhysicalDimension.Width / fitRate
Dim fitHeight As Single = image.PhysicalDimension.Height / fitRate
page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight)

ms = New System.IO.MemoryStream
doc.SaveToStream(ms)
doc.Clone()
Return ms.GetBuffer
End Function

Attached is the PDF I Get from the function if I run System.IO.File.WriteAllBytes("p:\test.pdf", pdfByteArray)
Adobe says the file is corrupt and will not open.
PDF Exchange does open the file, PDF Exchange does tend to be more forgiving than Adobe.

How can I change my function to improve the file so it does not appear corrupt to adobe?

Thank You,
Daniel

drpotts
 
Posts: 7
Joined: Thu Mar 07, 2013 3:23 am

Mon Aug 11, 2014 7:07 am

Dear Daniel,

Thanks for your inquiry.

Please try the below solution.
Code: Select all
PdfDocument doc = new PdfDocument();
Image image = Image.FromFile("..\\..\\2.tiff");
PdfImage pdfimage = PdfImage.FromImage(image);
PdfUnitConvertor uinit = new PdfUnitConvertor();
//convert pixel size to point size
SizeF pageSize = uinit.ConvertFromPixels(image.Size, PdfGraphicsUnit.Point);
//create a page based on size of image
PdfPageBase page = doc.Pages.Add(pageSize, new PdfMargins(0f));
//Draw image into page
page.Canvas.DrawImage(pdfimage, new PointF(0, 0));
doc.SaveToFile("sample.pdf");

If the issue still persists, please provide us your tiff file to help us do an investigation and then we will provide solution to resolve it.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Mon Aug 11, 2014 2:03 pm

Thank you for the prompt reply.

That got me headed in the right direction because it worked when I did save to file.

As I tested I found out the problem was with my code and had nothing to do with Spire.PDF

I was returning a byte array from the memory stream with ms.GetBuffer(), I needed to use ms.ToArray().
Although there are some caveats as explained.
http://stackoverflow.com/questions/1305 ... ver-useful

Anyway it is working great now.

Thank You,
Daniel

drpotts
 
Posts: 7
Joined: Thu Mar 07, 2013 3:23 am

Tue Aug 12, 2014 1:50 am

Dear Daniel,

Thank you for sharing.

Please feel free to contact us if you have any problems.

Best regards,
Burning
E-iceblue support team
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Return to Spire.PDF