C# PDF Background Editor Library
C# PDF Library: how to add, remove, update PDF page background using c#
C# Demo Code to add, remove, update PDF page background using c#
In the following C# tutorial, you will learn how to add, remove, extract PDF file background text, image content in your C# ASP.NET Web application and Windows application.
- Add text, image, color settings to PDF file background
- Easy to apply background pages range, and background opacity
- Read, extract PDF document background information
- Remove background from PDF file
How to add, remove PDF background programmatically using C#
PDF background is displayed behind text and images on the PDF pages. The background can be a solid color rendered on the whole page, or you
can use an image, or you can use the content from an existing PDF file.
You can also apply the background on specific pages or page rangs on a PDF file. There is only one background per PDF page. However you can
apply the different backgrounds on each page.
PDF C# library includes:
c# pdf page count,
how to add header and footer in pdf using in c# with example,
create thumbnail from pdf c#,
c# rotate pdf document,
c# scale pdf page,
add pages to pdf c#.
Add background to PDF file using C#
Using C# PDF background editor library, you have three methods to create, add background to PDF file.
- Fill a solid color on page background
- Add image to the PDF background
- Load, and use content from an existing pdf file
Apply a solid color background on PDF pages using C#
The following C# source code explains how to use a solid color as PDF pages background.
String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_backgd.pdf";
// open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);
PDFBackgroudRes resBackground = new PDFBackgroundFillColorRes(Color.Yellow);
{
// define a page background setting
PDFPageBackground background1 = new PDFPageBackground(resBackground);
background1.Opacity = 0.2F;
// define page range: all odd pages
PageRangeOptions pageRange1 = new PageRangeOptions();
pageRange1.AllPages = true;
pageRange1.Subset = PageRangeSubset.Odd;
// apply page background settings to all odd pages
PDFPageFieldHandler.ApplyBackground(doc, background1, pageRange1);
}
{
// define a page background setting
PDFPageBackground background2 = new PDFPageBackground(resBackground);
background2.Opacity = 0.2F;
// define page range: all even pages
PageRangeOptions pageRange2 = new PageRangeOptions();
pageRange2.AllPages = true;
pageRange2.Subset = PageRangeSubset.Even;
// apply page background settings to all even pages
PDFPageFieldHandler.ApplyBackground(doc, background2, pageRange2);
}
doc.Save(outputFilePath);
Use an image as PDF pages' background in C#
The following C# source code shows how to use an image as PDF background image.
String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\1_backgd.b.pdf";
String srcImageFilePath = @"C:\SourceImage.png";
// open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);
// Create a background resource by an image file.
PDFBackgroudRes resBackground = new PDFBackgroundImageRes(new Bitmap(srcImageFilePath));
// define a page background setting
PDFPageBackground background = new PDFPageBackground(resBackground);
// Set transparency
background.Opacity = 0.2F;
// Alignment the background to center.
background.HoriAlignment = BackgroundHoriAlignment.Center;
background.VertAlignment = BackgroundVertAlignemnt.Center;
// Indicate if use image's acutal size or not.
// If set to false, it would fill the image to the target page.
// Default: false
background.EnableAbsoluteScale = true;
// define page range: to all pages
PageRangeOptions pageRange = new PageRangeOptions();
pageRange.AllPages = true;
pageRange.Subset = PageRangeSubset.All;
// apply page background settings to all pages
PDFPageFieldHandler.ApplyBackground(doc, background, pageRange);
doc.Save(outputFilePath);
Add PDF background from another PDF file content using C#
The following C# source code shows how to add PDF background from another PDF file content using C#.
If you need add text content on PDF background, this is the only method.
String inputFilePath = @"C:\1.pdf";
String outputFilePath = @"C:\1_backgd.c.pdf";
PDFBackgroudRes resBackground = null;
// Create a PDF file with text and use this PDF file as the background resource.
PDFContext ctx = new PDFContext(new SizeF(3, 1));
RasterEdge.Imaging.Drawing.RESolidBrush brush = new RasterEdge.Imaging.Drawing.RESolidBrush(Color.Red);
ctx.DrawString(brush, 10, 50, REString.Create("This is background text.", new Font("Arial", 36F, FontStyle.Regular)));
using (MemoryStream ms = new MemoryStream())
{
ctx.RenderToStream(ms);
resBackground = new PDFBackgroundFileRes(ms, 0);
}
// open a PDF file
PDFDocument doc = new PDFDocument(inputFilePath);
// define a page background setting
PDFPageBackground background = new PDFPageBackground(resBackground);
// Set no transparency
background.Opacity = 1.0F;
// Alignment the background to top-left.
background.HoriAlignment = BackgroundHoriAlignment.Left;
background.VertAlignment = BackgroundVertAlignemnt.Top;
// Indicate if use image's acutal size or not.
// If set to false, it would fill the image to the target page.
// Default: false
background.EnableAbsoluteScale = true;
// define page range: all odd pages
PageRangeOptions pageRange = new PageRangeOptions();
pageRange.AllPages = true;
pageRange.Subset = PageRangeSubset.Odd;
// apply page background settings to all odd pages
PDFPageFieldHandler.ApplyBackground(doc, background, pageRange);
doc.Save(outputFilePath);
Remove all Page Background settings in a PDF document object
String inputFilePath = Program.RootPath + "\\" + "1_backgd.pdf";
String outputFilePath = Program.RootPath + "\\" + "output.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
PDFPageFieldHandler.RemoveBackgrounds(doc);
doc.Save(outputFilePath);
Read, extract PDF background data using C#
String inputFilePath = Program.RootPath + "\\" + "1_backgd.pdf";
PDFDocument doc = new PDFDocument(inputFilePath);
// get all Background settings in the document
PDFPageBackgroundInfo info = PDFPageFieldHandler.RetreiveBackgrounds(doc);
// get default setting for Background and Page Range
PDFPageBackground defaultSetting = info.GetDefaultSetting();
PageRangeOptions defaultPageRange = info.GetDefaultPageRangeSettings();