zoukankan      html  css  js  c++  java
  • PDF To TIFF

    找到的几种PDF To Tiff方法:

    一:使用Ghostscript,开源项目,注意Win32和Win64版本。

    免费,生成的大小适度的图片清晰度一般。当然可以设置生成很大的清晰文件。

    下载地址:http://sourceforge.net/projects/ghostscript/files/

    下载后解压,只需要Bin中的文件:

    gswin32c.exe

    gsdll32.dll

    gswin32.exe

    gsdll32.lib

    通过命令行调用:

    gswin32c.exe -dSAFER -dBATCH -dNOPAUSE -r100 -dNOTRANSPARENCY -sDEVICE=tiff12nc -sCompression=lzw -dAdjustWidth=0 -dMaxStripSize=999999999  -sOutputFile=D:\Work\Test\PDF\abc.tif  D:\Work\Test\PDF\abc.PDF

    另外,gswin32c.exe还可以将PDF转为其他格式的图片文件。

    如JPG:gswin32c.exe -dSAFER -dBATCH -dNOPAUSE -r100 -sDEVICE=jpeg -dGraphicsAlphaBits=4 -sOutputFile=D:\Work\Test\PDF\abc%d.jpg  D:\Work\Test\PDF\abc.PDF

    参数设置参见压缩包中的doc\Use.htm

    二、oakdoc的PDF to TIFF Converter,收费软件。分GUI、Command、COMPONENT版本。

    下载地址:http://www.oakdoc.com/products/pdftotiff.html

    GUI网上有破解版。试用版生成的图片上有水印。

    收费,价格低,生成的文件小且清晰。支持PDF目录 -> Tif目录的批量操作。

    Command版只需要pdftotiff.exe

    通过命令行调用:

    pdftotiff.exe  -i "D:\Work\Test\PDF" -o "D:\Work\Test\Tif" -x 96 -y 96 -q 75 -c 4 -e \"LZW\" -a

    参数设置参见压缩包中的help\index.htm

    三、Adobe的Professional版,收费。有破解版。
    首先,使用Acrobat.CAcroPDDoc将PDF页面CopyToClipboard
    然后,从Clipboard.GetData(DataFormats.Bitmap)读取出Image
    最后,将Image合并到一个Tif文件中。

    PDF To Image代码

    private static List<Image> PDFToImage(string InputFilename)
    {
        List
    <Image> imgs = new List<Image>();

        Acrobat.CAcroPDPage pdfPage 
    = null;
        Acrobat.CAcroPoint pdfPoint 
    = null;
        Acrobat.CAcroRect pdfRect 
    = null;
        Acrobat.CAcroPDDoc pdfDoc 
    = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc""");
        
    //pdfDoc = new Acrobat.AcroPDDocClass();

        
    //Type AcroApp = Type.GetTypeFromCLSID(new System.Guid("{85DE1C45-2C66-101B-B02E-04021C009402}"));
        
    //Type AcrobatType = Type.GetTypeFromCLSID(new System.Guid("{72498821-3203-101B-B02E-04021C009402}"));
        
    //Type AcroPD = Type.GetTypeFromCLSID(new System.Guid("{FF76CB60-2EC8-101B-B02E-04021C009402}"));
        
    //Acrobat.CAcroApp oApp = (Acrobat.CAcroApp)Activator.CreateInstance(AcroApp);
        
    //Acrobat.CAcroAVDoc avdoc = (Acrobat.CAcroAVDoc)Activator.CreateInstance(AcrobatType);
        
    //Acrobat.CAcroPDDoc pdfDoc = (Acrobat.CAcroPDDoc)Activator.CreateInstance(AcroPD);

        
    bool llRet = pdfDoc.Open(InputFilename);

        
    if (!llRet)
        {
            
    throw new FileNotFoundException();
        }
        
    int pageCount = pdfDoc.GetNumPages();
        
    for (int i = 0; i < pageCount; i++)
        {
            pdfPage 
    = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);
            pdfPoint 
    = (Acrobat.CAcroPoint)pdfPage.GetSize();
            pdfRect 
    = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect""");
            pdfRect.Left 
    = 0;
            pdfRect.right 
    = pdfPoint.x;
            pdfRect.Top 
    = 0;
            pdfRect.bottom 
    = pdfPoint.y;

            pdfPage.CopyToClipboard(pdfRect, 
    00100);

            IDataObject loClipboardData 
    = Clipboard.GetDataObject();
            
    if (loClipboardData.GetDataPresent(DataFormats.Bitmap))
            {
                
    //Get Image
                Bitmap pdfBitmap = (Bitmap)loClipboardData.GetData(DataFormats.Bitmap);
                imgs.Add(pdfBitmap);
            }
        }
        pdfDoc.Close();

        Marshal.ReleaseComObject(pdfPage);
        Marshal.ReleaseComObject(pdfRect);
        Marshal.ReleaseComObject(pdfDoc);

        
    return imgs;
    }

      

    Images To Tif代码
    /// <summary>
    /// Combine some file into 1 tif multipage file
    /// if black and write image, we use CCITT4 compression
    /// else we use the LZW compression
    /// </summary>
    /// <param name="InputFilenames">the files to be combined, canbe (bmp, jpg, gif, png, tif)</param>
    /// <param name="OutputFilename">the output filename</param>
    public static void CombineTif(string[] InputFilenames, string OutputFilename)
    {
        
    //get ImageCodecInfo, generate tif format
        ImageCodecInfo info = null;
        
    foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
        {
            
    if (ice.MimeType == "image/tiff")
            {
                info 
    = ice;
                
    break;
            }
        }

        
    /*
        * define the encoderparameter, 
        * when the 1st page, will be EncoderValue.MultiFrame.
        * when the other pages, will be EncoderValue.FrameDimensionPage.
        * when all pages saved, will be the EncoderValue.Flush.
        
    */
        EncoderParameters ep 
    = new EncoderParameters(2);

        
    /*
        * when the 1st file, 1st frame, will be true.
        * from the 1st file, 2nd frame, will be false.
        
    */
        
    bool b11 = true;

        Image img 
    = null;

        
    //create a image instance from the 1st image
        for (int nLoopfile = 0; nLoopfile < InputFilenames.Length; nLoopfile++)
        {
            
    //get image from src file
            Image img_src = Image.FromFile(InputFilenames[nLoopfile]);

            Guid guid 
    = img_src.FrameDimensionsList[0];
            System.Drawing.Imaging.FrameDimension dimension 
    = new System.Drawing.Imaging.FrameDimension(guid);

            
    //get the frames from src file
            for (int nLoopFrame = 0; nLoopFrame < img_src.GetFrameCount(dimension); nLoopFrame++)
            {
                img_src.SelectActiveFrame(dimension, nLoopFrame);

                
    /*
                * if black and write image, we use CCITT4 compression
                * else we use the LZW compression
                
    */
                
    if (img_src.PixelFormat == PixelFormat.Format1bppIndexed)
                {
                    ep.Param[
    0= new EncoderParameter(Encoder.Compression, Convert.ToInt32(EncoderValue.CompressionCCITT4));
                }
                
    else
                {
                    ep.Param[
    0= new EncoderParameter(Encoder.Compression, Convert.ToInt32(EncoderValue.CompressionLZW));
                }

                
    if (b11)
                {
                    
    //1st file, 1st frame, create the master image
                    img = img_src;

                    ep.Param[
    1= new EncoderParameter(Encoder.SaveFlag, Convert.ToInt32(EncoderValue.MultiFrame));
                    img.Save(OutputFilename, info, ep);

                    b11 
    = false;
                    
    continue;
                }

                ep.Param[
    1= new EncoderParameter(Encoder.SaveFlag, Convert.ToInt32(EncoderValue.FrameDimensionPage));
                img.SaveAdd(img_src, ep);
            }
        }
        ep.Param[
    1= new EncoderParameter(Encoder.SaveFlag, Convert.ToInt32(EncoderValue.Flush));
        img.SaveAdd(ep);
    }

      


    其他方法:
    XpdfRasterizerNet可以将PDF输出为BMP文件,带水印,需要regsvr32注册XpdfRasterizer.dll,没有进一步使用。
    含以下DLL:XpdfRasterizer.dll、XpdfRasterizerNet.dll、O2S.Components.PDFRender4NET.dll、O2S.Components.PDFView4NET.dll

    代码
        XpdfRasterizerNet.XpdfRasterizerClass rast = new XpdfRasterizerNet.XpdfRasterizerClass();
        rast.loadFile(pdffile);
        
    for (int page = 1; page <= rast.numPages; page++)
        {
            
    string jpgfile = fileName + page.ToString() + ".bmp";
            rast.writePageBitmap(page, dpi, rast.imageRGB, rast.imageFileBMP, jpgfile);
        }
        rast.closeFile();

     Java中的PDFBox有J#版,不过没有最终完成,没有实现page.convertToImage方法。

    代码
        PDDocument doc = PDDocument.load(@"D:\Work\Test\PDF\abc.pdf");
        List pages 
    = doc.getDocumentCatalog().getAllPages();
        Iterator iter 
    = pages.iterator();
        
    while (iter.hasNext())
        {
            PDPage page 
    = (PDPage)iter.next();
            BufferedImage image 
    = page.convertToImage();//error
            File file = new java.io.File(@"D:\Work\Test\PDF\abc.tif");
            ImageIO.write(image, 
    "tiff", file);
            
    //OutputStream os = new FileOutputStream(file);
            
    //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
            
    //encoder.encode(image);
        }


     

  • 相关阅读:
    JDK7集合框架源码阅读(四) LinkedHashMap
    JDK7集合框架源码阅读(三) HashMap
    JDK7集合框架源码阅读(二) LinkedList
    在django中解决跨域AJAX
    Python基础之文件操作
    Python基础之深浅copy
    Python基础之集合set
    Python基础之range()
    Python基础之enumerate枚举
    Python基础之for循环
  • 原文地址:https://www.cnblogs.com/xujiaoxiang/p/1732701.html
Copyright © 2011-2022 走看看