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



首先,对于这个低成本的问题感到抱歉。。我用这些问题作为参考:

在c#.net中使用iTextSharp合并多个pdf
c#iTextSharp通过字节数组合并多个pdf

我的要求有点不同。

我得到了多个文件的输入,要么是图像,要么是pdf,我需要将它们合并到一个pdf中
每张图片都有自己的页面。因此,如果你有2张图片,你会得到一张每页有1张图片的pdf。如果你有一个pdf有2页,一个图像和另一个pdf的3个图像,得到的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;
}
}
}
}

我想知道是否有人有实施这项技术的诀窍https://github.com/VahidN/iTextSharp.LGPLv2.Core.前面提到的问题将多个pdf合并在一起,但我也需要合并图像。

最后,我的代码需要在.NET 5.0、windows和linux上运行。我相信这个库支持它:(

如果有人有任何线索,那将是惊人的!如果没有,请随意为我的一个低难度问题而生气。。当我自己弄清楚的时候,我会发布最新消息!

我找到了一个解决方案!

我的代码如下:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
public class ITextSharpPdfMerger
{
private const float _imageLeftMargin = 10f;
private const float _imageRightMargin = 10f;
private const float _imageBottomMargin = 30f;
private const float _imageTopMargin = 30f;
// https://github.com/VahidN/iTextSharp.LGPLv2.Core
// https://stackoverflow.com/a/6056801/3013479
public byte[] Create(IEnumerable<(string ContentType, byte[] Content)> blobs)
{
Document document = null;
PdfCopy copy = null;
using (var stream = new MemoryStream())
{
try
{
document = new Document();
copy = new PdfCopy(document, stream);
document.Open();
foreach (var blob in blobs)
{
if (blob.ContentType.StartsWith("image/"))
{
AddImage(copy, blob.Content);
}
else if (blob.ContentType == "application/pdf")
{
AddPdf(copy, blob.Content);
}
else
{
throw new ArgumentException($"Blob with ContentType {blob.ContentType} is not supported for merging.");
}
}
}
finally
{
document?.Close();
copy?.Close();
}
return stream.ToArray();
}
}
private static void AddPdf(PdfCopy copy, byte[] content)
{
PdfReader reader = null;
try
{
reader = new PdfReader(content);
// Grab each page from the PDF and copy it
for (int i = 1; i <= reader.NumberOfPages; i++)
{
var page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
}
// A PDF can have a form; we copy it into the resulting pdf.
// Example: https://web.archive.org/web/20210322125650/https://www.windjack.com/PDFSamples/ListPrograming_Part1_AcroForm.pdf 
var form = reader.AcroForm;
if (form != null)
{
copy.CopyAcroForm(reader);
}
}
finally
{
reader?.Close();
}
}
private static void AddImage(PdfCopy copy, byte[] content)
{
// We have a little workaround to add images because we use PdfCopy which is only useful for COPYING and doesn't work for adding pages manually.
// So we create a new PDF in memory containing the image, and then we copy that PDF into the resulting PDF.
// https://stackoverflow.com/a/26111677/3013479
Document document = null;
PdfWriter writer = null;
PdfReader reader = null;
using (var stream = new MemoryStream())
{
try
{
document = new Document();
writer = PdfWriter.GetInstance(document, stream);
document.Open();
if (!document.NewPage())
{
throw new Exception("New page could not be created");
}
var image = Image.GetInstance(content);
// Make the image fit on the page
// https://stackoverflow.com/q/4932187/3013479
image.Alignment = Element.ALIGN_MIDDLE;
var pageWidth = copy.PageSize.Width - (_imageLeftMargin + _imageRightMargin);
var pageHeight = copy.PageSize.Height - (_imageBottomMargin + _imageTopMargin);
image.ScaleToFit(pageWidth, pageHeight);
if (!document.Add(image))
{
throw new Exception("Unable to add image to page");
}
// These are required for the PdfReader instantation to succeed
document.Close();
writer.Close();
reader = new PdfReader(stream.ToArray());
copy.AddPage(copy.GetImportedPage(reader, 1));
}
finally
{
document?.Close();
reader?.Close();
writer?.Close();
}
}
}

相关内容

  • 没有找到相关文章

最新更新