base64转图片转pdf.txt C#

                string pdf_file_path = "";
                string pdf_file_name = "";
                string labelUrl = "";
                string strLabelSavePath = "";
                string strFileName = "PRINT_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                if (resultroot.WayBillUniteResponseData.LogisticalNumFile != null)
                {

                    pdf_file_path = string.Format("/api-lable/pdf/{0}/", DateTime.Now.ToString("yyyyMMdd"));
                    pdf_file_name = string.Format("{0}.pdf", System.Guid.NewGuid().ToString());
                    string pdf_output_filepath = System.Web.HttpContext.Current.Server.MapPath("~" + pdf_file_path);

                    byte[] imagebyte = Convert.FromBase64String(resultroot.WayBillUniteResponseData.LogisticalNumFile);

                    strLabelSavePath = pdf_output_filepath + pdf_file_name;

                    string strDirectory = System.IO.Path.GetDirectoryName(strLabelSavePath);
                    if (!Directory.Exists(strDirectory))
                    {
                        Directory.CreateDirectory(strDirectory);
                    }

                    using (var mem = new MemoryStream(imagebyte))
                    {
                        using (var file = new FileStream(strLabelSavePath, FileMode.Create, FileAccess.Write))
                        {
                            mem.WriteTo(file);
                        }
                    }
                    labelUrl = "http://" + DTRequest.GetCurrentFullHost() + pdf_file_path + pdf_file_name;
                }
C#中,将Base64编码的PDF换为图片Base64通常需要借助第三方库,因为.NET内置的处理能力有限。你可以使用一些开源库如iTextSharp(针对PDF)、System.Drawing.Common(用于图像操作),或者在线API服务来进行这样的换。 以下是一个简单的示例,使用iTextSharp库,假设已经安装了`iTextsharp.text.pdf`和`System.Drawing.Imaging`: ```csharp using System; using System.IO; using iTextSharp.text.pdf; using System.Drawing.Imaging; public string Base64PdfToImage(string pdfBase64) { byte[] pdfBytes = Convert.FromBase64String(pdfBase64); // 创建临时文件保存PDF内容 var tempFile = Path.GetTempFileName(); using (FileStream fileStream = File.OpenWrite(tempFile)) { fileStream.Write(pdfBytes, 0, pdfBytes.Length); // 使用iTextSharp读取PDF换为image PdfReader reader = new PdfReader(tempFile); using (MemoryStream imgStream = new MemoryStream()) { Image image = null; try { image = iTextSharp.text.Image.GetInstance(reader.GetPage(1)); // 取出第一页,按需替换页码 image.SetAbsolutePosition(0, 0); // 调整位置到原点 image.ScaleToFit(800, 600); // 自适应缩放尺寸 image.Rotation = 0; // 设置旋角度,如需要 image.Save(imgStream, ImageFormat.Png); // 保存为PNG格式 imgStream.Seek(0, SeekOrigin.Begin); byte[] imgBytes = imgStream.ToArray(); return Convert.ToBase64String(imgBytes); } finally { if (image != null) image.Dispose(); } } } File.Delete(tempFile); // 删除临时文件 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值