簡體   English   中英

使用Atalasoft和C#將PDF / JPEG轉換為TIFF

[英]Converting PDF/JPEG to TIFF using Atalasoft & C#

關於將不同的fie /圖像類型轉換為TIFF格式,我有一個小問題。 我們使用Atalasoft,這是第三方軟件,用於處理有關掃描和查看的文檔和圖像。

我面臨的問題是,我在TiffDocument()方法中收到了Argument異常。 它將文件流傳遞給該方法。 參數異常指出該文件不是TIFF格式。 當我傳遞PDF或JPEG時,這是可以理解的。

我嘗試了無數次嘗試將其轉換的嘗試,但均無濟於事。 每當我嘗試將jpeg轉換為tiff時,都會出現問題,因為該圖像是AtalaImage而不是System.Drawing.Image。

對於JPEG轉換,我從此處的注釋部分中劫持了此代碼。

        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;  
        }  
    }  
} 

此外, Atalasoft確實有一條關於將PDF轉換為TIFF的小指令,但是Save方法中引發了ArgumentException(錯誤消息:tiff編解碼器寫入tiff流時出錯)。 下面的代碼與鏈接中的代碼相同:

    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);
    }

編輯 :上面的代碼工作,並且它成功地將PDF轉換為TIFF。

此外,我需要知道此文件的格式,以便可以將其發送到適當的方法進行轉換。 我嘗試使用此問題中的代碼,但無濟於事。

以下是魔術發生位置的摘要。 有一個函數調用此函數,但是該函數將圖像設置為WebImageViewer.Image設置為變量,並調用以下方法:

    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);
    }

任何幫助或思考過程將不勝感激。

我發現Atalasoft軟件中有一個JPegDecoder。 為了轉換圖像,您需要與PDF轉換器類似的功能。 TiffDocument()將流作為參數,因此為什么它們是流函數。

    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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM