)
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: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create a questionnaire using PdfTextElement ?

Platform: WinForms |
Control: PDF |
Published Date: January 28, 2016 |
Last Revised Date: January 24, 2020

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

 

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

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

Live Chat Icon For mobile
Live Chat Icon