首页 文章

在.NET Core 2.0中合并PDF并将PDF转换为PNG图像

提问于
浏览
2

我正在寻找第三方.dll,它可以支持将pdf合并为一个,并将合并的pdf转换为一个.PNG图像文件 .

我知道Ghostscript或pdfsharp支持.NET框架,但不支持.NET核心2.0框架 .

如果有人可以请建议任何可以合并所有pdf的第三方聚会dll,并将合并的pdf转换为.NET核心2.0中的PNG图像 .

有任何帮助或建议来达到这个要求吗?..提前谢谢!

2 回答

  • 1

    你可以使用iTextSharp.LGPLv2.Core合并pdf文件,效果很好 . 请check this tutorial . 它也支持.NETStandard .

    using System;
        using System.Collections.Generic;
        using System.IO;
        using iTextSharp.text;
        using iTextSharp.text.pdf;
    
        namespace HelveticSolutions.PdfLibrary
        {
          public static class PdfMerger
          {
            /// <summary>
            /// Merge pdf files.
            /// </summary>
            /// <param name="sourceFiles">PDF files being merged.</param>
            /// <returns></returns>
            public static byte[] MergeFiles(List<byte[]> sourceFiles)
            {
              Document document = new Document();
              using (MemoryStream ms = new MemoryStream())
              {
                PdfCopy copy = new PdfCopy(document, ms);
                document.Open();
                int documentPageCounter = 0;
    
                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                {
                  // Create pdf reader
                  PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
                  int numberOfPages = reader.NumberOfPages;
    
                  // Iterate through all pages
                  for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                  {
                    documentPageCounter++;
                    PdfImportedPage importedPage = copy.GetImportedPage(reader, currentPageIndex);
                    PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(importedPage);
    
                    // Write header
                    ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                        new Phrase("PDF Merger by Helvetic Solutions"), importedPage.Width / 2, importedPage.Height - 30,
                        importedPage.Width < importedPage.Height ? 0 : 1);
    
                    // Write footer
                    ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                        new Phrase(String.Format("Page {0}", documentPageCounter)), importedPage.Width / 2, 30,
                        importedPage.Width < importedPage.Height ? 0 : 1);
    
                    pageStamp.AlterContents();
    
                    copy.AddPage(importedPage);
                  }
    
                  copy.FreeReader(reader);
                  reader.Close();
                }
    
                document.Close();
                return ms.GetBuffer();
              }
            }
          }
        }
    
  • 1

    我找到了一个适合我需要的库,所以我写了一个围绕 PDFium 的C#包装器,它有 BSD 3-clause 许可证,我的包装器代码在MIT下发布,所以你可以使用 NuGet 包或直接使用代码 . 回购可以在这里找到docnet .

相关问题