How to create a questionnaire using PdfTextElement in WinForms?
The questionnaire can be created using WinForms PDF TextElement. 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:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfSample163904622
Conclusion
I hope you enjoyed learning about to create a questionnaire using PdfTextElement.
You can refer to our Winforms PDF’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms PDF documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms PDF and otherWinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!