c# - 如何在 C# 中将 PDF Base64 转换为 pdf,然后将 PDF 转换为图像

标签 c# pdf file-conversion

添加的代码如下: 图像对象位于library.imaging中。

"using System.Drawing;"
"using System.Drawing.Imaging;"
{
  byte[] b = Convert.FromBase64String("R0lGODlhAQABAIAAA");
  Image image;
  using (MemoryStream memstr = new MemoryStream(b))
  {
    image = Image.FromStream(memstr);
  }
}

这是我正在编写的新代码:

{
  string base64BinaryStr = " ";
  byte[] PDFDecoded = Convert.FromBase64String(base64BinaryStr);
  string FileName = (@"C:\Users\Downloads\PDF " + DateTime.Now.ToString("dd-MM-yyyy-hh-mm"));

  BinaryWriter writer = new BinaryWriter(File.Create(FileName + ".pdf"));            
  writer.Write(PDFDecoded);
  string s = Encoding.UTF8.GetString(PDFDecoded);
}   

最佳答案

这是您当前的代码:

byte[] PDFDecoded = Convert.FromBase64String(base64BinaryStr);
string FileName = (@"C:\Users\Downloads\PDF " + DateTime.Now.ToString("dd-MM-yyyy-hh-mm"));

BinaryWriter writer = new BinaryWriter(File.Create(FileName + ".pdf"));            
writer.Write(PDFDecoded);

您实际上并不需要 BinaryWriter 来实现此目的。 File.Create 已经为您提供了一个 FileStream:

FileStream writer = File.Create(FileName + ".pdf");
writer.Write(PDFDecoded, 0, PDFDecoded.Length);

但这仍然会遇到您遇到的问题,因为您没有将数据刷新到其中。我们还需要关闭该文件。值得庆幸的是,我们可以将其包装在 using 中,它将为我们完成这两件事:

using (FileStream writer = File.Create(FileName + ".pdf"))
{
    writer.Write(PDFDecoded, 0, PDFDecoded.Length); 
}

但更简单的方法是:

File.WriteAllBytes(FileName + ".pdf", PDFDecoded);

至于 PDF -> Image,您可能需要查看是否有可用的库(搜索“PDF to Image NuGet”)可以帮助您解决此问题,因为我认为没有构建任何东西-in.

关于c# - 如何在 C# 中将 PDF Base64 转换为 pdf,然后将 PDF 转换为图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716794/

相关文章:

c# - Entity Framework Code First 在创建 1 :n relationship 时在数据库中生成两个外键列

ios - 如何在 iOS 应用程序中将任何格式(.txt、.Doc)文件转换为 epub 文件

c# - Windows 工作流基础中基于人工的任务

javascript - 图像质量差使用 JSPDF 将 Google Charts 插入 PDF

c# - PDF阅读高亮文本(高亮注释)使用C#

c# - 使用 iText7 创建新 PDF 时如何将 HTML 放入表格单元格?

html - 如何使用 Cocoa 将 html 文件转换为 pdf

将 m4a 转换为 WAV 的 iOS 代码

c# - FXCop 自定义规则未出现在规则集中

c# - 如何实现MVC寻呼机?