### Create PDF Launch Action to Open File Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md This example demonstrates how to add a PDF launch action to a document, which allows users to open a specified file by clicking on an annotation. It involves creating a PDF document, adding a page, defining a launch action with a file name, and adding an action annotation to the page. ```javascript // Create a new PdfDocument object let doc = wasmModule.PdfDocument.Create(); // Add a new page to the document let page = doc.Pages.Add(); // Create a PDF Launch Action that will open a text file let launchAction = spirepdf.PdfLaunchAction.Create({ fileName: inputFileName }); // Create a PDF Action Annotation with the PDF Launch Action let text = "Click here to open file"; let trueTypeFont1 = wasmModule.PdfTrueTypeFont.Create({ fontFile: "ARIALUNI.TTF", size: 13, style: spirepdf.PdfFontStyle.Regular }); let font = trueTypeFont1; let rect = spirepdf.RectangleF.Create({ x: 50, y: 50, width: 230, height: 15 }); page.Canvas.DrawString({ s: text, font: font, brush: spirepdf.PdfBrushes.get_ForestGreen(), layoutRectangle: rect }); let annotation = spirepdf.PdfActionAnnotation.Create(rect, launchAction); // Add the PDF Action Annotation to the page page.Annotations.Add(annotation); ``` -------------------------------- ### Create PDF with Text and Image Layers Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Provides an example of creating a PDF document with both text and image layers using Spire.PDF for JavaScript. It demonstrates adding a page, drawing text with specified formatting and font, measuring text width, loading an image, and drawing the image onto the page. ```javascript // Create a PDF document let doc = wasmModule.PdfDocument.Create(); // Creates a page let page = doc.Pages.Add(); // Create text let text = "Welcome to evaluate Spire.PDF for javascript !"; let format1 = wasmModule.PdfStringFormat.Create({alignment:wasmModule.PdfTextAlignment.Left}); let brush = wasmModule.PdfSolidBrush.Create({color:wasmModule.Color.get_Black()}); let font = wasmModule.PdfTrueTypeFont.Create({fontFile: "/Library/Fonts/ARIALUNI.TTF",size: 15,style: wasmModule.PdfFontStyle.Regular}); let x = 50.0; let y = 50.0; // Draw text layer page.Canvas.DrawString({s:text, font:font, brush:brush,point: wasmModule.PointF.Create({x:x,y:y}), format:format1}); // Get the size of text let size = font.MeasureString({text:"Welcome to evaluate",format: format1}); let size2 = font.MeasureString({text:"Spire.PDF for Python",format: format1}); // Loads an image let image = wasmModule.PdfImage.FromFile(imgFileName); // Draw image layer page.Canvas.DrawImage({image:image,point: wasmModule.PointF.Create({x:x + size.Width,y: y}),size:size2}); // Save the document to the specified path doc.SaveToFile({fileName: outputFileName}); doc.Close(); ``` -------------------------------- ### Convert OFD to PDF Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Provides a code example for converting an OFD (Open Fixed-layout Document) file to a standard PDF format using Spire.PDF for JavaScript. The conversion is handled by the OfdConverter class. ```javascript let converter = spirepdf.OfdConverter.Create({ fileName: inputFileName }); const outputFileName = "OFDToPDF.pdf"; converter.ToPdf({ fileName: outputFileName }); converter.Dispose(); ``` -------------------------------- ### Convert PDF to PDF/A with Attachments Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Provides a JavaScript example using Spire.PDF to convert an existing PDF file to the PDF/A-1b standard. It also demonstrates how to add multiple file attachments to the converted PDF/A document before saving. ```javascript // Convert the input PDF file to PDF/A-1b standard and save it to the memory stream let converter = spirepdf.PdfStandardsConverter.Create({ filePath: inputFileName }); const toA1B_result = "SampleB_2_to_A1B_result.pdf"; converter.ToPdfA1B({ filePath: toA1B_result }); converter.Dispose(); // Create a new PDF document let newDoc = spirepdf.PdfDocument.Create(); // Load the converted PDF document from the memory stream newDoc.LoadFromFile({ fileName: toA1B_result }); // Read the data of the first attachment file into a byte array let data = wasmModule.FS.readFile(inputImageName1) // Create a PdfAttachment object with the attachment file name and data let attach1 = spirepdf.PdfAttachment.Create({ fileName: "attachment1.png", data: data }); // Read the data of the second attachment file into a byte array let data2 = wasmModule.FS.readFile(inputFileName2) // Create a PdfAttachment object with the attachment file name and data let attach2 = spirepdf.PdfAttachment.Create({ fileName: "attachment2.pdf", data: data2 }); // Add the attachments to the new PDF document newDoc.Attachments.Add({ attachment: attach1 }); newDoc.Attachments.Add({ attachment: attach2 }); // Save the document to the specified path newDoc.SaveToFile({ fileName: outputFileName }); newDoc.Close(); ``` -------------------------------- ### Create Bulleted and Ordered Lists in PDF Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md This snippet shows how to create a PDF document and add different types of lists, including bulleted lists, standard ordered lists, and lists with custom numbering styles (Roman and Latin). It details the setup of page margins, fonts, brushes, and the drawing process for each list element. ```javascript let doc = wasmModule.PdfDocument.Create(); let unitCvtr = wasmModule.PdfUnitConvertor.Create(); let margin = wasmModule.PdfMargins.Create(); margin.Top = unitCvtr.ConvertUnits(2.54, wasmModule.PdfGraphicsUnit.Centimeter, wasmModule.PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17, wasmModule.PdfGraphicsUnit.Centimeter, wasmModule.PdfGraphicsUnit.Point); margin.Right = margin.Left; let page = doc.Pages.Add({ size: wasmModule.PdfPageSize.A4(), margins: margin }); let y = 10; let brush1 = wasmModule.PdfBrushes.get_Black(); let font1 = wasmModule.PdfTrueTypeFont.Create({ fontFile: "/Library/Fonts/ARIALUNI.TTF", size: 16, style: wasmModule.PdfFontStyle.Bold }); let format1 = wasmModule.PdfStringFormat.Create({ alignment: wasmModule.PdfTextAlignment.Center }); page.Canvas.DrawString({ s: "Categories List", font: font1, brush: brush1, x: page.Canvas.ClientSize.Width / 2, y: y, format: format1 }); y = y + font1.MeasureString({ text: "Categories List", format: format1 }).Height; y = y + 5; let location = wasmModule.PointF.Create({ x: 0, y: 0 }); let rctg = wasmModule.RectangleF.Create({ location: location, size: page.Canvas.ClientSize }); let pdfrgbColor1 = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Navy() }); let pdfrgbColor2 = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_OrangeRed() }); let brush = wasmModule.PdfLinearGradientBrush.Create({ rect: rctg, color1: pdfrgbColor1, color2: pdfrgbColor2, mode: wasmModule.PdfLinearGradientMode.Vertical }); let font = wasmModule.PdfFont.Create({ fontFamily: wasmModule.PdfFontFamily.Helvetica, size: 12, style: wasmModule.PdfFontStyle.Bold }); let formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood"; let list = wasmModule.PdfList.Create({ text: formatted }); list.Font = font; list.Indent = 8; list.TextIndent = 5; list.Brush = brush; let layoutWidget1 = new wasmModule.PdfLayoutWidget(list.H); let result = layoutWidget1.Draw({ page: page, x: 0, y: y }); y = result.Bounds.Bottom; let sortedList = wasmModule.PdfSortedList.Create({ text: formatted }); sortedList.Font = font; sortedList.Indent = 8; sortedList.TextIndent = 5; sortedList.Brush = brush; let layoutWidget2 = new wasmModule.PdfLayoutWidget(sortedList.H); let result2 = layoutWidget2.Draw({ page: page, x: 0, y: y }); y = result2.Bounds.Bottom; let font2 = wasmModule.PdfFont.Create({ fontFamily: wasmModule.PdfFontFamily.Helvetica, size: 12 }); let marker1 = wasmModule.PdfOrderedMarker.Create({ style: wasmModule.PdfNumberStyle.LowerRoman, font: font2 }); let list2 = wasmModule.PdfSortedList.Create({ text: formatted }); list2.Font = font; list2.Marker = marker1; list2.Indent = 8; list2.TextIndent = 5; list2.Brush = brush; let layoutWidget3 = new wasmModule.PdfLayoutWidget(list2.H); let result3 = layoutWidget3.Draw({ page: page, x: 0, y: y }); y = result3.Bounds.Bottom; let font3 = wasmModule.PdfFont.Create({ fontFamily: wasmModule.PdfFontFamily.Helvetica, size: 12 }); let marker2 = wasmModule.PdfOrderedMarker.Create({ style: wasmModule.PdfNumberStyle.LowerLatin, font: font2 }); let list3 = wasmModule.PdfSortedList.Create({ text: formatted }); list3.Font = font; list3.Marker = marker2; list3.Indent = 8; list3.TextIndent = 5; list3.Brush = brush; let layoutWidget4 = new wasmModule.PdfLayoutWidget(list3.H); layoutWidget4.Draw({ page: page, x: 0, y: y }); ``` -------------------------------- ### Create Line Annotations in PDF Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Provides examples of creating multiple line annotations in a PDF document with varying styles, colors, and properties. This includes setting border styles, line intents, beginning and ending line styles, flags, and leader line properties for each annotation. ```javascript let doc = wasmModule.PdfDocument.Create(); let page = doc.Pages.Add(); let linePoints = [100, 650, 180, 650]; let lineAnnotation = wasmModule.PdfLineAnnotation.Create({ linePoints: linePoints, text: "This is the first line annotation" }); lineAnnotation.lineBorder.BorderStyle = wasmModule.PdfBorderStyle.Solid; lineAnnotation.lineBorder.BorderWidth = 1; lineAnnotation.LineIntent = wasmModule.PdfLineIntent.LineDimension; lineAnnotation.BeginLineStyle = wasmModule.PdfLineEndingStyle.Butt; lineAnnotation.EndLineStyle = wasmModule.PdfLineEndingStyle.Diamond; lineAnnotation.Flags = wasmModule.PdfAnnotationFlags.Default; lineAnnotation.InnerLineColor = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Green() }); lineAnnotation.BackColor = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Green() }); lineAnnotation.LeaderLineExt = 0; lineAnnotation.LeaderLine = 0; page.Annotations.Add(lineAnnotation); linePoints = [100, 550, 280, 550]; lineAnnotation = wasmModule.PdfLineAnnotation.Create({ linePoints: linePoints, text: "This is the second line annotation" }); lineAnnotation.lineBorder.BorderStyle = wasmModule.PdfBorderStyle.Underline; lineAnnotation.lineBorder.BorderWidth = 2; lineAnnotation.LineIntent = wasmModule.PdfLineIntent.LineArrow; lineAnnotation.BeginLineStyle = wasmModule.PdfLineEndingStyle.Circle; lineAnnotation.EndLineStyle = wasmModule.PdfLineEndingStyle.Diamond; lineAnnotation.Flags = wasmModule.PdfAnnotationFlags.Default; lineAnnotation.InnerLineColor = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Pink() }); lineAnnotation.BackColor = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Pink() }); lineAnnotation.LeaderLineExt = 0; lineAnnotation.LeaderLine = 0; page.Annotations.Add(lineAnnotation); linePoints = [100, 450, 280, 450]; lineAnnotation = wasmModule.PdfLineAnnotation.Create({ linePoints: linePoints, text: "This is the third line annotation" }); lineAnnotation.lineBorder.BorderStyle = wasmModule.PdfBorderStyle.Beveled; lineAnnotation.lineBorder.BorderWidth = 2; lineAnnotation.LineIntent = wasmModule.PdfLineIntent.LineDimension; lineAnnotation.BeginLineStyle = wasmModule.PdfLineEndingStyle.None; lineAnnotation.EndLineStyle = wasmModule.PdfLineEndingStyle.None; lineAnnotation.Flags = wasmModule.PdfAnnotationFlags.Default; lineAnnotation.InnerLineColor = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Blue() }); lineAnnotation.BackColor = wasmModule.PdfRGBColor.Create({ color: wasmModule.Color.get_Blue() }); lineAnnotation.LeaderLineExt = 1; lineAnnotation.LeaderLine = 1; page.Annotations.Add(lineAnnotation); ``` -------------------------------- ### Compress PDF Document with Image Options Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Shows how to compress a PDF document using Spire.PDF for JavaScript, focusing on image compression settings. This example sets options to resize images and adjust image quality to 'Low' before saving the compressed document. ```javascript // Create a new instance of PdfCompressor with the specified input PDF file path let compressor = wasmModule.PdfCompressor.Create({filePath: inputFileName}); // Set compression options for the compressor compressor.Options.ImageCompressionOptions.ResizeImages = true; compressor.Options.ImageCompressionOptions.ImageQuality = wasmModule.ImageQuality.Low; // Define the output file name const outputFileName = "CompressDocumentSecondApproach_result.pdf"; // Save the document to the specified path compressor.CompressToFile(outputFileName); ``` -------------------------------- ### Get Page Count of PDF Document Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md A simple JavaScript example using Spire.PDF to retrieve and display the total number of pages in a PDF document. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile(inputFileName); //Get the page count let count = (doc.Pages.Count).toString(); doc.Close(); ``` -------------------------------- ### Manage PDF Page Labels Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Shows how to create a PDF document, load it from a file, initialize page labels, and add a range of pages with a specific numbering style (Decimal Arabic Numerals) and a prefix. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile(inputFileName); doc.PageLabels = wasmModule.PdfPageLabels.Create(); doc.PageLabels.AddRange({ indexOfFirstPage: 0, numberStyle: wasmModule.PdfPageLabels.Decimal_Arabic_Numerals_Style, prefix: "label test" }); doc.SaveToFile({fileName: outputFileName}); doc.Close(); ``` -------------------------------- ### Create PDF with Hello World Text Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md This snippet shows how to create a new PDF document, add a page, define text, font, and brush, and then draw the 'Hello World' string onto the PDF canvas. ```javascript let doc = wasmModule.PdfDocument.Create(); let pagebase = doc.Pages.Add(); const text = "Hello World"; let pdffont = wasmModule.PdfFont.Create(wasmModule.PdfFontFamily.Helvetica, 30); let pdfBrush = wasmModule.PdfSolidBrush.Create({color:wasmModule.Color.get_Blue()}); pagebase.Canvas.DrawString({s: text, font: pdffont, brush: pdfBrush, x: 10, y: 10}); ``` -------------------------------- ### Get PDF Attachment Information Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Provides a JavaScript example to load a PDF document, access its attachments, retrieve the first attachment, and extract its properties such as filename, description, creation date, and modification date. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({ fileName: inputFileName }); let collection = doc.Attachments; let attachment = collection.get_Item(0); let embeddedFileSpecification = new wasmModule.PdfEmbeddedFileSpecification(attachment.H); let content = ""; content = content + "Filename: " + embeddedFileSpecification.FileName + "\r\n"; content = content + "Description: " + embeddedFileSpecification.Description + "\r\n"; content = content + "Creation Date: " + embeddedFileSpecification.CreationDate.ToString() + "\r\n"; content = content + "Modification Date: " + embeddedFileSpecification.ModificationDate.ToString() + "\r\n"; doc.Close(); ``` -------------------------------- ### Get PDF Zoom Factor Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Retrieves the zoom factor from a PDF document's action destination and displays it as a percentage. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({fileName: inputFileName}); let action = wasmModule.PdfGoToAction.Create({destination:doc.AfterOpenAction}) let zoomvalue = action.Destination.Zoom let result = "The zoom factor of the document is "+ (zoomvalue*100).toFixed(2).toString() +"%." ; ``` -------------------------------- ### Create PDF Document and Add Links Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md This snippet demonstrates creating a new PDF document, setting margins, adding a page, and then adding various types of links including text links, web links, and URI annotations with JavaScript actions. It utilizes the Spire.PDF for JavaScript library to manage PDF elements and their properties. ```javascript let doc = spirepdf.PdfDocument.Create(); let unitCvtr = spirepdf.PdfUnitConvertor.Create(); let margin = spirepdf.PdfMargins.Create(); margin.Top = unitCvtr.ConvertUnits(2.54, spirepdf.PdfGraphicsUnit.Centimeter, spirepdf.PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17, spirepdf.PdfGraphicsUnit.Centimeter, spirepdf.PdfGraphicsUnit.Point); margin.Right = margin.Left; let page = doc.Pages.Add({ size: spirepdf.PdfPageSize.A4(), margins: margin }); let y = 100; let x = 10; // Simple Text Link let trueTypeFont1 = wasmModule.PdfTrueTypeFont.Create({ fontFile: "Lucida Sans Unicode.ttf", size: 14, style: spirepdf.PdfFontStyle.Regular }); let font = trueTypeFont1; let label = "Simple Text Link: "; let format = spirepdf.PdfStringFormat.Create(); format.MeasureTrailingSpaces = true; page.Canvas.DrawString({ s: label, font: font, brush: spirepdf.PdfBrushes.get_Orange(), x: 0, y: y, format: format }); x = font.MeasureString({ text: label, format: format }).Width; let trueTypeFont2 = wasmModule.PdfTrueTypeFont.Create({ fontFile: "Lucida Sans Unicode.ttf", size: 14, style: spirepdf.PdfFontStyle.Underline }); let font1 = trueTypeFont2; let url1 = "http://www.e-iceblue.com"; page.Canvas.DrawString({ s: url1, font: font1, brush: spirepdf.PdfBrushes.get_CadetBlue(), x: x, y: y }); y = y + font1.MeasureString({ text: url1 }).Height + 25; // Web Link label = "Web Link: "; page.Canvas.DrawString({ s: label, font: font, brush: spirepdf.PdfBrushes.get_Orange(), x: 0, y: y, format: format }); x = font.MeasureString({ text: label, format: format }).Width; let text = "E-iceblue home"; let link2 = spirepdf.PdfTextWebLink.Create(); link2.Text = text; link2.Url = url1; link2.Font = font1; link2.Brush = spirepdf.PdfBrushes.get_CadetBlue(); link2.DrawTextWebLink({ graphics: page.Canvas, location: spirepdf.PointF.Create({ x: x, y: y }) }); y = y + font1.MeasureString({ text: text }).Height + 30; // URI Annotation label = "URI Annotation: "; page.Canvas.DrawString({ s: label, font: font, brush: spirepdf.PdfBrushes.get_Orange(), x: 0, y: y, format: format }); x = font.MeasureString({ text: label, format: format }).Width; text = "Google"; let location = spirepdf.PointF.Create({ x: x, y: y }); let size = font1.MeasureString({ text: text }); let linkBounds = spirepdf.RectangleF.Create({ location: location, size: size }); let link3 = spirepdf.PdfUriAnnotation.Create({ rectangle: linkBounds }); link3.Border = spirepdf.PdfAnnotationBorder.Create({ borderWidth: 0 }); link3.Uri = "http://www.google.com"; page.Annotations.Add(link3); page.Canvas.DrawString({ s: text, font: font1, brush: spirepdf.PdfBrushes.get_CadetBlue(), x: x, y: y }); y = y + size.Height + 30; // URI Annotation with Action label = "URI Annotation Action: "; page.Canvas.DrawString({ s: label, font: font, brush: spirepdf.PdfBrushes.get_Orange(), x: 0, y: y, format: format }); x = font.MeasureString({ text: label, format: format }).Width; text = "JavaScript Action (Click Me)"; location = spirepdf.PointF.Create({ x: x - 2, y: y - 2 }); size = font1.MeasureString({ text: text }); size = spirepdf.SizeF.Create({ width: size.Width + 5, height: size.Height + 5 }); linkBounds = spirepdf.RectangleF.Create({ location: location, size: size }); let link4 = spirepdf.PdfUriAnnotation.Create({ rectangle: linkBounds }); link4.Border = spirepdf.PdfAnnotationBorder.Create({ borderWidth: 0.75 }); link4.Color = spirepdf.PdfRGBColor.Create({ color: spirepdf.Color.get_CadetBlue() }); let script = "app.alert({" + "cMsg: \"Hello.\"," + "nIcon: 3," + "cTitle: \"JavaScript Action\"" + "});"; let action = spirepdf.PdfJavaScriptAction.Create(script); link4.Action = action; page.Annotations.Add(link4); page.Canvas.DrawString({ s: text, font: font1, brush: spirepdf.PdfBrushes.get_CadetBlue(), x: x, y: y }); ``` -------------------------------- ### Get All Bookmarks from PDF Document Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Provides a function to retrieve all bookmarks from a PDF document and their associated information, including titles and text styles. It recursively processes child bookmarks. ```javascript function GetBookmarks(bookmarks) { let content = ""; if (bookmarks.Count > 0) { content = "Pdf bookmarks:" + "\r\n"; for (let i = 0; i < bookmarks.Count; i++) { let parentBookmark = bookmarks.get_Item(i); content += parentBookmark.Title + "\r\n"; let textStyle = parentBookmark.DisplayStyle.toString(); content += textStyle + "\r\n"; let childContent = GetChildBookmark(parentBookmark); content += childContent; } } return content; } function GetChildBookmark(parentBookmark) { let childContent = ""; if (parentBookmark.Count > 0) { for (let i = 0; i < parentBookmark.Count; i++) { let childBookmark = parentBookmark.get_Item(i); childContent += childBookmark.Title + "\r\n"; let textStyle = childBookmark.DisplayStyle.toString(); childContent += textStyle + "\r\n"; childContent += GetChildBookmark(childBookmark); } } return childContent; } let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({ fileName: inputFileName }); let bookmarks = doc.Bookmarks; let bookmarkInfo = GetBookmarks(bookmarks); ``` -------------------------------- ### Configure PDF Page Settings Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Illustrates configuring various page settings in a PDF document, including margins, size, orientation, and rotation. It demonstrates creating pages with different background colors, custom margins, and applying landscape orientation and 180-degree rotation. ```javascript let doc = wasmModule.PdfDocument.Create(); let unitCvtr = wasmModule.PdfUnitConvertor.Create(); let margin = wasmModule.PdfMargins.Create(); margin.Top = unitCvtr.ConvertUnits(2.54, wasmModule.PdfGraphicsUnit.Centimeter, wasmModule.PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17, wasmModule.PdfGraphicsUnit.Centimeter, wasmModule.PdfGraphicsUnit.Point); margin.Right = margin.Left; let page = doc.Pages.Add({size: wasmModule.PdfPageSize.A4(), margins: margin}); page.BackgroundColor = wasmModule.Color.get_Chocolate(); page = doc.Pages.Add({size: wasmModule.PdfPageSize.A4(), margins: margin}); page.BackgroundColor = wasmModule.Color.get_Coral(); page = doc.Pages.Add(wasmModule.PdfPageSize.A3(), margin, wasmModule.PdfPageRotateAngle.RotateAngle180, wasmModule.PdfPageOrientation.Landscape); page.BackgroundColor = wasmModule.Color.get_LightPink(); let section = doc.Sections.Add(); page = section.Pages.Add(); section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_LightSkyBlue(); section = doc.Sections.Add(); section.PageSettings.Orientation = wasmModule.PdfPageOrientation.Landscape; page = section.Pages.Add(); section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; section = doc.Sections.Add(); page = section.Pages.Add(); section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; section.PageSettings.Rotate = wasmModule.PdfPageRotateAngle.RotateAngle90; section = doc.Sections.Add(); page = section.Pages.Add(); section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; section.PageSettings.Rotate = wasmModule.PdfPageRotateAngle.RotateAngle180; ``` -------------------------------- ### Get PDF Page Information Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Retrieves detailed information about a PDF page, including dimensions (MediaBox, BleedBox, CropBox, ArtBox, TrimBox, ActualSize) and rotation angle. The dimensions are provided in points. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile(inputFileName); let page = doc.Pages.get_Item(0) //Get the size of page MediaBox based on "point" let MediaBoxWidth = page.MediaBox.Width let MediaBoxHeight = page.MediaBox.Height let MediaBoxX = page.MediaBox.X let MediaBoxY = page.MediaBox.Y //Get the size of page BleedBox based on "point" let BleedBoxWidth = page.BleedBox.Width let BleedBoxHeight = page.BleedBox.Height let BleedBoxX = page.BleedBox.X let BleedBoxY = page.BleedBox.Y //Get the size of page CropBox based on "point" let CropBoxWidth = page.CropBox.Width let CropBoxHeight = page.CropBox.Height let CropBoxX = page.CropBox.X let CropBoxY = page.CropBox.Y //Get the size of page ArtBox based on "point" let ArtBoxWidth = page.ArtBox.Width let ArtBoxHeight = page.ArtBox.Height let ArtBoxX = page.ArtBox.X let ArtBoxY = page.ArtBox.Y //Get the size of page TrimBox based on "point" let TrimBoxWidth = page.TrimBox.Width let TrimBoxHeight = page.TrimBox.Height let TrimBoxX = page.TrimBox.X let TrimBoxY = page.TrimBox.Y //Get the actual size of page let actualSizeW = page.ActualSize.Width let actualSizeH = page.ActualSize.Height //Gets the rotation angle of the current page let rotationAngle = page.Rotation let rotation = rotationAngle.toString(); //Create content to save let content = ""; //Add page information string to content content += "MediaBox width: " + MediaBoxWidth.toString() + "pt, height: " + MediaBoxHeight.toString() + "pt, RectangleF X: " + MediaBoxX.toString() + "pt, RectangleF Y: " + MediaBoxY.toString() + "pt." + "\n"; content += "BleedBox width: " + BleedBoxWidth.toString() + "pt, height: " + BleedBoxHeight.toString() + "pt, RectangleF X: " + BleedBoxX.toString() + "pt, RectangleF Y: " + BleedBoxY.toString() + "pt." + "\n"; content += "CropBox width: " + CropBoxWidth.toString() + "pt, height: " + CropBoxHeight.toString() + "pt, RectangleF X: " + CropBoxX.toString() + "pt, RectangleF Y: " + CropBoxY.toString() + "pt." + "\n"; content += "ArtBox width: " + ArtBoxWidth.toString() + "pt, height: " + ArtBoxHeight.toString() + "pt, RectangleF X: " + ArtBoxX.toString() + "pt, RectangleF Y: " + ArtBoxY.toString() + "pt." + "\n"; content += "TrimBox width: " + TrimBoxWidth.toString() + "pt, height: " + TrimBoxHeight.toString() + "pt, RectangleF X: " + TrimBoxX.toString() + "pt, RectangleF Y: " + TrimBoxY.toString() + "pt." + "\n"; content += "The actual size of the current page width: " + actualSizeW.toString() + "\n"; content += "The actual size of the current page height: " + actualSizeH.toString() + "\n"; content += "The rotation angle of the current page: " + rotation; doc.Close(); ``` -------------------------------- ### Convert Text to PDF Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Illustrates how to create a PDF document from plain text content. It covers setting up fonts, text formatting, and drawing the text onto a PDF page. ```javascript let doc = wasmModule.PdfDocument.Create(); let section = doc.Sections.Add(); let page = section.Pages.Add(); let font = wasmModule.PdfFont.Create({fontFamily: wasmModule.PdfFontFamily.Helvetica, size: 11}); let format = wasmModule.PdfStringFormat.Create(); format.LineSpacing = 20; let brush = wasmModule.PdfBrushes.get_Black(); let textLayout = wasmModule.PdfTextLayout.Create(); textLayout.Break = wasmModule.PdfLayoutBreakType.FitPage; textLayout.Layout = wasmModule.PdfLayoutType.Paginate; let bounds = wasmModule.RectangleF.Create({ location: wasmModule.PointF.Create({x: 10, y: 20}), size: page.Canvas.ClientSize }); let textWidget = wasmModule.PdfTextWidget.Create({text: text, font: font, brush: brush}); textWidget.StringFormat = format; let layoutWidget = new wasmModule.PdfLayoutWidget(textWidget.H); layoutWidget.Draw({page: page, layoutRectangle: bounds, format: textLayout}); ``` -------------------------------- ### Retrieve PDF Page Size in Multiple Units Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Gets the size of a PDF page in various units: points, pixels, inches, and centimeters. It utilizes a PdfUnitConvertor to perform the conversions from the default 'point' unit. ```javascript // Assuming a PDF document is already loaded let page = doc.Pages.get_Item(0); // Get the width of page based on "point" let pointWidth = page.Size.Width; // Get the height of page let pointHeight = page.Size.Height; // Create PdfUnitConvertor to convert the unit let unitCvtr = wasmModule.PdfUnitConvertor.Create(); // Convert the size with "pixel" let pixelWidth = unitCvtr.ConvertUnits( pointWidth, wasmModule.PdfGraphicsUnit.Point, wasmModule.PdfGraphicsUnit.Pixel); let pixelHeight = unitCvtr.ConvertUnits( pointHeight, wasmModule.PdfGraphicsUnit.Point, wasmModule.PdfGraphicsUnit.Pixel); // Convert the size with "inch" let inchWidth = unitCvtr.ConvertUnits( pointWidth, wasmModule.PdfGraphicsUnit.Point, wasmModule.PdfGraphicsUnit.Inch); let inchHeight = unitCvtr.ConvertUnits( pointHeight, wasmModule.PdfGraphicsUnit.Point, wasmModule.PdfGraphicsUnit.Inch); // Convert the size with "centimeter" let centimeterWidth = unitCvtr.ConvertUnits( pointWidth, wasmModule.PdfGraphicsUnit.Point, wasmModule.PdfGraphicsUnit.Centimeter); let centimeterHeight = unitCvtr.ConvertUnits( pointHeight, wasmModule.PdfGraphicsUnit.Point, wasmModule.PdfGraphicsUnit.Centimeter); ``` -------------------------------- ### Get Child Bookmarks from PDF Document Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Extracts child bookmarks from a PDF document, including their titles, colors, and text styles (Bold, Italic, Regular). The extracted information is written to a specified output file. ```javascript function GetChildBookmarks(bookmarks, result) { let content = ""; for (let i = 0; i < bookmarks.Count; i++) { let parentBookmark = bookmarks.get_Item(i); if (parentBookmark.Count > 0) { content = content + "Child Bookmarks:" + "\r\n"; for (let j = 0; j < parentBookmark.Count; j++) { let childBookmark = parentBookmark.get_Item(j); content = content + childBookmark.Title + "\r\n"; let color = childBookmark.Color.R + "," + childBookmark.Color.G + "," + childBookmark.Color.B; content = content + "color: "+color + "\r\n"; let textStyle = ""; if (childBookmark.DisplayStyle == spirepdf.PdfTextStyle.Bold) { textStyle = "Bold" + "\r\n"; } else if (childBookmark.DisplayStyle == spirepdf.PdfTextStyle.Italic) { textStyle = "Italic" + "\r\n"; } if (childBookmark.DisplayStyle == spirepdf.PdfTextStyle.Regular) { textStyle = "Regular" + "\r\n"; } content = content + textStyle; } } } wasmModule.FS.writeFile(result, content.toString()); } let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({ fileName: inputFileName }); let bookmarks = doc.Bookmarks; GetChildBookmarks(bookmarks, outputFileName); doc.Close(); ``` -------------------------------- ### Configure PDF Document Template with Headers and Footers Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Sets up a document template for a PDF, configuring page size, margins, and drawing header and footer elements. It includes drawing lines, text labels, and page number fields. ```javascript function SetDocumentTemplate(doc, pageSize, margin) { // Create a template element for the left space on the page let leftSpace = wasmModule.PdfPageTemplateElement.Create({width: margin.Left, height: pageSize.Height}); doc.Template.Left = leftSpace; let topSpace = wasmModule.PdfPageTemplateElement.Create({width: pageSize.Width, height: margin.Top}); topSpace.Foreground = true; doc.Template.Top = topSpace; // Draw header label let inputArialFont = "/Library/Fonts/ARIALUNI.TTF"; let font = wasmModule.PdfTrueTypeFont.Create({ fontFile: inputArialFont, size: 9, style: wasmModule.PdfFontStyle.Italic }); let format = wasmModule.PdfStringFormat.Create({alignment: wasmModule.PdfTextAlignment.Right}); let label = "Demo of Spire.Pdf"; let size = font.MeasureString({text: label, format: format}); let y = topSpace.Height - font.Height - 1; let pdfrgbColor = wasmModule.PdfRGBColor.Create({color: wasmModule.Color.get_Black()}); let pen = wasmModule.PdfPen.Create({pdfRgbColor: pdfrgbColor, width: 0.75}); topSpace.Graphics.SetTransparency({alpha: 0.5}); topSpace.Graphics.DrawLine({pen: pen, x1: margin.Left, y1: y, x2: pageSize.Width - margin.Right, y2: y}); y = y - 1 - size.Height; topSpace.Graphics.DrawString({ s: label, font: font, brush: wasmModule.PdfBrushes.get_Black(), x: pageSize.Width - margin.Right, y: y, format: format }); // Create a template element for the right space on the page let rightSpace = wasmModule.PdfPageTemplateElement.Create({width: margin.Right, height: pageSize.Height}); doc.Template.Right = rightSpace; let bottomSpace = wasmModule.PdfPageTemplateElement.Create({width: pageSize.Width, height: margin.Bottom}); // Create a template element for the bottom space on the page bottomSpace.Foreground = true; doc.Template.Bottom = bottomSpace; // Draw footer label y = font.Height + 1; bottomSpace.Graphics.SetTransparency({alpha: 0.5}); bottomSpace.Graphics.DrawLine({pen: pen, x1: margin.Left, y1: y, x2: pageSize.Width - margin.Right, y2: y}); // Draw a footer label y = y + 1; let pageNumber = wasmModule.PdfPageNumberField.Create(); let pageCount = wasmModule.PdfPageCountField.Create(); let pageNumberLabel = wasmModule.PdfCompositeField.Create(); pageNumberLabel.AutomaticFields = [pageNumber, pageCount]; pageNumberLabel.Brush = wasmModule.PdfBrushes.get_Black(); pageNumberLabel.Font = font; pageNumberLabel.StringFormat = format; pageNumberLabel.Text = "page {0} of {1}"; pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y); } ``` -------------------------------- ### Get PDF Bookmark Page Number Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Retrieves the page number for a specific bookmark within a PDF document. It accesses the bookmark's destination page and finds its index in the document's pages collection. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({ fileName: inputFileName }); let bookmarks = doc.Bookmarks; let bookmark = bookmarks.get_Item(0); let pageNumber = doc.Pages.IndexOf(bookmark.Destination.Page) + 1; ``` -------------------------------- ### Create PDF Annotation to Launch File Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md This snippet shows how to create a PDF annotation that launches a file in a new window when clicked. It iterates through pages and text, creating a launch action and adding it as an annotation. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({ fileName: inputFileName }); let collection = null; let test = ["Spire.PDF"]; // Iterate through each page of the PDF document for (let pageIndex = 0; pageIndex < doc.Pages.Count; pageIndex++) { let page = doc.Pages.get_Item(pageIndex); // Iterate through each keyword in the 'test' array for (let i = 0; i < test.length; i++) { // Create a PdfTextFinder to search for the keyword in the current page let finder = spirepdf.PdfTextFinder.Create(page); finder.Options.Parameter = spirepdf.TextFindParameter.WholeWord; // Find all occurrences of the keyword on the current page collection = finder.Find(test[i]); // Iterate through each found text fragment for (let m = 0; m < collection.length; m++) { let find = collection[m]; // Create a PdfLaunchAction to launch a file when the annotation is clicked let launchAction = spirepdf.PdfLaunchAction.Create({ fileName: inputFileName, path: spirepdf.PdfFilePathType.Relative }); // Set the launch action to open the file in a new window launchAction.IsNewWindow = true; // Get the position and size of the found text fragment let rect = spirepdf.RectangleF.Create({ x: find.Positions[0].X, y: find.Positions[0].Y, width: find.Sizes[0].Width, height: find.Sizes[0].Height }); // Create a PdfActionAnnotation with the launch action and the annotation rectangle let annotation = spirepdf.PdfActionAnnotation.Create(rect, launchAction); // Add the annotation to the current page page.Annotations.Add(annotation); } } } ``` -------------------------------- ### Get Individual PDF Attachment Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Demonstrates how to load a PDF document, access its attachments collection, retrieve a specific attachment by index, and save it to a file. It also shows how to extract file name and data from the attachment. ```javascript let doc = wasmModule.PdfDocument.Create(); doc.LoadFromFile({ fileName: inputFileName }); let collection = doc.Attachments; let attachment = collection.get_Item(1); let embeddedFileSpecification = new wasmModule.PdfEmbeddedFileSpecification(attachment.H); const outputFileName = embeddedFileSpecification.FileName; wasmModule.FS.writeFile(outputFileName, embeddedFileSpecification.Data); doc.Close(); ``` -------------------------------- ### Configure PDF Page Transitions with Spire.PDF Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Demonstrates how to set various page transition styles (Fly, Box, Split) for different sections of a PDF document. It configures transition duration, style, dimension, and motion. ```javascript // Create a pdf document let doc = wasmModule.PdfDocument.Create(); doc.ViewerPreferences.PageMode = wasmModule.PdfPageMode.FullScreen; //Set the margin let unitCvtr = wasmModule.PdfUnitConvertor.Create(); let margin = wasmModule.PdfMargins.Create(); margin.Top = unitCvtr.ConvertUnits(2.54, wasmModule.PdfGraphicsUnit.Centimeter, wasmModule.PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17, wasmModule.PdfGraphicsUnit.Centimeter, wasmModule.PdfGraphicsUnit.Point); margin.Right = margin.Left; //Create one section let section = doc.Sections.Add(); // Configure the transition settings for the section's pages section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; section.PageSettings.Transition.Duration = 2.0; section.PageSettings.Transition.Style = wasmModule.PdfTransitionStyle.Fly; section.PageSettings.Transition.PageDuration = 1.0; // Add pages to the section let page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Red(); page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Green(); page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Blue(); // Create a new section with different transition settings section = doc.Sections.Add(); section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; // Configure the transition settings for this section's pages section.PageSettings.Transition.Duration = 2.0; section.PageSettings.Transition.Style = wasmModule.PdfTransitionStyle.Box; section.PageSettings.Transition.PageDuration = 1.0; // Add pages to the section page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Orange(); page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Brown(); page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Navy(); // Create another section with different transition settings section = doc.Sections.Add(); section.PageSettings.Size = wasmModule.PdfPageSize.A4(); section.PageSettings.Margins = margin; // Configure the transition settings for this section's pages section.PageSettings.Transition.Duration = 2.0; section.PageSettings.Transition.Style = wasmModule.PdfTransitionStyle.Split; section.PageSettings.Transition.Dimension = wasmModule.PdfTransitionDimension.Vertical; section.PageSettings.Transition.Motion = wasmModule.PdfTransitionMotion.Inward; section.PageSettings.Transition.PageDuration = 1.0; // Add pages to the section page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Orange(); page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Brown(); page = section.Pages.Add(); page.BackgroundColor = wasmModule.Color.get_Navy(); ``` -------------------------------- ### Get All Attachments from PDF Document Source: https://github.com/eiceblue/spire.pdf-for-javascript/blob/main/JavaScript Examples/pdf_vue.md Illustrates how to retrieve all attachments from a PDF document using Spire.PDF for JavaScript. The code loads a PDF, accesses the attachments collection, and iterates through each attachment to save its data to files in a specified output directory. ```javascript // Create a PDF document let doc = wasmModule.PdfDocument.Create(); // Load the PDF file doc.LoadFromFile({ fileName: inputFileName }); // Get a collection of attachments on the PDF document let collection = doc.Attachments; // Save all the attachments to the files for (let i = 0; i < collection.Count; i++) { let attachment = collection.get_Item(i); let embeddedFileSpecification = new wasmModule.PdfEmbeddedFileSpecification(attachment.H); wasmModule.FS.writeFile(outputDirectoryName+embeddedFileSpecification.FileName, embeddedFileSpecification.Data); } // Clean up resources doc.Close(); ```