public static byte[] CreatePdfA3WithAttachment(byte[] pdfContent, byte[] xmlData, Stream iccProfileData)
{
using (MemoryStream pdfStream = new MemoryStream())
{
PdfWriter writer = new PdfWriter(pdfStream);
PdfADocument pdfDocument = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_3B,
new PdfOutputIntent("Custom ICC Profile", "", "https://www.color.org", "", iccProfileData));
Document document = new Document(pdfDocument);
pdfDocument.GetDocumentInfo().SetTitle("PDF/A-3 Document with XML Attachment");
pdfDocument.GetDocumentInfo().SetAuthor("Author Name");
pdfDocument.GetDocumentInfo().SetSubject("PDF/A-3 Document");
pdfDocument.GetDocumentInfo().SetKeywords("PDF/A-3, XML, iText7");
pdfDocument.SetTagged();
pdfDocument.GetCatalog().SetLang(new PdfString("en-US"));
pdfDocument.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
using (MemoryStream existingPdfStream = new MemoryStream(pdfContent))
{
try
{
PdfReader pdfReader = new PdfReader(existingPdfStream);
PdfDocument existingPdf = new PdfDocument(pdfReader);
existingPdf.CopyPagesTo(1, existingPdf.GetNumberOfPages(), pdfDocument);
existingPdf.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception while adding existing content: " + ex.Message);
}
}
using (MemoryStream xmlStream = new MemoryStream(xmlData))
{
PdfDictionary fileParameter = new PdfDictionary();
PdfFileSpec fileSpec = PdfFileSpec.CreateEmbeddedFileSpec(
pdfDocument, xmlStream.ToArray(), "attached.xml", "attached.xml", PdfName.ApplicationXml, fileParameter, PdfName.Data);
pdfDocument.AddFileAttachment("attached.xml", fileSpec);
PdfArray afArray = new PdfArray();
afArray.Add(fileSpec.GetPdfObject());
pdfDocument.GetCatalog().Put(new PdfName("AF"), afArray);
}
document.Close();
return pdfStream.ToArray();
}
}
Microsoft.Extensions.Logging
would be helpful.Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 290,867 Super User 2024 Season 2
Martin Dráb 229,173 Most Valuable Professional
nmaenpaa 101,156