C#实现PDF转SWF的操作

摘要:
System.IO.File.Exists|| System.IO.File.Exists){returnfalse;}StringBuildersb=newStringBuilder();附加;附加;附加;ifendpage=获取页面计数;附加;附加;stringCommand=sb.ToString();System.Diagnostics.Processp=newSystem.Diagnostics.Pprocess();p、 StartInfo.FileName=exe;p、 StartInfo.Arguments=命令;p、 StartInfo.WorkingDirectory=HttpContext.Current.Server.MapPath;p、 StartInfo.UseShellExecute=false;p、 StartInfo.RirectStandardError=true;p、 StartInfo.CreateNoWindow=false;p、 开始();p、 BeginErrorReadLine();p、 WaitForExit();p、 Close();p、 Dispose();returntrue;}//////返回的页数///////PDF文件地址privatestatisticGetPageCount{byte[]缓冲区=System.IO.File.ReadAllBytes;intlength=缓冲区长度;ifreturn-1;ifreturn-1;stringpdfText=编码默认.GetString;System.Text.RegularExpressions.Regexrx1=newSystem.Text.RregularExpressions.Regex;System.Text.RegularExpressions.MatchCollectionmatches=rx1.Matches;returnmatches.Count;}}2.通过代码将PDF转换为SWF,最常见的方法是在SWFTools中使用pdf2swf。这个工具相对容易使用。转换的SWF文件的质量也很好/**代码publicclassConverter{publicstaticintconPDF2SWthrowsIOException{//如果目标路径不存在,请创建目标路径Filedest=newFile;If(!Source.exists())return0;//调用pdf2swf命令

http://www.cpbcw.com/codetree1365/PSD2swfHelper.cs.html 

http://tangshuo.iteye.com/blog/538361

1.

using System.Web;
using System.Text;

public static class PSD2swfHelper
{

    /// <summary>

    /// 转换所有的页,图片质量80%

    /// </summary>

    /// <param name="pdfPath">PDF文件地址</param>

    /// <param name="swfPath">生成后的SWF文件地址</param>

    public static bool PDF2SWF(string pdfPath, string swfPath)
    {

        return PDF2SWF(pdfPath, swfPath, 1, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)), 80);

    }



    /// <summary>

    /// 转换前N页,图片质量80%

    /// </summary>

    /// <param name="pdfPath">PDF文件地址</param>

    /// <param name="swfPath">生成后的SWF文件地址</param>

    /// <param name="page">页数</param>

    public static bool PDF2SWF(string pdfPath, string swfPath, int page)
    {

        return PDF2SWF(pdfPath, swfPath, 1, page, 80);

    }



    /// <summary>

    /// PDF格式转为SWF

    /// </summary>

    /// <param name="pdfPath">PDF文件地址</param>

    /// <param name="swfPath">生成后的SWF文件地址</param>

    /// <param name="beginpage">转换开始页</param>

    /// <param name="endpage">转换结束页</param>

    private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
    {

        string exe = HttpContext.Current.Server.MapPath("~/Bin/tools/pdf2swf-0.9.1.exe");

        pdfPath = HttpContext.Current.Server.MapPath(pdfPath);

        swfPath = HttpContext.Current.Server.MapPath(swfPath);

        if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
        {

            return false;

        }

        StringBuilder sb = new StringBuilder();

        sb.Append(" \"" + pdfPath + "\"");

        sb.Append(" -o \"" + swfPath + "\"");

        sb.Append(" -s flashversion=9");

        if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);

        sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");

        sb.Append(" -j " + photoQuality);

        string Command = sb.ToString();

        System.Diagnostics.Process p = new System.Diagnostics.Process();

        p.StartInfo.FileName = exe;

        p.StartInfo.Arguments = Command;

        p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");

        p.StartInfo.UseShellExecute = false;

        p.StartInfo.RedirectStandardError = true;

        p.StartInfo.CreateNoWindow = false;

        p.Start();

        p.BeginErrorReadLine();

        p.WaitForExit();

        p.Close();

        p.Dispose();

        return true;

    }



    /// <summary>

    /// 返回页数

    /// </summary>

    /// <param name="pdfPath">PDF文件地址</param>

    private static int GetPageCount(string pdfPath)
    {

        byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);

        int length = buffer.Length;

        if (buffer == null)

            return -1;

        if (buffer.Length <= 0)

            return -1;

        string pdfText = Encoding.Default.GetString(buffer);

        System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");

        System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);

        return matches.Count;

    }

}

2.

