I am trying to add a stamp on a generated pdf document using a template.
How can I replace pdfDocument with the generated 1
Document pdfDocument = new Document(dataDir+ “Statement.pdf”);
I am trying to add a stamp on a generated pdf document using a template.
How can I replace pdfDocument with the generated 1
Document pdfDocument = new Document(dataDir+ “Statement.pdf”);
Thanks for contacting support.
Can you please share source file to further investigate this issue on our end. Also please visit this documentation link as well. This will help you to achieve your requirements.
XElement xml = XmlUtils.ToXElement(transactionHistory);
var _template = "";
var _fileName = "";
if (selectedAccount.AccountSubTypeCode != "PT00370")
{
_template = "TransactionHistoryv2";
_fileName = "TransactionHistory";
}
else
{
_template = "LoanAccountStatement";
_fileName = "LoanAccountStatement";
}
var generatedDocument = GenerateDocument(xml, _template);
var textGen = GenerateDocumentHtml(xml, _template);
var test = textGen.Payload;
var newGeneratedDocument = Convert.ToByte(InsertBankStamp(textGen.Payload));
Stream stream = new MemoryStream(newGeneratedDocument);
var response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
response.Content.Headers.Add("Content-Disposition", string.Format("attachment; filename={0}{1}.pdf", _fileName, request.AccountNumber));
return response;
}
private Aspose.Pdf.Document InsertBankStamp(string getPdfDocument)
{
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(getPdfDocument);
// Loop through all the pages
int pageCount = 1;
foreach (Page pdfPage in pdfDocument.Pages)
{
// Create page number stamp
PageNumberStamp pageNumberStamp = new PageNumberStamp
{
// Page number properties
Background = false,
Format = "Page # of " + pdfDocument.Pages.Count,
BottomMargin = 25,
HorizontalAlignment = HorizontalAlignment.Center,
StartingNumber = 1
};
pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");
pageNumberStamp.TextState.FontSize = 8.0F;
pageNumberStamp.TextState.ForegroundColor = Color.DarkGray;
// Create a footer text stamp
PageNumberStamp footerText = new PageNumberStamp
{
// Properties
Background = false,
Format = "© " + DateTime.Now.ToString("yyyy") + " Sasfin.",
BottomMargin = 30,
LeftMargin = 75,
HorizontalAlignment = HorizontalAlignment.Left,
StartingNumber = 1
};
footerText.TextState.Font = FontRepository.FindFont("Arial");
footerText.TextState.FontSize = 7.0F;
footerText.TextState.ForegroundColor = Color.DarkGray;
// Create a footer text stamp
PageNumberStamp footerText2 = new PageNumberStamp
{
// Properties
Background = false,
Format = "All rights reserved.",
BottomMargin = 20,
LeftMargin = 75,
HorizontalAlignment = HorizontalAlignment.Left,
StartingNumber = 1
};
footerText2.TextState.Font = FontRepository.FindFont("Arial");
footerText2.TextState.FontSize = 7.0F;
footerText2.TextState.ForegroundColor = Color.DarkGray;
// Create a footer text stamp
PageNumberStamp footerText3 = new PageNumberStamp
{
// Properties
Background = false,
Format = "Authorised Financial Services Provider 23833",
BottomMargin = 30,
RightMargin = 70,
HorizontalAlignment = HorizontalAlignment.Right,
StartingNumber = 1
};
footerText3.TextState.Font = FontRepository.FindFont("Arial");
footerText3.TextState.FontSize = 7.0F;
footerText3.TextState.ForegroundColor = Color.DarkGray;
// Create a footer text stamp
PageNumberStamp footerText4 = new PageNumberStamp
{
// Properties
Background = false,
Format = "and Registered Credit Provider NCRCP22.",
BottomMargin = 20,
RightMargin = 70,
HorizontalAlignment = HorizontalAlignment.Right,
StartingNumber = 1
};
footerText4.TextState.Font = FontRepository.FindFont("Arial");
footerText4.TextState.FontSize = 7.0F;
footerText4.TextState.ForegroundColor = Color.DarkGray;
// Create FloatingBox object (Bank Stamp)
FloatingBox aBox = new FloatingBox(90, 90)
{
// Set left position for FloatingBox
Left = 170,
Top = 20,
HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center
};
// Add text fragment to paragraphs collection of FloatingBox
aBox.Paragraphs.Add(new TextFragment());
aBox.Paragraphs.Add(new TextFragment("SASFIN BANK"));
aBox.Paragraphs.Add(new TextFragment(" "));
aBox.Paragraphs.Add(new TextFragment("Johannesburg"));
aBox.Paragraphs.Add(new TextFragment(" "));
aBox.Paragraphs.Add(new TextFragment(DateTime.Now.ToString("yyyy-MM-dd")));
aBox.Paragraphs.Add(new TextFragment(" "));
aBox.Paragraphs.Add(new TextFragment("683 000"));
// Set border for FloatingBox
aBox.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Black);
// Set border for FloatingBox
pdfDocument.Pages[pageCount].Paragraphs.Add(aBox);
pdfDocument.Pages[pageCount].AddStamp(pageNumberStamp);
pdfDocument.Pages[pageCount].AddStamp(footerText);
pdfDocument.Pages[pageCount].AddStamp(footerText2);
pdfDocument.Pages[pageCount].AddStamp(footerText3);
pdfDocument.Pages[pageCount].AddStamp(footerText4);
pageCount++;
}
return pdfDocument;
}
public void sendEmail(string Payload)
{
_emailService.SendEmail(new EmailSendRequest
{
EmailContent = Payload,
Subject = "Test",
Recipients = new string[] { "Darryl.kirton@sasfin.com" },
IsHtmlContent = true
});
}
private DocumentTransform.PdfResponse GenerateDocument(XElement xml, string nameOfTemplate)
{
var docTransform = XElement.Parse(new XmlDocument().ReadNode(xml.CreateReader()).OuterXml);
var generatedDocument = _documentTransform.GetPdfDocument(new DocumentTransform.PdfRequest
{
Id = Guid.NewGuid().ToString(),
Payload = docTransform,
Template = nameOfTemplate
});
return generatedDocument;
}
private DocumentTransform.TextResponse GenerateDocumentHtml(XElement xml, string nameOfTemplate)
{
var docTransform = XElement.Parse(new XmlDocument().ReadNode(xml.CreateReader()).OuterXml);
var generatedDocument = _documentTransform.GetTextDocument(new DocumentTransform.TextRequest
{
Id = Guid.NewGuid().ToString(),
Payload = docTransform,
Template = nameOfTemplate
});
return generatedDocument;
}