在 PDF 文档中创建多行文本 Xamarin Forms Syncfusion

Create multiline text in PDF document Xamarin Forms Syncfusion

我在使用官方 Syncfusion Xamarin Forms PDF 文档时遇到了一些问题。我设法创建了一个 table,将其绑定到值并将其附加到 PdfDocument。

然而奇怪的是,我似乎无法在不同的行上绘制文本字符串。我尝试使用 graphics.DrawString() 或 TextElement 绘制的每个文本都在文档的第一行中彼此叠加绘制。

所有字符串都是 AppResources,相互连接形成来自 resx 文件和代表我的数据的 class 对象的报告。其中许多需要呈现为单独的短语和段落。

是否有关于如何逐行追加文本并在它们之间添加 tables 的工作示例?

谢谢!

使用 Essential PDF 可以一个接一个地绘制文本。我们可以通过使用 PdfTextElement 和 PdfLayoutResult 或在 PdfGraphics.DrawString 方法中指定其位置来一个接一个地绘制文本。请参考下面的代码片段和示例以获取更多详细信息。

        //Creates a new PDF document.
        PdfDocument doc = new PdfDocument();

        //Adds a page.
        PdfPage page = doc.Pages.Add();

        //create a new PDF string format
        PdfStringFormat drawFormat = new PdfStringFormat();
        drawFormat.WordWrap = PdfWordWrapType.Word;
        drawFormat.Alignment = PdfTextAlignment.Justify;
        drawFormat.LineAlignment = PdfVerticalAlignment.Top;

        //Set the font.
        PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f);

        //Create a brush.
        PdfBrush brush = PdfBrushes.Red;

        //bounds
        RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height - 20));

        //Create a new text elememt
        PdfTextElement element = new PdfTextElement(text, font, brush);

        //Set the string format
        element.StringFormat = drawFormat;

        //Draw the text element
        PdfLayoutResult result =  element.Draw(page, bounds);

        // Draw the string one after another.
        result = element.Draw(result.Page, new RectangleF(result.Bounds.X, result.Bounds.Bottom + 10, result.Bounds.Width, result.Bounds.Height));

        // Creates a PdfLightTable.
        PdfLightTable pdfLightTable = new PdfLightTable();

        //Add colums to light table
        pdfLightTable.Columns.Add(new PdfColumn("Name"));
        pdfLightTable.Columns.Add(new PdfColumn("Age"));
        pdfLightTable.Columns.Add(new PdfColumn("Sex"));

        //Add row        
        pdfLightTable.Rows.Add(new string[] { "abc", "21", "Male" });

        //Includes the style to display the header of the light table.
        pdfLightTable.Style.ShowHeader = true;

        //Draws PdfLightTable and returns the rendered bounds.
        result = pdfLightTable.Draw(page, new PointF(result.Bounds.Left,result.Bounds.Bottom+20));

        //draw string with returned bounds from table
        result =  element.Draw(result.Page, result.Bounds.X, result.Bounds.Bottom + 10);

        //draw string with returned bounds from table
        element.Draw(result.Page, result.Bounds.X, result.Bounds.Bottom + 10);

        MemoryStream stream = new MemoryStream();

        //Saves the document.
        doc.Save(stream);

        doc.Close(true);

示例 link:http://www.syncfusion.com/downloads/support/directtrac/171058/ze/App2938608856

如果您需要任何进一步的帮助,请告诉我们。