是否可以使用iTextSharp将现有PDF文件附加到动态创建的PDF中?我根据下面的代码尝试使用PdfCopy,但我收到Cannot access a closed Stream
错误。
我已经看到将实际PDF文件合并在一起的示例,但我没有存储完成的PDF,只是将MemoryStream输出到FilestreamResult:
private readonly float _spacingBefore = 3f;
private readonly float _spacingAfter = 3f;
private readonly float _totalWidth = PageSize.A4.Width - 80;
public MemoryStream CreateMemoryStream()
{
var document = new Document(PageSize.A4, 25, 25, 30, 30);
var workStream = new MemoryStream();
PdfWriter.GetInstance(document, workStream);
var pdfCopy = new PdfCopy(document, workStream);
document.Open();
var pdfPTable = new PdfPTable(1)
{
TotalWidth = _totalWidth,
LockedWidth = true,
SpacingBefore = _spacingBefore,
SpacingAfter = _spacingAfter
};
float[] widths = { 272f };
pdfPTable.SetWidths(widths);
var image = Image.GetInstance(HttpContext.Current.Server.MapPath("\\Images\\Logo.png"));
image.ScaleToFit(125f, 125f);
image.Alignment = Image.RIGHT_ALIGN;
var pdfPCell = new PdfPCell(image)
{
Border = 0,
HorizontalAlignment = Element.ALIGN_RIGHT
};
pdfPTable.AddCell(pdfPCell);
document.Add(pdfPTable);
pdfCopy.AddDocument(new PdfReader(HttpContext.Current.Server.MapPath("/Documents/Test.pdf")));
if (document != null)
{
document.Close();
}
var file = workStream.ToArray();
var memoryStream = new MemoryStream();
memoryStream.Write(file, 0, file.Length);
memoryStream.Position = 0;
return memoryStream;
}
public FileStreamResult Pdf(int id)
{
var memoryStream = CreateMemoryStream();
HttpContext.Response.AddHeader("content-disposition", "inline; filename=" + id + ".pdf");
return File(memoryStream, "application/pdf");
}
我还试过以下代替pdfCopy.AddDocument(new PdfReader(HttpContext.Current.Server.MapPath("/Documents/Test.pdf")));
var pdfReader = new PdfReader(HttpContext.Current.Server.MapPath("/Documents/Test.pdf"));
var numberOfPages = pdfReader.NumberOfPages;
for (var i = 0; i < numberOfPages;)
{
pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, ++i));
}
无论采用何种方法,我仍然会收到Cannot access a closed Stream
错误。
非常感谢任何帮助: - )
答案 0 :(得分:2)
以下是我的问题的解决方案,我希望它对其他人有用:
public MemoryStream CreateMemoryStream()
{
byte[] dynamicPdfBytes;
using (var dynamicPDfMemoryStream = new MemoryStream())
{
using (var document = new Document(PageSize.A4, 25, 25, 30, 30))
{
PdfWriter.GetInstance(document, dynamicPDfMemoryStream);
document.Open();
var pdfPTable = new PdfPTable(1)
{
TotalWidth = _totalWidth,
LockedWidth = true,
SpacingBefore = _spacingBefore,
SpacingAfter = _spacingAfter
};
float[] widths = { 272f };
pdfPTable.SetWidths(widths);
var image = Image.GetInstance(HttpContext.Current.Server.MapPath("/Images/logo.png"));
image.ScaleToFit(125f, 125f);
image.Alignment = Image.RIGHT_ALIGN;
var pdfPCell = new PdfPCell(image)
{
Border = 0,
HorizontalAlignment = Element.ALIGN_RIGHT
};
pdfPTable.AddCell(pdfPCell);
document.Add(pdfPTable);
}
dynamicPdfBytes = dynamicPDfMemoryStream.ToArray();
}
byte[] pdfBytes;
using (var pdfReader = new PdfReader(HttpContext.Current.Server.MapPath("/Documents/Test.pdf")))
{
using (var pdfMemoryStream = new MemoryStream())
{
var pdfStamper = new PdfStamper(pdfReader, pdfMemoryStream);
var acroFields = pdfStamper.AcroFields;
acroFields.SetField("TestField", "This is a test");
pdfStamper.FormFlattening = true;
pdfStamper.Close();
pdfBytes = pdfMemoryStream.ToArray();
}
}
var files = new List<byte[]> { dynamicPdfBytes, pdfBytes };
byte[] array;
using (var arrayMemoryStream = new MemoryStream())
{
var document = new Document(PageSize.A4, 25, 25, 30, 30);
var pdfWriter = PdfWriter.GetInstance(document, arrayMemoryStream);
document.Open();
var directContent = pdfWriter.DirectContent;
foreach (var bytes in files)
{
var pdfReader = new PdfReader(bytes);
var numberOfPages = pdfReader.NumberOfPages;
for (var i = 1; i <= numberOfPages; i++)
{
document.NewPage();
var page = pdfWriter.GetImportedPage(pdfReader, i);
directContent.AddTemplate(page, 0, 0);
}
}
document.Close();
array = arrayMemoryStream.ToArray();
}
var memoryStream = new MemoryStream();
memoryStream.Write(array, 0, array.Length);
memoryStream.Position = 0;
return memoryStream;
}