- Home
- Forum
- ASP.NET Core - EJ 2
- Create PDF ignoringmargins for backgound Image
Create PDF ignoringmargins for backgound Image
Hello,
I am creating an PDF with Syncfusion.Pdf.
For all the text and table contents I added margins to the pages, this works grate.
But now I need to add a backgroundimage over the whole page, the image I want to add has the right size to fit the pages.
page.Graphics.DrawImage(image, new PointF(0, 0), new SizeF(page.Size.Width, page.Size.Height));
put the image in the correct size to the page, but in the margin area it is cropped.
So, how can I ignore margins for the image, but still use them for text generation?
And the next question is how to add the image to every page of the pdf?
I am working with the Flow Layout and LayoutResult to dynamically put the contents to the document. So I do not know how many pages the document will have until all the contents are added.
Best regards,
Alex
Hi Alexander Kuhlmann,
We do not have built-in support for omitting margins and drawing a background image. However, this can be achieved as a workaround by drawing the page as a template. We have attached a sample to demonstrate this approach and request you to try it on your end. Please let us know if you need any further assistance.
Code:
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page to the document
PdfPage page = document.Pages.Add();
PdfGrid grid = new PdfGrid();
//Create a DataTable
//Add values to the list.
List<object> data = new List<object>();
for (int i = 0; i < 100; i++)
{
data.Add(new { ID = "E01", Name = "Clay" });
data.Add(new { ID = "E02", Name = "Thomas" });
data.Add(new { ID = "E03", Name = "George" });
data.Add(new { ID = "E04", Name = "Steffen" });
data.Add(new { ID = "E05", Name = "Mathew" });
}
grid.DataSource = data;
//Draw grid to the page
grid.Draw(page, new PointF(0, 0));
//Save the document
MemoryStream stream = new MemoryStream();
document.Save(stream);
document.Close(true);
//Load the created PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
//Get the first page of the document
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
//Get the page size
SizeF pageSize = loadedPage.Size;
//Get the background image
FileStream imageStream = new FileStream(@"../../../reports.jpg", FileMode.Open, FileAccess.Read);
//Create the PdfBitmap object from the background image file
PdfBitmap image = new PdfBitmap(imageStream);
//Create a PdfTemplate based on the image size.
PdfTemplate imageTemplate = new PdfTemplate(pageSize);
// Draw the image onto the template
imageTemplate.Graphics.DrawImage(image, new PointF(0, 0), pageSize);
//Create a new PDF document
PdfDocument doc = new PdfDocument();
//Iterate all the pages from the PDF document
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
//Get the page from the PDF document
PdfLoadedPage loadedPage1 = loadedDocument.Pages[i] as PdfLoadedPage;
//Create a template from the loaded document
PdfTemplate template = loadedPage1.CreateTemplate();
//Create a new section
PdfSection section = doc.Sections.Add();
//Set page size.
section.PageSettings.Size = loadedPage1.Size;
//set margins to 0
section.PageSettings.Margins.All = 0;
//Create a new page
PdfPage pdfPage = section.Pages.Add();
//Draw the background template.
pdfPage.Graphics.DrawPdfTemplate(imageTemplate, PointF.Empty, pageSize);
//Draw the loaded template into new document
pdfPage.Graphics.DrawPdfTemplate(template, PointF.Empty, pageSize);
}
//Save the document
MemoryStream stream1 = new MemoryStream();
doc.Save(stream1);
doc.Close(true);
File.WriteAllBytes("Output.pdf", stream1.ToArray());
Regards,
Jeyalakshmi T
Attachment: PdfSample_a070f58a.zip
I have the same requirement and the workaround mentioned worked for me in the past. But since on of the last updates, the font is completely messed up when drawing the pdf templates on the new pdf page. This happens when using custom fonts (PdfTrueTypeFont), but not with PdfStandardFont. Is this a known issue?
Hi Alexander,
We are trying to replicate the problem on our end using our test documents and we are not able to reproduce it. We suspect that the issue is document-specific. Therefore, we request you to share the input PDF document with us so that we can replicate the problem on our end. It will be more helpful for us to analyze further and provide you with a prompt solution.
Please rest assured that Syncfusion maintains strict confidentiality regarding all information disclosed by our customers. We have a Non-Disclosure Agreement (NDA) in place, ensuring that any documents shared will be used solely for replicating and resolving the issue. These documents will not be shared with any third party.
Regards,
Irfana J
- 3 Replies
- 4 Participants
-
AK Alexander Kuhlmann
- Mar 30, 2025 11:17 AM UTC
- May 12, 2025 02:41 PM UTC