We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create a questionnaire using PdfTextElement ?

How to create a questionnaire using PdfTextElement?

The questionnaire can be created using PdfTextElement. Please find the code snippet and sample for the same.

C#:

//Create a PDF document

PdfDocument doc = new PdfDocument();

//Add a page

PdfPage newPage = doc.Pages.Add();

//Draw the question

PdfLayoutResult layoutResult = AddQuestion(" Question 1 ", newPage, 0);

//Draw the answer

layoutResult = AddText("Answer 1", layoutResult.Page, layoutResult.Bounds.Bottom);

//Draw the question

layoutResult = AddQuestion("Question 2", layoutResult.Page, layoutResult.Bounds.Bottom);

//Draw the answer

layoutResult = AddText("Answer 2", layoutResult.Page, layoutResult.Bounds.Bottom);

//Draw the question

layoutResult = AddQuestion("Question 3", layoutResult.Page, layoutResult.Bounds.Bottom);

//Read the texts from text file.

StreamReader reader = new StreamReader(@"..\..\Data\Answer3.txt", System.Text.Encoding.ASCII);

//Draw the answer

layoutResult = AddText(reader.ReadToEnd(), layoutResult.Page, layoutResult.Bounds.Bottom);

//Save the document.

doc.Save(@"Output.pdf");

//Close the document.

doc.Close(true);

 

private PdfLayoutResult AddQuestion(string text, PdfPage page, float bottom)

{

PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);

PdfBrush brush = PdfBrushes.Blue;

float prevBottom = bottom + font.Height;

//Rendering text

PdfTextElement textElement = new PdfTextElement( text, font, brush);

// Formatting layout

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();

format.Layout = PdfLayoutType.Paginate;

format.Break = PdfLayoutBreakType.FitPage;

//Drawing string

PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, prevBottom, page.GetClientSize().Width, 0), format);

return result;

}

 

private PdfLayoutResult AddText(string text, PdfPage page, float bottom)

{

PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);

PdfBrush brush = PdfBrushes.Black;

float prevBottom = bottom + font.Height;

//Rendering text

PdfTextElement textElement = new PdfTextElement( text, font, brush);

// Formatting layout

PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();

format.Layout = PdfLayoutType.Paginate;

format.Break = PdfLayoutBreakType.FitPage;

//Drawing string

PdfLayoutResult result = textElement.Draw(page, new RectangleF(0, prevBottom, page.GetClientSize().Width, 0), format);

return result;

}

 

Sample link:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfSample163904622

 

 

Article ID: Published Date: Last Revised Date: Platform: Control:
6177 01/28/2016 01/28/2016 WinForms PDF
Did you find this information helpful?
Add Comment
You must log in to leave a comment

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon