是否可以为从多个MemoryStream动态创建的PDF添加加密?
继我的上一个问题C# Append PDF file to MemoryStream - Cannot access a closed Stream后,我正在尝试添加PDF加密。
从我在下面提供的示例中,如果注释掉行pdfStamper.Close();
,则不会收到错误Cannot access a closed Stream
,但加密也不会应用于生成的PDF。
有人可以告诉我哪里出错了,如果可能的话,如何解决我的问题?
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 pdfStamper = new PdfStamper(pdfReader, arrayMemoryStream);
pdfStamper.SetEncryption(null, _password, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
var numberOfPages = pdfReader.NumberOfPages;
for (var i = 1; i <= numberOfPages; i++)
{
document.NewPage();
var page = pdfWriter.GetImportedPage(pdfReader, i);
directContent.AddTemplate(page, 0, 0);
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
}
document.Close();
array = arrayMemoryStream.ToArray();
}
var memoryStream = new MemoryStream();
memoryStream.Write(array, 0, array.Length);
memoryStream.Position = 0;
return memoryStream;
}
非常感谢任何帮助: - )
答案 0 :(得分:-1)
这可能不是最优雅的解决方案,但它是这个特定问题的解决方案,我希望它对其他人有用。
以下代码可以与原始示例代码一起使用,只需在上面的代码中注释掉所有pdfStamper
引用:
private MemoryStream SecureMemoryStream()
{
var memoryStream = CreateMemoryStream();
return GetSecureMemoryStream(memoryStream);
}
private MemoryStream GetSecureMemoryStream(MemoryStream memoryStream)
{
byte[] array;
using (var secureMemoryStream = new MemoryStream())
{
var pdfReader = new PdfReader(memoryStream);
using (var pdfStamper = new PdfStamper(pdfReader, secureMemoryStream))
{
pdfStamper.SetEncryption(null, _password, PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
pdfStamper.FormFlattening = true;
}
array = secureMemoryStream.ToArray();
}
var outputStream = new MemoryStream();
outputStream.Write(array, 0, array.Length);
outputStream.Position = 0;
return outputStream;
}