How to convert PDF to TIFF through C#?
Hi,
I am sending PDF as fax from my application.It works fine bt the only issue is, Adobe Acrobat's new instance gets opened automatically.The solutions I got is to fax TIFF files rather than PDF.
Can anyone do let meknow how to convert PDF to TIFF through C#?
Thanks,
Ravi Bhatt
Answers
Hi Ravi,
Currently I don't find a class in the .NET Framework to convert PDF to TIFF file.
However, you can use an external library PDF4NET to achieve your goal, which can handle very large files (30000+ pages in a PDF).
See this threa Convert to TIFF image for detail.
Another approach is checking this article including good samples and illustrations.
100% .NET component for rendering PDF documents
PDFRasterizer.NET is a component for rendering PDF documents and is written entirely in C#.
This article describes how to use the PDFRasterizer.NET component to:
- Convert PDF documents to raster images such as BMP, GIF, PNG, TIFF etc.
- Display PDF documents in your WinForms application (with and without an EMF)
- Programmatically print PDF documents
Hope that helps!
NOTE:
The following code is for converting TIFF files to other image files. NOT for converting PDF to TIFF. Sorry.
Code Snippetpublic static byte[] ConvertImage(byte[] fromImage, string mimeType)
{
// Read the image from the byte variable into a bitmap variable
MemoryStream fromImageStream = new MemoryStream();
fromImageStream.Write(fromImage, 0, fromImage.Length);
Image image = Image.FromStream( fromImageStream, true ) ;
Bitmap bitmap = (Bitmap) image;
// Instantiate the encoder
EncoderParameters encoderParams = new EncoderParameters();
encoderParams.Param[0] = new EncoderParameter( Encoder.Quality, 50L );
ImageCodecInfo codecInfo = GetEncoderInfo( mimeType );
MemoryStream newImage = new MemoryStream();
// Convert the image to the new format
bitmap.Save( newImage, codecInfo, encoderParams );
// Read the new image into a byte variable
byte[] data = newImage.ToArray();if (Logger.LogSwitch.LogInfo)
{
Logger.Write("ImageUtility", "EXITING ConvertImage method");
}return data;
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}Hi,
I got it solved.I was wble to send text and .jpeg successfully.Only the problwem came when i needed to send the pdf.So i convertedt it to tiff and treid to send it as a fax.Yest i sent it sucessfully and problem with the pdf got solved. No adobe instance gets open now.Big Relief to me.
I used ABCPdf's professional edition to convert pdf to tiff. And that worked for me.
Regards,
Ravi bhatt.
The following code works for converting PDF to TIFF.But for that you have to use the ABCpdf 6.0 .NET Professional
Doc theDoc = new Doc();theDoc.Read(Server.MapPath(
"../Pdfreports/Procedure_Report_" + ReportId.ToString() + ".pdf")); // set up the rendering parameterstheDoc.Rendering.ColorSpace =
"Gray";theDoc.Rendering.BitsPerChannel = 1;
theDoc.Rendering.DotsPerInchX = 200;
theDoc.Rendering.DotsPerInchY = 400;
// loop through the pages int n = theDoc.PageCount; for (int i = 1; i <= n; i++){
theDoc.PageNumber = i;
theDoc.Rect.String = theDoc.CropBox.String;
theDoc.Rendering.SaveAppend = (i != 1);
//theDoc.Rendering.SaveCompression = XRendering.Compression.G4;theDoc.SetInfo(0,
"ImageCompression", "4");theDoc.Rendering.Save(Server.MapPath(
"../Pdfreports/Procedure_Report_" + ReportId.ToString() + "_" + i.ToString() + ".tiff"));}
theDoc.Clear();
Regards,
Ravi Bhatt
All Replies
Hi Ravi,
Currently I don't find a class in the .NET Framework to convert PDF to TIFF file.
However, you can use an external library PDF4NET to achieve your goal, which can handle very large files (30000+ pages in a PDF).
See this threa Convert to TIFF image for detail.
Another approach is checking this article including good samples and illustrations.
100% .NET component for rendering PDF documents
PDFRasterizer.NET is a component for rendering PDF documents and is written entirely in C#.
This article describes how to use the PDFRasterizer.NET component to:
- Convert PDF documents to raster images such as BMP, GIF, PNG, TIFF etc.
- Display PDF documents in your WinForms application (with and without an EMF)
- Programmatically print PDF documents
Hope that helps!
NOTE:
The following code is for converting TIFF files to other image files. NOT for converting PDF to TIFF. Sorry.
Code Snippetpublic static byte[] ConvertImage(byte[] fromImage, string mimeType)
{
// Read the image from the byte variable into a bitmap variable
MemoryStream fromImageStream = new MemoryStream();
fromImageStream.Write(fromImage, 0, fromImage.Length);
Image image = Image.FromStream( fromImageStream, true ) ;
Bitmap bitmap = (Bitmap) image;
// Instantiate the encoder
EncoderParameters encoderParams = new EncoderParameters();
encoderParams.Param[0] = new EncoderParameter( Encoder.Quality, 50L );
ImageCodecInfo codecInfo = GetEncoderInfo( mimeType );
MemoryStream newImage = new MemoryStream();
// Convert the image to the new format
bitmap.Save( newImage, codecInfo, encoderParams );
// Read the new image into a byte variable
byte[] data = newImage.ToArray();if (Logger.LogSwitch.LogInfo)
{
Logger.Write("ImageUtility", "EXITING ConvertImage method");
}return data;
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}i also have the same problem, when fax pdf file\
anyone can solve, can share with me
email:
hajor#msn.com
- good
- DId you find a solution if so can you send me some info
i didn't solove it ,
now I try Ghostscript
I tried using your code but received an error on the
Image image = Image.FromStream.....
"Parameter is not valid"
Only happens when I open a PDF. I tried with .tif and it worked.
Any ideas
I am not sure. It may has something to do with how you read the PDF into the byte[]
Here is how I did it.
Code Snippetbyte[] data = null;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
// Read the image into the byte variable
data = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();Then I passed the "data" to the method in my previous post. It worked fine for me.
--------------------------------------------------------------------------------------------------------------------------------
www.dotnetideas.com
- Try this free online service to convert PDF files to TIFF or up to 117 other file formats. www.freepdfconvert.com To incorporate this service directly into your website or application, refer to http://www.freepdfconvert.com/service_integration.asp . This service is free with limitations and for a nomonal membership fee you can unlock the limitations.
i also happen the same problem
Image image = Image.FromStream.....
"Parameter is not valid"
I seem to be getting the invalid parameter error as well. Is there other objects that need referencing?
Thanks
Hi,
I got it solved.I was wble to send text and .jpeg successfully.Only the problwem came when i needed to send the pdf.So i convertedt it to tiff and treid to send it as a fax.Yest i sent it sucessfully and problem with the pdf got solved. No adobe instance gets open now.Big Relief to me.
I used ABCPdf's professional edition to convert pdf to tiff. And that worked for me.
Regards,
Ravi bhatt.
I'm getting this "Parameter is not valid" exception, as well.
It's not only PDF, using RTF files causes the same exception.
Actually, I cannot imagine that it's possible to instanciate an Image with the data of an arbitray file without any previous conversion.
But I'm still interested in any solution of this problem.
Hi, great work
can you say details?
thanks
- Hi ,
As I have the same problem? If you got solution for your problem,please let me know friend.... The following code works for converting PDF to TIFF.But for that you have to use the ABCpdf 6.0 .NET Professional
Doc theDoc = new Doc();theDoc.Read(Server.MapPath(
"../Pdfreports/Procedure_Report_" + ReportId.ToString() + ".pdf")); // set up the rendering parameterstheDoc.Rendering.ColorSpace =
"Gray";theDoc.Rendering.BitsPerChannel = 1;
theDoc.Rendering.DotsPerInchX = 200;
theDoc.Rendering.DotsPerInchY = 400;
// loop through the pages int n = theDoc.PageCount; for (int i = 1; i <= n; i++){
theDoc.PageNumber = i;
theDoc.Rect.String = theDoc.CropBox.String;
theDoc.Rendering.SaveAppend = (i != 1);
//theDoc.Rendering.SaveCompression = XRendering.Compression.G4;theDoc.SetInfo(0,
"ImageCompression", "4");theDoc.Rendering.Save(Server.MapPath(
"../Pdfreports/Procedure_Report_" + ReportId.ToString() + "_" + i.ToString() + ".tiff"));}
theDoc.Clear();
Regards,
Ravi Bhatt
- What do I 'use' to get the Doc() library?
I don't see how this code can work. We are all getting an error of "parameter is not valid"
after this line: Image image = Image.FromStream( fromImageStream, true ) ;
Since the image coming in is from a PDF file how does it know how to format it correctly to the image object?
Are you leaving something out?
Frankly, I don't think it is even possible to convert a PDF to a TIFF without purchasing third party software of some sort but I would love to be corrected.
Davicito
Washington, DC
The PDF we are using is generated using ComponentOne. It may be much than regular PDF. That's probably why the code worked for me. Like I said, we were able to convert the PDF we generated to GIF format using above code. Then we can fax the GIF. I haven't tried the code with just any PDF file.
I also got the invalid parameter on the FromStream method. There must be something that he left out in the code example for everyone to get this same error.
I just tested this code with a bitmap file instead of PDF , and works fine, if i try with PDF files, I got the same "Parameter not Valid" error, perhaps image class do not know to handle PDF data.
Does anybody know how to use tihis sample with PDF ?
Regards,
I appologize for misleading everyone. The code I posted here was used to convert TIFF to other format of images. I made mistake by thinking it was used for converting PDF to TIFF. Sorry about that.
Hello All,
After a week of reading through Adobe SDK documentation and looking through a lot of forums, I have successfully written a test application to convert PDF to .TIFF. Note: you must have the Adobe standard or Professional 7.0 or greater installed to make this work. I did it using the trial version of Adobe Pro 8.0. You will have to set a reference to the Acrobat COM object. Here is my coding in VB.NET:
Code BlockPublic
Class Form1 Dim PDFApp As Acrobat.AcroApp Dim PDDoc As Acrobat.CAcroPDDoc Dim AVDoc As Acrobat.CAcroAVDocPrivate Sub btnBrowsePath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowsePath.Click
Dim result As DialogResult
OpenFileDialog1.FileName = ""
OpenFileDialog1.AddExtension =
TrueOpenFileDialog1.DefaultExt =
".TIF"OpenFileDialog1.Filter =
"Adobe PDF Files (*.PDF) | *.PDF"OpenFileDialog1.InitialDirectory =
"DeskTop"result = OpenFileDialog1.ShowDialog()
If (result = Windows.Forms.DialogResult.OK) Then
Me.txtPDFPath.Text = OpenFileDialog1.FileName End IfEnd Sub
Private Sub btnLoadPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadPDF.Click
' Dim javaString As String
Dim JSObj As Object, strPDFText As String' Create Acrobat Application object
PDFApp = CreateObject(
"AcroExch.App") ' Create Acrobat Document objectPDDoc = CreateObject(
"AcroExch.PDDoc") ' Open PDF filePDDoc.Open(
Me.txtPDFPath.Text) ' Create AV doc from PDDoc objectAVDoc = PDDoc.OpenAVDoc(
"TempPDF") ' Hide Acrobat application so everything is done in silent modePDFApp.Hide()
' Create Javascript bridge objectJSObj = PDDoc.GetJSObject()
' Test making change to one of the text fields ' Syntax for GetField method GetField( strFieldName )JSObj.GetField(
"Text2.0.0.0.0").value = "JESSICA" ' Test pulling data from a PDF text field and showing in a Windows MessageboxstrPDFText = JSObj.GetField(
"Text2.0.0.2.0").value ' Attempt to save PDF to TIF image file. ' SaveAs method syntax .SaveAs( strFilePath, cConvID ) ' For TIFF output the correct cConvid is "com.adobe.acrobat.tiff" ' cCovid MUST BE ALL LOWERCASE.JSObj.SaveAs(
"C:\TestTiffs\TestGood.tif", "com.adobe.acrobat.tiff")PDDoc.Close()
PDFApp.CloseAllDocs()
' Clean up
System.Runtime.InteropServices.Marshal.ReleaseComObject(JSObj)
JSObj =
NothingSystem.Runtime.InteropServices.Marshal.ReleaseComObject(PDFApp)
PDFApp =
NothingSystem.Runtime.InteropServices.Marshal.ReleaseComObject(PDDoc)
PDDoc =
NothingSystem.Runtime.InteropServices.Marshal.ReleaseComObject(AVDoc)
AVDoc =
NothingEnd Sub
End Class
Hai
Great! I too have a requirement the same way. But my pdf does not have any js or objects included in pdf. Its a simple clean pdf contains only text.
Hence, I get nothing at
' Create Javascript bridge object
JSObj = PDDoc.GetJSObject()
' Test making change to one of the text fields
' Syntax for GetField method GetField( strFieldName )JSObj.GetField(
"Text2.0.0.0.0").value = "JESSICA"and the code does not run after the above line.
Can you please help me out. How to save a pdf as tiff, which does not produce any value by executing JSObj = PDDoc.GetJSObject().
- Do you have the standard or Professional Acrobat installed not just the reader? Did you set a reference to Acrobat?
Let me thank you first for your interest and quick response!
Yes. I have installed Acrobat 8.0.1 Professional trial version.
I have set references.
Your code runs fine for me. But when the execution encounters the statement JSObj = PDDoc.GetJSObject(), GetJSObject returns nothing, and rest of the code gets messed up. I guess with JSObj you are reading and assigning values to some controls available in the pdf. But my PDF does not contain any. Its just a simple PDF.
Yes, the GetField("Text2.0.0.0.0").value = "JESSICA" references a text field already in my pdf file. You would need to add some text fields in your pdf and changed the getfield parameter to reference the name of your fields. If you take out all the code after the JSOBJ = PDDOC.GetJSObj() and immediately put the PDDoc.SaveAs it should work. The path of the SaveAs cannot be a root or system folder.
- Thanks!
But PDDoc does not have SaveAs method. By any chance if you come to know how to do this please post. Sorry, I mistakenly said PDDoc.SaveAS when I mean JSObj.SaveAs
This code should work and I have seen a similar code sample on another site by someone else, so I am sure it works.
Code BlockPublic Class Form1Dim PDFApp As Acrobat.AcroApp
Dim PDDoc As Acrobat.CAcroPDDoc
Dim AVDoc As Acrobat.CAcroAVDoc
Private Sub btnLoadPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadPDF.Click
' Dim javaString As String
Dim JSObj As Object, strPDFText As String' Create Acrobat Application object
PDFApp = CreateObject(
"AcroExch.App") ' Create Acrobat Document objectPDDoc = CreateObject(
"AcroExch.PDDoc") ' Open PDF filePDDoc.Open("C:\Test.pdf"
) ' Path of some PDF you want to open' Hide Acrobat application so everything is done in silent mode
PDFApp.Hide()
' Create Javascript bridge object
JSObj = PDDoc.GetJSObject()
' Create Tiff file
JSObj.SaveAs("C:\TestTiffs\TestGood.tif", "com.adobe.acrobat.tiff")
PDDoc.Close()
PDFApp.CloseAllDocs()
' Clean up
System.Runtime.InteropServices.Marshal.ReleaseComObject(JSObj)
JSObj =
NothingSystem.Runtime.InteropServices.Marshal.ReleaseComObject(PDFApp)
PDFApp =
NothingSystem.Runtime.InteropServices.Marshal.ReleaseComObject(PDDoc)
PDDoc =
NothingEnd Sub
End Class
Thanks you!
You have done a great help!
Thank you once again!
- Proposed As Answer byselvaCTS Tuesday, November 18, 2008 12:02 PM
- Hi i am developer PdfCreator from gotdotnet.
Tiff to PDF with CCITT compression is not diffucult. In the market very tool successfully make to it. Such as PDF Technologies for .NET
This product very easy
PDFDocument MyPDF = new PDFDocument("Tiff2Pdf.pdf"); - Many thanks to New2bi.
Is there a way I can save it as Multipage Tif images?
Thanks
=BlackSpider= - Hi,
I tried your code it works fine. If i have "test.PDF" file which contains 20 pages. i want to convert the test.PDF in to single "test.tif"
If i tried same file with ur approach it converted each page in single tiff.
Please help its bit urgent
Thanks in Advance
Selvaraj P - hi,
Thanks for the code .This is what am looking for conversion of pdf to tif.
But i got the error
"Cannot create ActiveX component." on line
PDFApp = CreateObject(
"AcroExch.App")
AM using the Adobe reader 7.0.
Please guys clarify me.
Thanks
vijay - hi,
Now i had upgraded Acrobat reader to 8.0 and now workign fine. But when i try to transfer multi page pdf file to a single tiff file, it actually creates each page as a seperate tiff file.But i want all the pages as a whole sinlge tiff fiel only...Are we missing any property?
Regards
vijay

