C# 使用ITextSharp(旧版本)将多个文件合并为pdf

C# 使用ITextSharp(旧版本)将多个文件合并为pdf,c#,pdf,.net-core,itext,C#,Pdf,.net Core,Itext,首先,很抱歉我的问题很难回答。。 我将这些问题作为参考: 我的要求有点不同 我输入了多个文件,包括图像和/或pdf,我需要将它们合并到一个pdf中。 每个图像都有自己的页面。所以,如果你有两张图片,你会得到一个pdf,每页有一张图片。如果您有一个包含2页、1幅图像的pdf和另一个包含3幅图像的pdf,则生成的pdf将包含6页 目前,我在IText7(较新版本)中使用的代码如下: using iText.IO.Image; using iText.Kernel.Pdf; using iText

首先,很抱歉我的问题很难回答。。 我将这些问题作为参考:


我的要求有点不同

我输入了多个文件,包括图像和/或pdf,我需要将它们合并到一个pdf中。
每个图像都有自己的页面。所以,如果你有两张图片,你会得到一个pdf,每页有一张图片。如果您有一个包含2页、1幅图像的pdf和另一个包含3幅图像的pdf,则生成的pdf将包含6页

目前,我在IText7(较新版本)中使用的代码如下:

using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.Collections.Generic;
using System.IO;

namespace Example
{
    public class ITextPdfCreator : IPdfCreator
    {
        public MemoryStream Create(IEnumerable<(string ContentType, byte[] Content)> blobs)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var writer = new PdfWriter(memoryStream))
                {
                    var pdf = new PdfDocument(writer);
                    var document = new Document(pdf);
                    var firstIteration = true;

                    foreach (var blob in blobs)
                    {
                        if (!firstIteration)
                        {
                            document.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                        }

                        if (blob.ContentType.StartsWith("image/"))
                        {
                            var content = new Image(ImageDataFactory.Create(blob.Content));
                            document.Add(content);
                        }
                        else if (blob.ContentType.StartsWith("application/pdf"))
                        {
                            Stream stream = new MemoryStream(blob.Content);
                            var d = new PdfDocument(new PdfReader(stream));
                            d.CopyPagesTo(1, d.GetNumberOfPages(), pdf, pdf.GetNumberOfPages() + 1);
                        }

                        firstIteration = false;
                    }
                    document.Close();
                }

                return memoryStream;
            }
        }
    }
}
使用iText.IO.Image;
使用iText.Kernel.Pdf;
使用iText.Layout;
使用iText.Layout.Element;
使用System.Collections.Generic;
使用System.IO;
名称空间示例
{
公共类ITextPdfCreator:IPdfCreator
{
公共内存流创建(IEnumerable blob)
{
使用(var memoryStream=new memoryStream())
{
使用(var writer=newpdfwriter(memoryStream))
{
var pdf=新的pdf文档(编写器);
var文件=新文件(pdf);
var firstIteration=true;
foreach(blob中的var blob)
{
if(!firstIteration)
{
添加(新的AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_页));
}
if(blob.ContentType.StartsWith(“image/”)
{
var content=新图像(ImageDataFactory.Create(blob.content));
文件。添加(内容);
}
else if(blob.ContentType.StartsWith(“application/pdf”))
{
Stream=新的MemoryStream(blob.Content);
var d=新PDF文档(新PDF阅读器(流));
d、 CopyPagesTo(1,d.GetNumberOfPages(),pdf,pdf.GetNumberOfPages()+1);
}
第一次迭代=假;
}
document.Close();
}
返回内存流;
}
}
}
}
我想知道是否有人知道如何用它来实现这一点。前面提到的问题将多个pdf合并在一起,但我也需要合并图像

最后,我的代码需要在.NET5.0、windows和linux上运行。我相信这个lib支持它:)


如果有人有任何线索,那太棒了!如果不是的话,请不要为一个不费吹灰之力的问题而生我的气。。当我自己弄明白的时候,我会发布一个更新

我找到了解决办法

我的代码如下:

using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using System.Collections.Generic;
using System.IO;

namespace Example
{
    public class ITextPdfCreator : IPdfCreator
    {
        public MemoryStream Create(IEnumerable<(string ContentType, byte[] Content)> blobs)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var writer = new PdfWriter(memoryStream))
                {
                    var pdf = new PdfDocument(writer);
                    var document = new Document(pdf);
                    var firstIteration = true;

                    foreach (var blob in blobs)
                    {
                        if (!firstIteration)
                        {
                            document.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
                        }

                        if (blob.ContentType.StartsWith("image/"))
                        {
                            var content = new Image(ImageDataFactory.Create(blob.Content));
                            document.Add(content);
                        }
                        else if (blob.ContentType.StartsWith("application/pdf"))
                        {
                            Stream stream = new MemoryStream(blob.Content);
                            var d = new PdfDocument(new PdfReader(stream));
                            d.CopyPagesTo(1, d.GetNumberOfPages(), pdf, pdf.GetNumberOfPages() + 1);
                        }

                        firstIteration = false;
                    }
                    document.Close();
                }

                return memoryStream;
            }
        }
    }
}
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用制度;
使用System.Collections.Generic;
使用System.IO;
公共类ITextSharpdPdfmerger
{
private const float_imageLeftMargin=10f;
私有常量浮点_imageRightMargin=10f;
私有常量浮动_imageBottomMargin=30f;
私有常量浮点值_imageTopMargin=30f;
// https://github.com/VahidN/iTextSharp.LGPLv2.Core
// https://stackoverflow.com/a/6056801/3013479
公共字节[]创建(IEnumerable blob)
{
单据=空;
PdfCopy copy=null;
使用(var stream=new MemoryStream())
{
尝试
{
文件=新文件();
副本=新的PdfCopy(文档、流);
document.Open();
foreach(blob中的var blob)
{
if(blob.ContentType.StartsWith(“image/”)
{
AddImage(副本、blob.Content);
}
else if(blob.ContentType==“application/pdf”)
{
AddPdf(副本,blob.Content);
}
其他的
{
抛出新ArgumentException($“合并时不支持ContentType为{Blob.ContentType}的Blob。”);
}
}
}
最后
{
文档?.Close();
复制?.Close();
}
返回流ToArray();
}
}
私有静态void AddPdf(PdfCopy副本,字节[]内容)
{
PdfReader reader=null;
尝试
{
阅读器=新的PDF阅读器(内容);
//从PDF中抓取每一页并复制它
对于(int i=1;i