UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

727 lines (726 loc) 27.3 kB
import { PdfPen, PdfBrush } from './pdf-graphics'; import { PdfFillMode, PathPointType } from './../enumerator'; import { Point, Rectangle } from './../pdf-type'; /** * Implements graphics path, which is a sequence of primitive graphics elements. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a line to the Graphics path * path.addLine({x: 10, y: 250}, {x: 200, y: 250}); * // Draw the path on the PDF page * graphics.drawPath(path, pen); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ export declare class PdfPath { /** * Ordered list of points that compose the path. * * @private */ _points: Array<Point>; /** * Path point type flags for each point in the path. * * @private */ _pathTypes: PathPointType[]; /** * Pen used to stroke the path. * * @private */ _pen: PdfPen; /** * Brush used to fill the path. * * @private */ _brush: PdfBrush; /** * Fill rule applied to the path. * * @private */ _fillMode: PdfFillMode; /** * Indicates whether the next segment starts a new sub path. * * @private */ _isStart: boolean; /** * Indicates whether this path belongs to XPS content. * * @private */ _isXps: boolean; /** * Indicates whether this path represents a rounded rectangle. * * @private */ _isRoundedRectangle: boolean; /** * Initializes a new instance of the `PdfPath` class. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a line to the Graphics path * path.addLine({x: 10, y: 250}, {x: 200, y: 250}); * // Draw the path on the PDF page * graphics.drawPath(path, pen); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ constructor(); /** * Initializes a new instance of the `PdfPath` class using a series of points and path types. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ constructor(points: Array<Point>, pathTypes: PathPointType[]); /** * Gets the last point of the path. * * @returns {Point} The value of the last point. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Get the last point of the path. * let lastPoint: Point = path.lastPoint; * // Draw the path on the PDF page. * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ readonly lastPoint: Point; /** * Gets the array of points that represent the x and y coordinates defining the path. * * @returns {Array<Point>} An array of arrays of numbers, where each inner array represents a set of points. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Get the path points of the path * let pathPoints: Array<Point> = path.pathPoints; * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ readonly pathPoints: Array<Point>; /** * Gets the types of the corresponding points in the path. * * @returns {PathPointType[]} An array of `PathPointType` objects representing the types of each path point. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, b: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Get the path types of the path * let pathTypes: PathPointType[] = path.pathTypes; * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ readonly pathTypes: PathPointType[]; /** * Gets the fill mode. * * @returns {PdfFillMode} The fill mode of the PDF path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Get the fill mode of the path * let fillMode: PdfFillMode = path.fillMode; * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ /** * Sets the fill mode. * * @param {PdfFillMode} mode The fill mode of the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Set the fill mode of the path * path.fillMode = PdfFillMode.alternate; * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * */ fillMode: PdfFillMode; /** * Appends the specified path to this one. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Get the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new PDF path * let path1: PdfPath = new PdfPath(); * // Add path points and path type * let path2: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * path1.addPath(path2); * // Draw the path on the PDF page * graphics.drawPath(path1, pen); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {PdfPath} path The path to append. */ addPath(path: PdfPath): void; /** * Appends the specified path points and their types to this path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new PDF path * let path1: PdfPath = new PdfPath(); * // Add path points and their types * path1.addPath([{x: 50, y: 50}, {x: 100, y: 100}], [PathPointType.start, PathPointType.line]); * // Draw the path on the PDF page * graphics.drawPath(path1, pen); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Array<Point>} pathPoints The path points to append. * @param {PathPointType[]} pathPointTypes The types of the path points. * @returns {void} Nothing */ addPath(pathPoints: Array<Point>, pathPointTypes: PathPointType[]): void; /** * Appends points and point types to this path after validating counts and indices. * * @private * @param {Array<Point>} pathPoints The points to add. * @param {PathPointType[]} pathTypes The types to add (same length). * @returns {void} * @throws {Error} If arrays are null/empty, lengths mismatch, or index invalid. */ _addPath(pathPoints: Array<Point>, pathTypes: PathPointType[]): void; /** * Adds a line segment to the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a line segment to the path * path.addLine({x: 10, y: 250}, {x: 200, y: 250}); * // Draw the path on the PDF page * graphics.drawPath(path, pen); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Point} start The (x,y) coordinates of the starting point of the line. * @param {Point} end The (x,y) coordinates of the ending point of the line. * @returns {void} Nothing */ addLine(start: Point, end: Point): void; /** * Adds a polyline defined by consecutive points. A single point yields a degenerate line. * * @private * @param {Point[]} linePoints The polyline points. * @returns {void} */ _addLines(linePoints: Point[]): void; /** * Adds points from a numeric tuple array, marking the first * point of a figure as `start`, and subsequent points with the given `type`. * * @private * @param {number[]} points Flat array of point coordinates. * @param {PathPointType} type Type for non-first points (e.g., line, bezier). * @param {number} [start=0] Start index in `points`. * @param {number} [end=points.length] End index (exclusive) in `points`. * @returns {void} */ _addPoints(points: number[], type: PathPointType, start?: number, end?: number): void; /** * Appends a single point with the specified path type. * * @private * @param {Point} point The point to add. * @param {PathPointType} type The path type (start/line/bezier). * @returns {void} */ _addPoint(point: Point, type: PathPointType): void; /** * Adds an arc within a bounding rectangle using the angles that define the start and sweep of the arc. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(readFromResources('Empty.pdf')); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a arc to the path * path.addArc({x: 10, y: 10, width: 100, height: 200}, 90, 270); * // Draw the path on the PDF page * graphics.drawPath(path, pen); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Rectangle} bounds The bounding rectangle. * @param {number} startAngle The start angle of the arc. * @param {number} sweepAngle The angle between start angle and the end of the arc. * @returns {void} Nothing */ addArc(bounds: Rectangle, startAngle: number, sweepAngle: number): void; /** * Adds a rectangle to the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a rectangle to the path * path.addRectangle({x: 10, y: 20, width: 50, height: 100}); * // Draw the path on the PDF page * graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Rectangle} bounds The bounding rectangle. * @returns {void} Nothing */ addRectangle(bounds: Rectangle): void; /** * Adds a polygon to the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a polygon to the path * path.addPolygon([{x: 200, y: 10}, {x: 300, y: 100}, {x: 150, y: 100}, {x: 200, y: 10}]); * // Draw the path on the PDF page * graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Array<Point>} points The points of the polygon, where each point representing the x and y coordinates. * @returns {void} Nothing */ addPolygon(points: Array<Point>): void; /** * Adds an ellipse to the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add an ellipse to the path * path.addEllipse({x: 200, y: 200, width: 100, height: 50}); * // Draw the path on the PDF page * graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Rectangle} bounds The bounds of the ellipse. * @returns {void} Nothing */ addEllipse(bounds: Rectangle): void; /** * Adds a Bezier curve to the path using specified coordinates for the start point, two control points, and the end point. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a Bezier curve to the path * path.addBezier({x: 100, y: 100}, {x: 150, y: 150}, {x: 50, y: 250}, {x: 100, y: 300}); * // Draw the path on the PDF page * graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Point} start The (x, y) coordinates of the starting point of the Bezier curve. * @param {Point} first The (x, y) coordinates of the first control point of the Bezier curve. * @param {Point} second The (x, y) coordinates of the second control point of the Bezier curve. * @param {Point} end The (x, y) coordinates of the ending point of the Bezier curve. * @returns {void} Nothing */ addBezier(start: Point, first: Point, second: Point, end: Point): void; /** * Adds a series of cubic Bezier segments from a point tuple collection. * * @private * @param {{number}} pointsCollection An array of `[x, y]` arrays in Bezier quads. * @returns {void} * @throws {Error} If the collection has fewer than 4 entries. */ _addBezierPoints(pointsCollection: number[][]): void; /** * Adds a pie slice to the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Add a pie slice to the path * path.addPie({x: 0, y: 20, width: 100, height: 100}, 270, 45); * // Draw the path on the PDF page * graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {Rectangle} bounds The bounding rectangle. * @param {number} startAngle The angle in degrees measured clockwise from the x-axis to the start of the pie slice. * @param {number} sweepAngle The angle in degrees measured clockwise from the startAngle parameter to the end of the pie slice. * @returns {void} Nothing */ addPie(bounds: Rectangle, startAngle: number, sweepAngle: number): void; /** * Starts a new figure in the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush([{r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath(); * // Start a new figure in the path * path.startFigure(); * // Add some path points (optional) * path.addLine({x: 50, y: 50}, {x: 100, y: 50}); * // Draw the path on the PDF page * graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @returns {void} Nothing */ startFigure(): void; /** * Closes all open figures in the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Close all open figures * path.closeFigure(); * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @returns {void} Nothing */ closeFigure(): void; /** * Closes all non-closed figures in the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Close the figure at index 1 * path.closeFigure(1); * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @param {number} index The optional index of the figure to close. If not provided, the last figure is closed. * @returns {void} Nothing */ closeFigure(index: number): void; /** * Closes all non-closed figures in the path. * * ```typescript * // Load an existing PDF document * let document: PdfDocument = new PdfDocument(data, password); * // Access the first page * let page: PdfPage = document.getPage(0); * // Gets the graphics object of the PDF page * let graphics: PdfGraphics = page.graphics; * // Create a new pen * let pen: PdfPen = new PdfPen({r: 0, g: 0, b: 0}, 1); * // Create a new brush * let brush: PdfBrush = new PdfBrush({r: 0, g: 255, b: 255}); * // Create a new PDF path * let path: PdfPath = new PdfPath([{x: 50, y: 50}, {x: 100, y: 50}, {x: 100, y: 100}, {x: 50, y: 100}, {x: 50, y: 50}], [0, 1, 1, 1, 1]); * // Close all non-closed figures * path.closeAllFigures(); * // Draw the path on the PDF page * page.graphics.drawPath(path, pen, brush); * // Save the document * document.save('output.pdf'); * // Destroy the document * document.destroy(); * ``` * * @returns {void} Nothing */ closeAllFigures(): void; /** * Computes the axis-aligned bounding box of the current path points. * * @private * @returns {number[]} The bounds as `[x, y, width, height]`, or `[0,0,0,0]` if empty. */ _getBounds(): number[]; }