Skip to content

Commit

Permalink
Add PDFPage.drawLine() method (#201)
Browse files Browse the repository at this point in the history
* Added drawLine method to PDFPage (#171)

* Cleanup

* Refactor PDFPage.drawline API

* Update doc comment
  • Loading branch information
Hopding authored Sep 25, 2019
1 parent e883a9e commit 96d03cc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
9 changes: 4 additions & 5 deletions apps/node/tests/test3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ export default async (assets: Assets) => {
font: helveticaFont,
color: hotPink,
});
page2.drawRectangle({
x: 30,
y: 50,
width: lastPageTextWidth,
height: 5,
page2.drawLine({
start: { x: 30, y: 50 },
end: { x: 30 + lastPageTextWidth, y: 50 },
color: hotPink,
thickness: 5,
});

const base64Pdf = await pdfDoc.saveAsBase64();
Expand Down
44 changes: 44 additions & 0 deletions src/api/PDFPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Color, rgb } from 'src/api/colors';
import {
drawEllipse,
drawImage,
drawLine,
drawLinesOfText,
drawRectangle,
} from 'src/api/operations';
Expand All @@ -17,6 +18,7 @@ import {
PDFPageDrawCircleOptions,
PDFPageDrawEllipseOptions,
PDFPageDrawImageOptions,
PDFPageDrawLineOptions,
PDFPageDrawRectangleOptions,
PDFPageDrawSquareOptions,
PDFPageDrawTextOptions,
Expand Down Expand Up @@ -673,6 +675,48 @@ export default class PDFPage {
);
}

/**
* Draw a line on this page. For example:
* ```js
* import { rgb } from 'pdf-lib'
*
* page.drawLine({
* start: { x: 25, y: 75 },
* end: { x: 125, y: 175 },
* thickness: 2,
* color: rgb(0.75, 0.2, 0.2)
* })
* ```
* @param options The options to be used when drawing the line.
*/
drawLine(options: PDFPageDrawLineOptions): void {
assertIs(options.start, 'options.start', [
[Object, '{ x: number, y: number }'],
]);
assertIs(options.end, 'options.end', [
[Object, '{ x: number, y: number }'],
]);
assertIs(options.start.x, 'options.start.x', ['number']);
assertIs(options.start.y, 'options.start.y', ['number']);
assertIs(options.end.x, 'options.end.x', ['number']);
assertIs(options.end.y, 'options.end.y', ['number']);
assertOrUndefined(options.thickness, 'options.thickness', ['number']);
assertOrUndefined(options.color, 'options.color', [[Object, 'Color']]);

const contentStream = this.getContentStream();
if (!('color' in options)) {
options.color = rgb(0, 0, 0);
}
contentStream.push(
...drawLine({
start: options.start,
end: options.end,
thickness: options.thickness || 1,
color: options.color || undefined,
}),
);
}

/**
* Draw a rectangle on this page. For example:
* ```js
Expand Down
7 changes: 7 additions & 0 deletions src/api/PDFPageOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export interface PDFPageDrawRectangleOptions {
borderColor?: Color;
}

export interface PDFPageDrawLineOptions {
start: { x: number; y: number };
end: { x: number; y: number };
thickness?: number;
color?: Color;
}

export interface PDFPageDrawSquareOptions {
x?: number;
y?: number;
Expand Down
16 changes: 16 additions & 0 deletions src/api/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ export const drawImage = (
popGraphicsState(),
];

export const drawLine = (options: {
start: { x: number | PDFNumber; y: number | PDFNumber };
end: { x: number | PDFNumber; y: number | PDFNumber };
thickness: number | PDFNumber;
color: Color | undefined;
}) =>
[
pushGraphicsState(),
options.color && setStrokingColor(options.color),
setLineWidth(options.thickness),
moveTo(options.start.x, options.start.y),
lineTo(options.end.x, options.end.y),
stroke(),
popGraphicsState(),
].filter(Boolean) as PDFOperator[];

export const drawRectangle = (options: {
x: number | PDFNumber;
y: number | PDFNumber;
Expand Down

0 comments on commit 96d03cc

Please sign in to comment.