C # Add a picture background to a PDF

Source: Internet
Author: User

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 usingSystem.Drawing;usingSpire.Pdf;

Step Two: Create a PDF Document object and load the source PDF file.

12 PdfDocument doc = newPdfDocument();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 usingSystem.Drawing;usingSpire.Pdf;namespaceAdd_image_background_to_PDF{    classProgram    {        staticvoidMain(string[] args)        {            PdfDocument doc = newPdfDocument();            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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.