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
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to create a questionnaire using PdfTextElement in WinForms.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!