This article describes the method of using iTextSharp to set all PDF page background image functions. Share it for your reference. The details are as follows:
When generating PDFs, you can set the background image in the page.
But when some content is too long to praise the page, it is difficult to set the background image and become a page with a blank background!
The following is the function code for regenerating PDF background image on each page!
public void SetPdfBackground(string pdfFilePath) { //Regenerated PDF path string destFile = (""); //create new pdf document FileStream stream = new FileStream(destFile, , ); PdfReader reader = new PdfReader(pdfFilePath); //read pdf stream PdfStamper stamper = new PdfStamper(reader, stream); string imagePage = ("../images/2012/"); image = (imagePage); var img = (image, ); (0, 0); int totalPage = ; for (int current = 1; current <= totalPage; current++) { var canvas = (current); var page = (reader, current); (img); } (); (); }
The following is to add image background to PDF files. PDF is one of the most popular files in recent years. It is often used in office and daily life. Many times, the background color of PDF files is white, and it is inevitable to feel tired after seeing it too much. Replacing the PDF background can not only make your eyes look more comfortable, but also make PDF files look more beautiful. How to achieve it?
As a programmer, of course, he had to "take the job" himself, and mainly wrote about how to use C# to add image background to PDF files.
First, let’s talk about the specific code below:
Code uses:
first step:Create a Visual C# console project, add references and use namespaces.
using ; using ;
Step 2:Create a PDF document object and load the source PDF file.
PdfDocument doc = new PdfDocument(); ("");
Step 3:Get the first page of the PDF file.
PdfPageBase page = [0];
Step 4:Load the image and set it as the page background.
Image backgroundImage = (""); = backgroundImage;
Step 5:Save the file and reopen it.
(""); ("");
Put all the codes:
using ; using ; namespace Add_image_background_to_PDF { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); (""); PdfPageBase page = [0]; Image backgroundImage = (""); = backgroundImage; (""); (""); } } }
Summarize:
Although PDF is not Microsoft's office software, it is widely used because of its many advantages. PDF itself is less easy to edit than files such as Word and Excel, and requires borrowing other components. In this example, I am using E-iceblue's free PDF component. As of now, the functions I want are basically met and are more convenient.