简体   繁体   中英

Converting PDF/JPEG to TIFF using Atalasoft & C#

I am having a small issue regarding converting different fie/image types to the TIFF format. We use Atalasoft, a third party software that handles our documents and imaging in regards to scans and viewing.

The problem I am facing is that I am getting an Argument exception in the TiffDocument() method. It passes in a filestream into that method. The Argument Exception states that the file is not in the TIFF format. This is understandable when I am passing in a PDF or JPEG.

I have tried numerous attempts at trying to convert these, but to no avail. Anytime I try to convert a jpeg to tiff, an issue arises because the image is an AtalaImage and not a System.Drawing.Image.

For the JPEG conversion, I hijacked this code from the comment section here .

        public static Image ConvertToJpeg(string fileName)  
        {  
            Image retVal = null;  
            using (FileStream fs = File.OpenRead(fileName))  
            {  
                retVal = ConvertToJpeg(fs);  
                fs.Close();  
            }  

            return retVal;  
        }  

        /// <summary>  
        /// Converts the specified image into a JPEG format  
        /// </summary>  
        /// <param name="imgStream">The stream of the image to convert</param>  
        /// <returns>An Image with JPEG data if successful; otherwise   null</returns>  
        public static Image ConvertToJpeg(Stream imgStream)  
        {  
            Image retVal = null;  
            Stream retStream = new MemoryStream();  

            using (Image img = Image.FromStream(imgStream, true, true))  
            {  
                img.Save(retStream, ImageFormat.Jpeg);  
                retStream.Flush();  
            }  
            retVal = Image.FromStream(retStream, true, true);  

            return retVal;  
        }  
    }  
} 

Also, Atalasoft does have a small instruction in regards to converting PDF to TIFF, but an ArgumentException is thrown in the Save method (Error Message: error in tiff codec error writing to tiff stream). The code below is the same as the code in the link:

    TiffEncoder noAppend = new TiffEncoder(TiffCompression.Default, true);
    PdfDecoder pdf = new PdfDecoder();

    for(int i=0; i< numPages; i++)
    {
      AtalaImage img = pdf.Read(inStream, i, null);
      noAppend.Save(outStream, img, null);
      img.Dispose();
      outStream.Seek(0, SeekOrigin.Begin);
    }

EDIT : The above code works and it successfully converts the PDF to TIFF.

Furthermore, I need to know what format this file is in so it can be sent to the appropriate method to be converted. I have tried using the code from this question , but to no avail.

Below is a snippet of where the magic is happening. There is a function that calls this, but that function sets the image to the WebImageViewer.Image to the variable and calls the below method:

    private void Foo(AtalaImage image)
    {
        /*I have tried converting here, before the file stream, and after the
          filestream. */
        var url = wiv.ImageUrl;
        var path = Page.MapPath(url);
        var frame = wiv.CurrentPage - 1;

        Stream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); 

        var doc = new TiffDocument(fs); //This is where the error occurs.
        var page = new TiffPage(image);

        doc.Pages[frame] = page;

        doc.Save(path + "_tmp");

        fs.Close();

        File.Delete(path);
        File.Move(path + "_tmp", path);

        wtv.OpenUrl(url);
        wtv.SelectedIndex = frame;

        wiv.OpenUrl(url, frame);
    }

Any help or thought processes would be greatly appreciated.

I found that there is a JPegDecoder in the Atalasoft software. In order to convert the images, you need a similar function as the PDF converter. The TiffDocument() takes a stream as a parameter hence why they are stream functions.

    private Stream ConvertPDFtoTiff( Stream filestream ) 
    {
         var ms = new MemoryStream();

        var noAppend = new TiffEncoder(TiffCompression.Default, true);
        var pdf = new PdfDecoder();

        for (int i = 0; i < 100; i++)
        {
            var img = pdf.Read(filestream, i, null);
            //When the image is null it will break the loop and return the stream.
            if( img == null )
            {
                break;
            }
            noAppend.Save(ms, img, null);
            img.Dispose();
            ms.Seek(0, SeekOrigin.Begin);
        }

        return ms;
    }

    private Stream ConvertJPEGtoTIFF( Stream filestream ) 
    {
        var ms = new MemoryStream();
        var jpg = new JpegDecoder();
        var saveJpg = new TiffEncoder();

        var img = jpg.Read( filestream, null );
        saveJpg.Save( ms, img, null );
        img.Dispose();
        ms.Seek( 0, SeekOrigin.Begin );

        return ms;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM