|
| 1 | +using Syncfusion.Drawing; |
| 2 | +using Syncfusion.Pdf; |
| 3 | +using Syncfusion.Pdf.Graphics; |
| 4 | + |
| 5 | +// Create the new PDF document |
| 6 | +using (PdfDocument document = new PdfDocument()) |
| 7 | +{ |
| 8 | + //Set margin |
| 9 | + document.PageSettings.Margins.All = 0; |
| 10 | + // Add a new page to the document |
| 11 | + PdfPage page = document.Pages.Add(); |
| 12 | + // Get the client size |
| 13 | + SizeF clientSize = page.GetClientSize(); |
| 14 | + // Load the background image from disk |
| 15 | + using FileStream imageStream = new FileStream( |
| 16 | + Path.GetFullPath(@"Data/Image.jpg"), |
| 17 | + FileMode.Open, |
| 18 | + FileAccess.Read |
| 19 | + ); |
| 20 | + PdfBitmap image = new PdfBitmap(imageStream); |
| 21 | + // Save the current graphics state, apply transparency, draw the image to cover the page, then restore. |
| 22 | + PdfGraphicsState state = page.Graphics.Save(); |
| 23 | + page.Graphics.SetTransparency(0.2f); // 20% opacity for the background image |
| 24 | + page.Graphics.DrawImage( |
| 25 | + image, |
| 26 | + new PointF(0, 0), |
| 27 | + new SizeF(clientSize.Width, clientSize.Height) |
| 28 | + ); |
| 29 | + page.Graphics.Restore(state); |
| 30 | + // Define a margin for the text content. |
| 31 | + const float margin = 40f; |
| 32 | + // Text bounds: fill the page within margins. |
| 33 | + RectangleF textBounds = new RectangleF( |
| 34 | + margin, |
| 35 | + margin, |
| 36 | + clientSize.Width - margin * 2, |
| 37 | + clientSize.Height - margin * 2 |
| 38 | + ); |
| 39 | + // Body font for the paragraph. |
| 40 | + PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 16, PdfFontStyle.Regular); |
| 41 | + // Sample paragraph text. |
| 42 | + string paragraphText = |
| 43 | + "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases " + |
| 44 | + "are based, is a large, multinational manufacturing company. The company manufactures and sells " + |
| 45 | + "metal and composite bicycles to North American, European and Asian commercial markets. While " + |
| 46 | + "its base operation is located in Washington with 290 employees, several regional sales teams " + |
| 47 | + "are located throughout their market base."; |
| 48 | + // Create a text element and configure layout to paginate if content exceeds current page. |
| 49 | + PdfTextElement textElement = new PdfTextElement(paragraphText, bodyFont, PdfBrushes.Black); |
| 50 | + PdfLayoutFormat layoutFormat = new PdfLayoutFormat |
| 51 | + { |
| 52 | + Break = PdfLayoutBreakType.FitPage, // Fit within page bounds |
| 53 | + Layout = PdfLayoutType.Paginate // Continue to subsequent pages if needed |
| 54 | + }; |
| 55 | + // Draw the text paragraph in the defined bounds. |
| 56 | + textElement.Draw(page, textBounds, layoutFormat); |
| 57 | + // Save the PDF document to disk. |
| 58 | + document.Save(Path.GetFullPath(@"Output/Output.pdf")); |
| 59 | +} |
0 commit comments