|
| 1 | +using Syncfusion.Pdf.Graphics; |
| 2 | +using Syncfusion.Pdf.Interactive; |
| 3 | +using Syncfusion.Pdf.Parsing; |
| 4 | +using Syncfusion.Pdf; |
| 5 | +using Syncfusion.Drawing; |
| 6 | +using System.Reflection.Metadata; |
| 7 | + |
| 8 | +//Create FileStream object to read the input PDF file |
| 9 | +using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) |
| 10 | +{ |
| 11 | + // Load the existing PDF file |
| 12 | + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream); |
| 13 | + |
| 14 | + // Get the first page of the PDF |
| 15 | + PdfLoadedPage lpage = loadedDocument.Pages[0] as PdfLoadedPage; |
| 16 | + |
| 17 | + // Create a PDF attachment |
| 18 | + PdfAttachment attachment = new PdfAttachment("Attachment.pdf", File.ReadAllBytes(Path.GetFullPath(@"Data/Attachment.pdf"))); |
| 19 | + attachment.Description = "PDF Attachment"; |
| 20 | + |
| 21 | + // Create attachments section if it doesn't exist |
| 22 | + if (loadedDocument.Attachments == null) |
| 23 | + loadedDocument.CreateAttachment(); |
| 24 | + |
| 25 | + // Add the attachment to the document |
| 26 | + loadedDocument.Attachments.Add(attachment); |
| 27 | + |
| 28 | + // Create a button field on the page |
| 29 | + PdfButtonField buttonField = new PdfButtonField(lpage, "Button"); |
| 30 | + buttonField.Bounds = new RectangleF(100, 100, 100, 20); |
| 31 | + buttonField.BorderColor = new PdfColor(Color.Black); |
| 32 | + buttonField.BackColor = new PdfColor(Color.LightGray); |
| 33 | + buttonField.Text = "Click Me"; |
| 34 | + buttonField.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 12); |
| 35 | + |
| 36 | + // Add JavaScript action to open the attachment |
| 37 | + buttonField.Actions.MouseUp = new PdfJavaScriptAction("this.exportDataObject({ cName: \"Attachment.pdf\", nLaunch: 2 });"); |
| 38 | + |
| 39 | + // Create a form if it doesn't exist |
| 40 | + if (loadedDocument.Form == null) |
| 41 | + loadedDocument.CreateForm(); |
| 42 | + |
| 43 | + // Add the button field to the form |
| 44 | + loadedDocument.Form.Fields.Add(buttonField); |
| 45 | + |
| 46 | + // Set default appearance for form fields |
| 47 | + loadedDocument.Form.SetDefaultAppearance(false); |
| 48 | + |
| 49 | + //Create file stream. |
| 50 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) |
| 51 | + { |
| 52 | + //Save the PDF document to file stream. |
| 53 | + loadedDocument.Save(outputFileStream); |
| 54 | + } |
| 55 | + |
| 56 | + //Close the document. |
| 57 | + loadedDocument.Close(true); |
| 58 | +} |
0 commit comments