.net-base64转ofd文件转pdf转图片后压缩打包

  • .net-base64转ofd文件转pdf转图片后压缩打包已关闭评论
  • 42 次浏览
  • A+
所属分类:.NET技术
摘要

最近有需求从第三方获取到ofd文件后,需要转pdf,
1.目前看的有一个免费的插件,需要安装程序包 FreeSpire.PDF

最近有需求从第三方获取到ofd文件后,需要转pdf,
1.目前看的有一个免费的插件,需要安装程序包 FreeSpire.PDF

.net-base64转ofd文件转pdf转图片后压缩打包

 安装后,直接引用

// odf文件地址 string path = @"D:OFD20240725吴天.ofd";
OfdConverter converter = new OfdConverter(path);
//pathPdf pdf 文件地址 string pathPdf = "D:OFD20240725吴天.pdf";
converter.ToPdf(pathPdf);

这就转成功了,失败直接会抛出异常

2. pdf在转图片

需要引用dll  O2S.Components.PDFRender4NET ,dll下载地址: https://note.youdao.com/s/OtHpAcdi

 

/// <summary>         /// 将PDF文档转换为图片的方法         /// </summary>         /// <param name="pdfInputPath">PDF文件路径</param>         /// <param name="imageOutputPath">图片输出路径</param>         /// <param name="imageName">生成图片的名字</param>         /// <param name="startPageNum">从PDF文档的第几页开始转换</param>         /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>         /// <param name="imageFormat">设置所需图片格式</param>         /// <param name="definition">设置图片的清晰度,数字越大越清晰 5</param>         /// <param name="imageSize">设置图片总体大小 35</param>         public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,             string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int definition, int imageSize)         {             PDFFile pdfFile = PDFFile.Open(pdfInputPath);             if (!Directory.Exists(imageOutputPath))             {                 Directory.CreateDirectory(imageOutputPath);             }             // validate pageNum             if (startPageNum <= 0)             {                 startPageNum = 1;             }             if (endPageNum > pdfFile.PageCount)             {                 endPageNum = pdfFile.PageCount;             }             if (startPageNum > endPageNum)             {                 int tempPageNum = startPageNum;                 startPageNum = endPageNum;                 endPageNum = startPageNum;             }             // start to convert each page             for (int i = startPageNum; i <= endPageNum; i++)             {                 Bitmap pageImage = pdfFile.GetPageImage(i - 1, imageSize * definition); // 35*5                  pageImage.Save(imageOutputPath + imageName, imageFormat);                 pageImage.Dispose();             }             pdfFile.Dispose();         }     }

 

 

外部方法调用

 

public class Test
{
// odf字符串转pdf转图片
string ret = new OfdFileHelper().OFDFileDownload("odf-strbase64文件字符串", "吴天");
// 压缩文件
string path = new OfdFileHelper().FileDeal();
}

以下是完整代码

