Create PDF with text, shapes, and images

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Creation >

Create PDF with text, shapes, and images

In this section we present example code to create a new PDF document containing text, graphic shapes and images. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

 

Given below is the sample code.

 

// Create a new PDF document
using (var pdfDoc = new PDF())
{
   // Set the measurement unit to inches
   pdfDoc.MeasurementUnit = MeasurementUnit.Inches;
      
   // Write some text with default font (Standard Type 1, Helvetica)
   pdfDoc.WriteText("Hello, Helvetica", x: 1, y: 1);
 
   // Write some text using Standard Type 1 Courier font and different coloring
   var fontCourier = new PDFFont(StdType1Font.Courier, PDFFontStyle.Stroke_And_Fill, 32);
   pdfDoc.Brush.Color = Color.Yellow;
   pdfDoc.Pen.Color = Color.Red;
   pdfDoc.Pen.Width = 1;
   pdfDoc.WriteText("Hello, Courier", x: 1, y: 2, wrap: false, fontCourier);
 
   // Write some text using the Arial font and make sure the font is fully embedded in the PDF
   var fontArial = new PDFFont(new Font("Arial", 32, FontStyle.Regular), FontEmbedType.Full);
   pdfDoc.WriteText("Hello, Arial", x: 1, y: 3, wrap: false, fontArial);
 
   // Write some text which is rotated by 45 degrees
   pdfDoc.WriteText("Hello, Arial rotated", new SimpleRect(x: 1, y: 3, width: 100, height: 100), fontArial, rotationAngle: 45);

 
   // Render an image
   pdfDoc.DrawImage("sample_image.bmp", x: 0, y: 2);
 
   // Draw an ellipse shape
   pdfDoc.DrawEllipse(x: 1, y: 0.5f, width: 3, height: 1, isFill: false, isStroke: true);
 
   // Save the document to file
   pdfDoc.Save("sample_doc.pdf");
 
   // Optional, since the using statement will automatically close the document
   pdfDoc.CloseDocument();
}