Today to achieve is to add a picture background to the PDF file feature. PDF is one of the most popular documents in recent years, whether it is in the office or in daily life are often used, many times, the background color of the PDF file is white, see more of the inevitable feel tired, change the background of the PDF can not only make the eyes look more comfortable, but also can make the PDF file look more beautiful. How is it implemented? As a program ape, of course, to personally "fencing", this article I mainly write about how to use C # to add a picture background to the PDF file.
This is the PDF file I prepared:
Code use:
First step: Create a Visual C # console project, add references, and use namespaces.
12 |
using System.Drawing; using Spire.Pdf; |
Step Two: Create a PDF Document object and load the source PDF file.
12 |
PdfDocument doc = new PdfDocument(); doc.LoadFromFile( "sample.pdf" ); |
Step three: get the first page of the PDF file.
1 |
PdfPageBase page = doc.Pages[0]; |
Fourth Step: load the picture and set it to the page background.
12 |
Image backgroundImage = Image.FromFile( "background.jpg" ); page.BackgroundImage = backgroundImage; |
Fifth Step: Save the file and reopen it.
12 |
doc.SaveToFile( "result.pdf" ); System.Diagnostics.Process.Start( "result.pdf" ); |
After adding the picture background (forgive me this picture compare Moe):
Put all the code:
1234567891011121314151617181920 |
using
System.Drawing;
using
Spire.Pdf;
namespace
Add_image_background_to_PDF
{
class
Program
{
static
void
Main(
string
[] args)
{
PdfDocument doc =
new
PdfDocument();
doc.LoadFromFile(
"sample.pdf"
);
PdfPageBase page = doc.Pages[0];
Image backgroundImage = Image.FromFile(
"background.jpg"
);
page.BackgroundImage = backgroundImage;
doc.SaveToFile(
"result.pdf"
);
System.Diagnostics.Process.Start(
"result.pdf"
);
}
}
}
|
Summarize:
PDF is not Microsoft's Office software, but it is widely used because of its many advantages. The PDF itself is more difficult to edit than files such as Word and Excel and needs to borrow other components, and in this example I'm using the free PDF component of E-iceblue Company, and for the time being I want to basically be able to meet the functionality and be more convenient. If you have better suggestions, please feel grateful to share with me.
C # Add a picture background to a PDF