c#将pdf文件转换成flash swf

101 篇文章 0 订阅
6 篇文章 0 订阅

来自:http://outofmemory.cn/code-snippet/1930/c-jiang-pdf-file-zhuanhuancheng-flash-swf

主要是为了在线浏览pdf文件

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("FlexPaper/pdf2swf.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");
            sb.Append(" -s languagedir=\"E:\\xpdf\\xpdf-chinese-simplified\"");
            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;
        }
    }


 

C#中将OFD(Open Fixed Document)文件转换成PDF文件的功能通常需要借助第三方库来实现,因为.NET框架原生并不支持OFD格式。下面是一个使用第三方库进行转换的简单示例: 首先,你需要找到一个支持OFD到PDF转换的第三方库,比如可以使用一些开源库或者购买商业许可证的库,例如"DotNetty"中的"DotNetty.Codecs.FusionPDF"。 以下是一个使用假定第三方库的示例代码: ```csharp using System; using第三方库命名空间; // 替换为实际使用的第三方库的命名空间 public class OFDToPDFConverter { public void ConvertOFDToPDF(string inputFilePath, string outputFilePath) { try { // 加载OFD文件 OFDDocument ofdDoc = LoadOFDDocument(inputFilePath); // 将OFD文档转换为PDF文档对象 PDFDocument pdfDoc = ConvertToPDF(ofdDoc); // 保存PDF文档到指定路径 SavePDFDocument(pdfDoc, outputFilePath); } catch (Exception ex) { // 处理可能发生的异常 Console.WriteLine("转换过程中发生错误: " + ex.Message); } } private OFDDocument LoadOFDDocument(string filePath) { // 实现加载OFD文档的逻辑 return new OFDDocument(); } private PDFDocument ConvertToPDF(OFDDocument ofdDoc) { // 实现将OFD文档转换为PDF文档的逻辑 return new PDFDocument(); } private void SavePDFDocument(PDFDocument pdfDoc, string filePath) { // 实现保存PDF文档到文件的逻辑 } } // 使用示例 public static void Main() { OFDToPDFConverter converter = new OFDToPDFConverter(); string inputPath = "C:\\path\\to\\your\\file.ofd"; string outputPath = "C:\\path\\to\\your\\file.pdf"; converter.ConvertOFDToPDF(inputPath, outputPath); } ``` 请注意,以上代码是一个示例框架,实际实现中的方法(`LoadOFDDocument`、`ConvertToPDF`和`SavePDFDocument`)需要根据你所使用的第三方库来完具体逻辑。 在选择第三方库时,请确保遵守其许可协议,并检查是否有适用于C#的.NET版本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值