通过代码将PDF转换成SWF来说,现在比较常用的一种方式就是利用SWFTools工具中的pdf2swf(http://www.swftools.org/)。这个工具还是比较好用的。转换成的SWF文件质量也不错。

/**

代码  
public class Converter {  
    public static int convertPDF2SWF(String sourcePath, String destPath, String fileName) throws IOException {  
        //目标路径不存在则建立目标路径  
        File dest = new File(destPath);  
        if (!dest.exists()) dest.mkdirs();  
          
        //源文件不存在则返回  
        File source = new File(sourcePath);  
        if (!source.exists()) return 0;  
          
        //调用pdf2swf命令进行转换  
        String command = "D:\\Program Files\\SWFTools\\pdf2swf.exe" + " -o \"" + destPath + "\\" + fileName + "\"  <span style="color: #ff0000;">-s languagedir=D:\\xpdf\\xpdf-chinese-simplified</span> -s flashversion=9 \"" + sourcePath + "\"";  
          
        Process pro = Runtime.getRuntime().exec(command);  
          
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(pro.getInputStream()));  
        while (bufferedReader.readLine() != null);   
          
        try {  
            pro.waitFor();  
        } catch (InterruptedException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
        return pro.exitValue();  
          
    }  
      
    public static void main(String []args) throws IOException {  
        String sourcePath = "c:\\test.pdf";  
        String destPath = "c:\\";  
        String fileName = "test.swf";  
        Converter.convertPDF2SWF(sourcePath, destPath, fileName);  
    }  
}  

   就这么简单的几行代码就可以了。但是在程序中遇到中文就会出现意想不到的情况,这个也不例外。在转换中,我发现有些中文PDF文件转换后会出现乱码的现象,因此这里还要处理一下乱码的问题。看到上面代码中红色的一段了吗?这就是解决乱码的方法。这个方法是参考了http://hi.baidu.com/xwx520/blog/item/1d0c423885b392fab311c72e.html这篇文章,感谢作者。

1.下载XPDF:ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz,并解压到xpdf-chinese-simplified目录下。

2.下载字体:http://blog.pjoke.com/wp-content/uploads/2009/02/font.zip,并解压到xpdf-chinese-simplified/CMap目录下。

3.修改xpdf-chinese-simplified目录下的add-to-xpdfrc文件。将里面的路径设为自己的路径:

C#实现PDF转SWF的操作第1张

4.参照上面的代码,在调用pdf2swf命令中加入“ -s languagedir=D:\\xpdf\\xpdf-chinese-simplified ”参数。

这样乱码的问题就解决了。

免责声明:文章转载自《C#实现PDF转SWF的操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇一文入门富文本编辑器圈复杂度下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

IText简介及示例

一、iText简介    iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。          使用iText非常方便,引入jar包,程序中就可以使用iText类库了。iText.jar包下载地址:http:...

Linux下分割、合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具

 Linux下分割、合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具 Linux下分割、合并PDF(pdftk) pdftk http://www.pdflabs.com/docs/install-pdftk/ 合并 pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf pd...

js实现html转pdf+html2canvas.js截图不全的问题

最近做项目中遇到要把整个页面保存为PDF文件,网上找了一下实现的方法都是 html2canvas.js+jsPdf.js 来实现。实现的过程是 先用html2canvas.js把html页面转成图片,再用jsPdf.js把图片导出为pdf。 于是做了个小案例来测试这个功能。 <body> <!-- PDF -->...

Python读取PDF文档

1 from pdfminer.converter import PDFPageAggregator 2 from pdfminer.layout import LAParams 3 from pdfminer.pdfparser import PDFParser 4 from pdfminer.pdfparser import PDFDocume...

C# Parsing 类实现的 PDF 文件分析器

下载示例 下载源代码 1. 介绍 这个项目让你可以去读取并解析一个PDF文件,并将其内部结构展示出来. PDF文件的格式标准文档可以从Adobe那儿获取到. 这个项目基于“PDF指南,第六版,Adobe便携文档格式1.7 2006年11月”. 它是一个恐怕有1310页的大部头. 本文提供了对这份文档的简洁概述. 与此相关的项目定义了用来读取和解析...

从PDF中提取信息----PDFMiner

今天由于某种原因需要将pdf中的文本提取出来,就去搜了下资料,发现PDFMiner是针对 内容提取的,虽然最后发现pdf里面的文本全都是图片,就没整成功,不过试了个文本可复制的 那种pdf文件,发现还是蛮好用的。 PDFMiner----python的PDF解析器和分析器 1.官方文档:http://www.unixuser.org/~euske/pyth...