[Tutorial] Spire.PDF Tutorial: How to add background color and background image tiling to PDF

Spire.PDF is a professional PDF component, can independently create, write, edit, manipulate and read PDF files, support for .NET, Java, WPF, and Silverlight. Spire.PDFof PDF API is rich in features, such as security settings (including digital signatures), PDF text / Accessories / extract images, PDF files, merge / split, metadata updates, sections and paragraphs optimization, graphic / image rendering and insertion , forms creation and processing, data import, and so on.

                                                [ Download the latest trial version Spire.PDF ]

C # PDF document to add a background color and background image

Spire.PDF component not only supports the PDF document to all pages or specified pages to add background colors and background images, and also supports to set the transparency of the background and context of the specified area. Here we will describe how to use Spire.PDF to existing PDF documents add background colors and background images.

Add a background color

//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("示例文档.pdf");

//遍历文档所有页面
foreach (PdfPageBase page in pdf.Pages)
{
    //设置页面背景颜色
    page.BackgroundColor = Color.LightSkyBlue;
}

//保存文档
pdf.SaveToFile("背景色.pdf");复制代码

The effect is as follows:

Add a background image

//加载PDF文档
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("示例文档.pdf");

//遍历文档所有页面
foreach (PdfPageBase page in pdf.Pages)
{
    //设置页面背景图片
    page.BackgroundImage = Image.FromFile("a.jpg");   

    //设置背景区域
    //page.BackgroundRegion = new RectangleF(200, 200, 200, 200);

    //设置背景透明度
    //page.BackgroudOpacity = 50; 
}

//保存文档
pdf.SaveToFile("背景图.pdf");复制代码

The effect is as follows:

C # Add tiled background image to PDF

Background allows PDF documents look better, or to use as a watermark in PDF. Background image can be a big picture, the image may be composed of a small tile map. Next you will learn how to tile images in PDF, and create a tiled background to PDF.

//创建PdfDocument对象
PdfDocument pdf = new PdfDocument();

//加载文档
pdf.LoadFromFile(@"F:\Documents\PDF\系统测试.pdf");

//加载图片
PdfImage image = PdfImage.FromFile("logo.png");

//遍历文档每一页
foreach (PdfPageBase page in pdf.Pages)
{
    //创建PdfTilingBrush对象,指定大小
    PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5));

    //设置画刷透明度
    brush.Graphics.SetTransparency(0.2f);

    //在画刷上绘制图片
    brush.Graphics.DrawImage(image,new PointF((brush.Size.Width-image.Width)/2,(brush.Size.Height-image.Height)/2));

    //使用画刷填充页面大小的矩形
    page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));
}

//保存文档
pdf.SaveToFile("output.pdf");复制代码

The effect is as follows:

If you have any questions or comments, you can leave a message in the comments area below, click on the resource list to see more tutorials resources ~


Guess you like

Origin blog.csdn.net/weixin_33980459/article/details/91395287