Explorar el Código

腾程三方lis 存在多张图片 代码进行优化

zzh_love hace 6 meses
padre
commit
d0377c7380
Se han modificado 2 ficheros con 83 adiciones y 7 borrados
  1. 32 2
      LisPacsDataUpload/SqlServerHelper.cs
  2. 51 5
      LisPacsDataUpload/business/PdfUtils.cs

+ 32 - 2
LisPacsDataUpload/SqlServerHelper.cs

@@ -3,7 +3,7 @@ using System.Configuration;
 using System.Data.SqlClient;
 using System.Data;
 using Common;
-
+using System.Collections.Generic;
 
 namespace LisPacsDataUpload
 {
@@ -38,6 +38,36 @@ namespace LisPacsDataUpload
             }
         }
 
+        public static List<string> GetPdfUrlList(string querySql)
+        {
+            var pdfUrls = new List<string>();
+
+            using (SqlConnection connection = new SqlConnection(GetConnStr()))
+            {
+                SqlCommand command = new SqlCommand(querySql, connection);
+
+                try
+                {
+                    connection.Open();
+                    SqlDataReader reader = command.ExecuteReader();
+
+                    while (reader.Read())
+                    {
+                        // 假设 pdfUrl 列不为空,并且数据库中的数据类型与 string 兼容
+                        string pdfUrl = reader.GetString(reader.GetOrdinal("pdfUrl"));
+                        pdfUrls.Add(pdfUrl);
+                    }
+                }
+                catch (Exception ex)
+                {
+                    LogHelper.Info("An error occurred: " + ex.Message);
+                    // 在这里添加适当的错误处理逻辑,比如日志记录
+                }
+            }
+
+            return pdfUrls;
+        }
+
         public static DataRow ExecuteSingleRowQuery(string query)
         {
 
@@ -60,7 +90,7 @@ namespace LisPacsDataUpload
                         {
                             return null;
                         }
-               
+
                 }
             }
             catch (Exception ex)

+ 51 - 5
LisPacsDataUpload/business/PdfUtils.cs

@@ -4,6 +4,7 @@ using iTextSharp.text.pdf;
 using LisPacsDataUpload.Models.Report;
 using Newtonsoft.Json;
 using System;
+using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
 using System.Diagnostics;
@@ -113,12 +114,10 @@ namespace LisPacsDataUpload.business
                 // 三方lis
                 string three_sql = @"select pdfUrl from three_lis_pdf where yzid =" + bbid;
                 LogHelper.Info("three_sql  "+ three_sql);
-                DataRow data = SqlServerHelper.ExecuteSingleRowQuery(three_sql);
-                if (data!=null)
+                List<string> pdfs = SqlServerHelper.GetPdfUrlList(three_sql);
+                if (pdfs != null && pdfs.Count()>0)
                 {
-                    string jpgUrl = data["pdfUrl"].ToString();
-                    LogHelper.Info("三方原始jpgUrl" + jpgUrl);
-                    GetPdfFromJpg(jpgUrl, path);
+                    GetPdfFromJpgList(pdfs, path);
                     return path;
                 }
                 return "三方lispdf报告获取失败";
@@ -164,6 +163,53 @@ namespace LisPacsDataUpload.business
             LogHelper.Info("JPG 转换为 PDF 成功!");
         }
 
+        public static void GetPdfFromJpgList(List<string> jpgUrls, string pdfUrl)
+        {
+            LogHelper.Info("开始从jpg列表转换pdf");
+            using (FileStream fs = new FileStream(pdfUrl, FileMode.Create))
+            {
+                // 创建一个文档对象
+                Document document = new Document();
+                // 创建一个 PDF 写入器,将文档写入文件流
+                PdfWriter writer = PdfWriter.GetInstance(document, fs);
+                document.Open();
+                foreach (string jpgUrl in jpgUrls)
+                {
+                    try
+                    {
+                        LogHelper.Info("三方pdfUrl="+ jpgUrl);
+                        // 创建一个图像对象并加载 JPG 文件
+                        Image image = Image.GetInstance(jpgUrl);
+                        // 设置图像在 PDF 中的尺寸(可以根据需要调整)
+                        // 这里为了保持图片比例,我们先获取图片的原始尺寸
+                        float imgWidth = image.ScaledWidth;
+                        float imgHeight = image.ScaledHeight;
+
+                        // 根据需要调整图片大小,这里以A4纸大小减去边距为例
+                        float pdfWidth = document.PageSize.Width - 20;
+                        float pdfHeight = document.PageSize.Height - 20;
+
+                        // 根据宽度或高度调整图片大小,保持比例
+                        float scaleFactor = Math.Min(pdfWidth / imgWidth, pdfHeight / imgHeight);
+                        image.ScaleAbsolute(imgWidth * scaleFactor, imgHeight * scaleFactor);
+
+                        // 将图像添加到 PDF 文档中
+                        document.Add(image);
+                        // 添加一个空行以分隔图片(可选)
+                        document.Add(new Paragraph(string.Empty));
+                        document.NewPage(); // 如果需要在每张图片后添加新页,则使用此行
+                    }
+                    catch (Exception ex)
+                    {
+                        LogHelper.Error(ex ,"无法加载图片"+jpgUrl);
+                    }
+                }
+                document.Close();
+            }
+            LogHelper.Info("JPG 列表转换为 PDF 成功!");
+        }
+
+
         public static string GeneratePdf(int lisVersion, string reportNumber, string bbid, string yzid)
         {
             string pdfUrl = getPdfParamByLisVersion(lisVersion, reportNumber, bbid, yzid);