C uses iTextSharp to set up all PDF page background map function instances
- 2021-07-22 11:07:36
- OfStack
This article illustrates how C # uses iTextSharp to set all PDF page background map functions. Share it for your reference. The details are as follows:
When generating PDF, although you can set the background map in the page.
But when some content is too long to boast of the page, it is difficult to set the background map and become a blank background page!
The following is the function code of regenerating PDF background map every 1 page!
public void SetPdfBackground(string pdfFilePath)
{
// Regenerated PDF Path of
string destFile = HttpContext.Current.Server.MapPath("sample.pdf");
//create new pdf document
FileStream stream = new FileStream(destFile, FileMode.Create, FileAccess.ReadWrite);
PdfReader reader = new PdfReader(pdfFilePath);
//read pdf stream
PdfStamper stamper = new PdfStamper(reader, stream);
string imagePage = HttpContext.Current.Server.MapPath("../images/2012/bg2.png");
System.Drawing.Image image = System.Drawing.Image.FromFile(imagePage);
var img = Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
img.SetAbsolutePosition(0, 0);
int totalPage = reader.NumberOfPages;
for (int current = 1; current <= totalPage; current++)
{
var canvas = stamper.GetUnderContent(current);
var page = stamper.GetImportedPage(reader, current);
canvas.AddImage(img);
}
stamper.Close();
reader.Close();
}
I hope this article is helpful to everyone's C # programming.