• 使用ItextSharop合并pdf文件,体积变大的解决


    通用的合并方式导致输出的pdf 文件中嵌入了大量的重复字体。导致文件体积膨胀。

    使用基于内存流的方式,读取文件字节,可以解决重复字体的嵌入问题;

     1    public static string MergeFiles(string targetPdfFilesDir)
     2         {
     3             string outPath = string.Empty;
     4             //验证文件是否存在
     5             if (!Directory.Exists(targetPdfFilesDir))
     6             {
     7                 throw new FileNotFoundException("指定的目录不存在:" + targetPdfFilesDir);
     8             }
     9 
    10             var filePathList = Directory.EnumerateFiles(targetPdfFilesDir, "*.pdf");
    11             if (filePathList.IsEmpty())
    12             {
    13                 return outPath;
    14             }
    15 
    16             //合并pdf文件
    17 
    18             string runningDir = AppDomainTypeFinder.Instance.GetBinDirectory();
    19 
    20             outPath = Path.Combine(runningDir, "temp", Guid.NewGuid().ToString() + ".pdf");
    21 
    22             MergeFiles(outPath, filePathList.ToArray());
    23 
    24             return outPath;
    25         }
    26 
    27 
    28         public static void MergeFiles(string destinationFile, string[] sourceFiles)
    29         {
    30 
    31             try
    32             {
    33 
    34                 byte[] bs = MergePDFs(sourceFiles);
    35                 using (var fsm = new FileStream(destinationFile, FileMode.Create))
    36                 {
    37                     fsm.Write(bs, 0, bs.Length);
    38                     fsm.Flush();
    39                 }
    40 
    41             }
    42             catch (Exception e)
    43             {
    44                 string strOb = e.Message;
    45             }
    46         }
    47         /// <summary>
    48         /// 合并多个pdf文件,并返回合并后的文件字节
    49         /// </summary>
    50         /// <param name="pdfFiles"></param>
    51         /// <returns></returns>
    52         private static byte[] MergePDFs(string[] pdfFiles)
    53         {
    54             if (pdfFiles == null || pdfFiles.Length <= 0)
    55             {
    56                 return null;
    57             }
    58             if (pdfFiles.Length == 1)
    59             {
    60                 return File.ReadAllBytes(pdfFiles[0]);
    61             }
    62 
    63 
    64             PdfReader reader;
    65             Document document;
    66             PdfWriter writer;
    67             MemoryStream msFinalPdf;
    68             using (msFinalPdf = new MemoryStream())
    69             {
    70 
    71                 reader = new PdfReader(pdfFiles[0]);
    72                 using (document = new Document())
    73                 {
    74                     //一个PdfSmartCopy基类
    75                     writer = new PdfSmartCopy(document, msFinalPdf);
    76                     document.Open();
    77 
    78                     for (int k = 0; k < pdfFiles.Length; k++)
    79                     {
    80                         reader = new PdfReader(pdfFiles[k]);
    81                         //将子文件中的页都追加到尾部
    82                         for (int i = 1; i < reader.NumberOfPages + 1; i++)
    83                         {
    84                             ((PdfSmartCopy)writer).AddPage(writer.GetImportedPage(reader, i));
    85                         }
    86                         writer.FreeReader(reader);
    87 
    88                     }
    89                     reader.Close();
    90                     writer.Close();
    91                     document.Close();
    92                 }
    93             }
    94 
    95             return msFinalPdf.ToArray();
    96         }


    下面的代码合并pdf 就会产生体积增加。字体重复被嵌入的问题

    public static void MergeFiles(string destinationFile, string[] sourceFiles)
    {
    
        try
        {
            int f = 0;
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(sourceFiles[f]);
            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            //Console.WriteLine("There are " + n + " pages in the original file.");
            // step 1: creation of a document-object
            Document document = new Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
            // step 3: we open the document
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page;
            int rotation;
            // step 4: we add content
            while (f < sourceFiles.Length)
            {
                int i = 0;
                while (i < n)
                {
                    i++;
                    document.SetPageSize(reader.GetPageSizeWithRotation(i));
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);
                    rotation = reader.GetPageRotation(i);
                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                    //Console.WriteLine("Processed page " + i);
                }
                f++;
                if (f < sourceFiles.Length)
                {
                    reader = new PdfReader(sourceFiles[f]);
                    // we retrieve the total number of pages
                    n = reader.NumberOfPages;
                    //Console.WriteLine("There are " + n + " pages in the original file.");
                }
            }
            // step 5: we close the document
            document.Close();
        }
        catch (Exception e)
        {
            string strOb = e.Message;
        }
    }

    ----下面也会产生重复字体嵌入的----
     public static void Merge(List<String> InFiles, String OutFile)
        {
    
            using (FileStream stream = new FileStream(OutFile, FileMode.Create))
            using (Document doc = new Document())
            using (PdfCopy pdf = new PdfCopy(doc, stream))
            {
                doc.Open();
    
                PdfReader reader = null;
                PdfImportedPage page = null;
    
                //fixed typo
                InFiles.ForEach(file =>
                {
                    reader = new PdfReader(file);
    
                    for (int i = 0; i < reader.NumberOfPages; i++)
                    {
                        page = pdf.GetImportedPage(reader, i + 1);
                        pdf.AddPage(page);
                    }
    
                    pdf.FreeReader(reader);
                    reader.Close();
                    File.Delete(file);
                });
            }
  • 相关阅读:
    leetcode 104. Maximum Depth of Binary Tree 二叉树的最大深度(简单)
    leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(中等)
    leetcode 83. Remove Duplicates from Sorted List 删除排序链表中的重复元素(简单)
    leetcode 637. Average of Levels in Binary Tree 二叉树的层平均值(简单)
    Fiddler的安装与使用
    Redis
    开发那些事儿:如何解决js打包文件体积过大导致的网页加载慢问题?
    AI人工智能识别技术如何助力构建风险监测预警系统?
    H.265流媒体播放器EasyPlayer切换播放协议时,快照无法消失如何处理?
    AI人脸检测/行为识别智能分析网关8大智慧应用场景分析
  • 原文地址:https://www.cnblogs.com/micro-chen/p/11093364.html
Copyright © 2020-2023  润新知