How to Draw Rectangles When Creating PDF Document with JavaScript

  • Home
  • /
  • Articles
  • /
  • How to Draw Rectangles When Creating PDF Document with JavaScript

This sample shows how to draw rectangles when creating PDF file in JavaScript with BytescoutPDF.js (Bytescout PDF Generator for JavaScript).
Bytescout PDF Generator for JS lets you to draw both simple and filled rectangles.

// function that creates BytescoutPDF instance (defined in BytescoutPDF.js script which have to be included into the same page)
// then calls API methods and properties to create PDF document
// and returns created BytescoutPDF object instance
// this CreatePDF() function is called from Sample.html

function CreatePDF() {

    // create BytescoutPDF object instance
    var pdf = new BytescoutPDF();

    // set document properties: Title, subject, keywords, author name and creator name
    pdf.propertiesSet("Sample document title", "Sample subject", "keyword1, keyword 2, keyword3", "Document Author Name", "Document Creator Name");

	// add page
	pdf.pageAdd();

	// draw simple rectangle 
	pdf.graphicsDrawRectangle(50, 50, 200, 200);
	
	// draw filled rectangle
	pdf.graphicsSetColor(255, 0, 0);
	pdf.graphicsDrawRectangleFill(50, 300, 200, 200);

    // return BytescoutPDF object instance
    return pdf;
}

Generated PDF file with different rectangles (click to view full size):

Click to view full size


prev
next