using System; using System.IO; using System.IO.Compression; using System.Drawing; using System.Drawing.Imaging; using Spire.Pdf.Conversion; using O2S.Components.PDFRender4NET; using NPOI.SS.Formula.Functions;  namespace OfdTest {     public class OfdFileHelper     {          /// <summary>         ///  OFD文件下载到本地         /// </summary>         /// <param name="strbase64">strbase64 ofd文件</param>         /// <param name="fileName">文件名称 =吴天</param>         /// <returns></returns>         public string OFDFileDownload(string strbase64, string fileName)         {             string result = "";             try             {                  string targetFilePath = @"D:OFD20240725";                  if (!Directory.Exists(targetFilePath))                 {                     Directory.CreateDirectory(targetFilePath);                 }                 string path = targetFilePath + @"吴天.ofd";                 string pathPdf = targetFilePath + @"吴天.pdf";                  //base64编码的文本 转为 图片                  byte[] fileData = Convert.FromBase64String(strbase64);                 FileStream fs = new FileStream(path, FileMode.Create);                 fs.Write(fileData, 0, fileData.Length);                 fs.Close();                  Stream fst = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);//将Path该文件读取为流格式 文件大小: fst.Length                   // odf文件地址  string path =  @"D:OFD20240725吴天.ofd";                 OfdConverter converter = new OfdConverter(path);                 //pathPdf pdf 文件地址    string pathPdf = "D:OFD20240725吴天.pdf";                 converter.ToPdf(pathPdf);                  string pngFilePath = targetFilePath + "\"; // 输出png文件路径                  string pngFile = fileName + ".Png"; // 输出png文件                   ConvertPDF2Image(pathPdf, pngFilePath, pngFile, 1, 1, ImageFormat.Png, 5, 35);                 result = pngFilePath;             }             catch (Exception ex)             {                 string str = ex.ToString();             }              return result;         }           /// <summary>         /// 找到已经下载的文件,打包文件         /// </summary>             /// <returns></returns>         public string FileDeal()         {             string filePath = "";             try             {                 string targetPath = @"D:Download";                 string targetFilePath = @"D:Download" + DateTime.Now.ToString("yyyy-MM-ddHHmm");                 DateTime date = DateTime.Now;                 //StreamWriter sw = new StreamWriter(fs);//创建输入流                  //sw.Write(""); //写入文本信息                  if (!Directory.Exists(targetFilePath))                 {                     Directory.CreateDirectory(targetFilePath);                 }                  try                 {                     string path = @"OFD20240725吴天.Png"; // 原目录文件                     var destFile = Path.Combine(targetFilePath, "吴天.Png");                     File.Copy(path, destFile, true);// 复制到新目录                     File.SetAttributes(path, FileAttributes.Normal);                 }                 catch (Exception ex)                 { // 下载失败,记录失败原因                     File.AppendAllText(targetFilePath + @"log" + date.ToString("yyyy-MM-dd") + ".txt", "rn" + ex.Message);                 }                 // 压缩                 string filename = $"压缩文件test{DateTime.Now.ToString("yyyyMMddHHmmss")}";                 string zipName = targetPath + filename + ".zip";                 CompressDirectoryZip(targetFilePath + @"", zipName);                  // 打包后的文件地址 targetFilePath                 Stream fileStream1 = new FileStream(zipName, FileMode.Open, FileAccess.Read, FileShare.Read);//将Path该文件读取为流格式                 byte[] expectedBytes = new byte[fileStream1.Length];                 fileStream1.Read(expectedBytes, 0, expectedBytes.Length);//读取该流文件字节,保存到对象中,为测试比对做准备`在这里插入代码片`                  filePath = zipName;// 压缩文件地址             }             catch (Exception ex)             {                 string ret = ex.Message;             }             return filePath;         }           /// <summary>         /// 将指定目录压缩为Zip文件         /// </summary>         /// <param name="folderPath">文件夹地址 D:/1/ </param>         /// <param name="zipPath">zip地址 D:/1.zip </param>         public static void CompressDirectoryZip(string folderPath, string zipPath)         {             DirectoryInfo directoryInfo = new DirectoryInfo(zipPath);              if (directoryInfo.Parent != null)             {                 directoryInfo = directoryInfo.Parent;             }              if (!directoryInfo.Exists)             {                 directoryInfo.Create();             }              System.IO.Compression.ZipFile.CreateFromDirectory(folderPath, zipPath, CompressionLevel.Optimal, false);         }           /// <summary>         /// 将PDF文档转换为图片的方法         /// </summary>         /// <param name="pdfInputPath">PDF文件路径</param>         /// <param name="imageOutputPath">图片输出路径</param>         /// <param name="imageName">生成图片的名字</param>         /// <param name="startPageNum">从PDF文档的第几页开始转换</param>         /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>         /// <param name="imageFormat">设置所需图片格式</param>         /// <param name="definition">设置图片的清晰度,数字越大越清晰 5</param>         /// <param name="imageSize">设置图片总体大小 35</param>         public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,             string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, int definition, int imageSize)         {             PDFFile pdfFile = PDFFile.Open(pdfInputPath);             if (!Directory.Exists(imageOutputPath))             {                 Directory.CreateDirectory(imageOutputPath);             }             // validate pageNum             if (startPageNum <= 0)             {                 startPageNum = 1;             }             if (endPageNum > pdfFile.PageCount)             {                 endPageNum = pdfFile.PageCount;             }             if (startPageNum > endPageNum)             {                 int tempPageNum = startPageNum;                 startPageNum = endPageNum;                 endPageNum = startPageNum;             }             // start to convert each page             for (int i = startPageNum; i <= endPageNum; i++)             {                 Bitmap pageImage = pdfFile.GetPageImage(i - 1, imageSize * definition); // 35*5                  pageImage.Save(imageOutputPath + imageName, imageFormat);                 pageImage.Dispose();             }             pdfFile.Dispose();         }      }
}