使用 ITextSharp(旧版本)将多个文件合并为一个 pdf
Merge multiple files into a pdf with ITextSharp (Old version)
首先,很抱歉这个不费力的问题..
我把这些问题作为参考:
Merging multiple PDFs using iTextSharp in c#.net
我的要求有点不同。
我收到多个文件的输入,可以是图像 and/or pdf,我需要将它们合并成 1 个 pdf。
每个图像都有自己的页面。所以如果你有 2 张图片,你会得到一个 pdf,每页有 1 张图片。如果您有 1 个 pdf 2 页、1 张图片和另一个 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。我相信这个库支持它:)
如果有人有任何线索,那就太棒了!如果没有,请随时因为一个不费吹灰之力的问题而对我生气。当我自己弄清楚时,我会 post 更新!
我找到解决办法了!
我的代码如下:
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
//
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.
//
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
//
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();
}
}
}
首先,很抱歉这个不费力的问题.. 我把这些问题作为参考:
Merging multiple PDFs using iTextSharp in c#.net
我的要求有点不同。
我收到多个文件的输入,可以是图像 and/or pdf,我需要将它们合并成 1 个 pdf。
每个图像都有自己的页面。所以如果你有 2 张图片,你会得到一个 pdf,每页有 1 张图片。如果您有 1 个 pdf 2 页、1 张图片和另一个 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。我相信这个库支持它:)
如果有人有任何线索,那就太棒了!如果没有,请随时因为一个不费吹灰之力的问题而对我生气。当我自己弄清楚时,我会 post 更新!
我找到解决办法了!
我的代码如下:
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
//
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.
//
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
//
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();
}
}
}