我需要生成一個 PDF,其中包含 xamarin.forms 中包含英語和阿拉伯語資料的表格。我一直在尋找很多。我找到了 syncfusion 檔案并嘗試應用它。這個不包括網格,但它告訴如何添加一個簡單的阿拉伯語文本:https : //blog.syncfusion.com/blogs/post/adding-rtl-support-to-your-pdf-in-xamarin.aspx這個有效。所以我嘗試使用它并進行修改以添加網格。這是我在 main.xaml.cs 中寫的:
private void btn_Clicked(object sender, EventArgs e)
{
PdfDocument doc = new PdfDocument();
//Add a page.
PdfPage page = doc.Pages.Add();
//Create a PdfGrid.
Syncfusion.Pdf.Grid.PdfGrid pdfGrid = new Syncfusion.Pdf.Grid.PdfGrid();
//Add values to list
List<object> data = new List<object>();
Object row1 = new { ID = "E01", Name = "???" };
Object row2 = new { ID = "E02", Name = "????" };
Object row3 = new { ID = "E03", Name = "Andrew" };
Object row4 = new { ID = "E04", Name = "Paul" };
Object row5 = new { ID = "E05", Name = "Gray" };
data.Add(row1);
data.Add(row2);
data.Add(row3);
data.Add(row4);
data.Add(row5);
//Add list to IEnumerable
IEnumerable<object> dataTable = data;
//Assign data source.
pdfGrid.DataSource = dataTable;
//Draw grid to the page of PDF document.
pdfGrid.Draw(page, new PointF(10, 10));
//Save the PDF document to stream.
MemoryStream ms = new MemoryStream();
//Create a new PDF document.
//Add a new PDF page.
//Load font.
Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("RTLDemo.Assets.arial.ttf");
//Create PDF true type font.
PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 12);
//String format
PdfStringFormat format = new PdfStringFormat();
//Set the format as right to left.
format.TextDirection = PdfTextDirection.RightToLeft;
//Set the alignment.
format.Alignment = PdfTextAlignment.Right;
SizeF pageSize = page.GetClientSize();
PdfGridCell pdfGridCell = new PdfGridCell();
PdfGridCellStyle style = new PdfGridCellStyle();
style.Font = pdfFont;
style.StringFormat.TextDirection = format.TextDirection;
//Set style to grid
pdfGridCell.Style = style;
//Save the document.
doc.Save(ms);
//Close the document
doc.Close(true);
ms.Position = 0;
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("RTLText.pdf", "application/pdf", ms);
else
Xamarin.Forms.DependencyService.Get<ISave>().Save("RTLText.pdf", "application/pdf", ms);
}
我不斷收到一個例外:System.NullReferenceException:** 'Object reference not set to an instance of an object.'上style.StringFormat.TextDirection = format.TextDirection;線。我按照本檔案中的步驟操作:https : //www.syncfusion.com/forums/135954/how-to-create-unicode-font-with-pdftruetypefont-or-the-proper-way-to-draw-a- unicode-string我嘗試了很多東西,但沒有任何效果。我該怎么辦?
更新:
我用新代碼替換了上面的代碼:
PdfDocument doc = new PdfDocument();
//Add a page.
PdfPage page = doc.Pages.Add();
//Create a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
//Create a DataTable.
DataTable dataTable = new DataTable();
//Add columns to the DataTable
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Name");
//Add rows to the DataTable.
dataTable.Rows.Add(new object[] { "E01", "???" });
dataTable.Rows.Add(new object[] { "E02", "Thomas" });
//Assign data source.
pdfGrid.DataSource = dataTable;
//Using the Column collection
pdfGrid.Columns[0].Width = 100;
//Adding grid cell style
PdfGridCellStyle cellStyle = new PdfGridCellStyle();
//Create new PDF string format instance.
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.TextDirection = PdfTextDirection.RightToLeft;
//Set string format to grid cell.
cellStyle.StringFormat = format;
//Set borders.
PdfBorders borders = new PdfBorders();
borders.All = PdfPens.Red;
cellStyle.Borders = borders;
//Set background image.
Stream fontStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("RTLDemo.Assets.arial.ttf");
PdfFont pdfFont = new PdfTrueTypeFont(fontStream, 12);
cellStyle.Font = pdfFont;
//Set cell paddings.
cellStyle.CellPadding = new PdfPaddings(5, 5, 5, 5);
//Applying style to grid
pdfGrid.Rows[0].Cells[0].Style = cellStyle;
//Draw grid to the page of PDF document.
pdfGrid.Draw(page, new PointF(10, 10));
MemoryStream ms = new MemoryStream();
//Save the document.
doc.Save(ms);
//Close the document
doc.Close(true);
ms.Position = 0;
if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
Xamarin.Forms.DependencyService.Get<ISaveWindowsPhone>().Save("RTLText.pdf", "application/pdf", ms);
else
Xamarin.Forms.DependencyService.Get<ISave>().Save("RTLText.pdf", "application/pdf", ms);
之前的例外沒有出現,生成了pdf,但沒有出現阿拉伯語文本,我錯過了什么?
uj5u.com熱心網友回復:
這是直接取自檔案
//Adding grid cell style
PdfGridCellStyle cellStyle = new PdfGridCellStyle();
//Create new PDF string format instance.
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
//Set string format to grid cell.
cellStyle.StringFormat = format;
uj5u.com熱心網友回復:
我們可以使用 Xamarin.Forms 應用程式中的 Syncfusion PDF 庫在 PDF 網格中繪制 RTL 文本。請在下面找到示例代碼,
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a new PDF page.
PdfPage page = document.Pages.Add();
//Get the font file as stream.
Stream fontStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("UnicodeText.Assets.arial.ttf");
//Create a new PdfTrueTypeFont instance.
PdfTrueTypeFont font = new PdfTrueTypeFont(fontStream, 14);
//Create a new bold stylePdfTrueTypeFont instance.
PdfTrueTypeFont boldFont = new PdfTrueTypeFont(fontStream, 14, PdfFontStyle.Bold);
page.Graphics.DrawString("PdfGrid", boldFont, PdfBrushes.Black, PointF.Empty);
//Create PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
//Add values to list
List<object> data = new List<object>();
data.Add(new { ID = "E01", Name = "???" });
data.Add(new { ID = "E02", Name = "????" });
data.Add(new { ID = "E03", Name = "Andrew" });
data.Add(new { ID = "E04", Name = "Paul" });
data.Add(new { ID = "E05", Name = "Clay" });
//Add list to IEnumerable.
IEnumerable<object> dataTable = data;
//Assign data source.
pdfGrid.DataSource = dataTable;
//Assign bold font to pdfGrid header.
pdfGrid.Headers[0].Style.Font = boldFont;
//Assign font to PdfGrid.
pdfGrid.Style.Font = font;
//Create String format with RTL text direction and center text alignment.
PdfStringFormat format = new PdfStringFormat();
format.TextDirection = PdfTextDirection.RightToLeft;
format.Alignment = PdfTextAlignment.Center;
//Assign string format to draw RTL text with center alsignment
pdfGrid.Rows[0].Cells[1].StringFormat = format;
pdfGrid.Rows[1].Cells[1].StringFormat = format;
//Draw grid to the page of PDF document.
pdfGrid.Draw(page, new Syncfusion.Drawing.PointF(0, 20));
MemoryStream stream = new MemoryStream();
//Save the document.
document.Save(stream);
//Close the document.
document.Close(true);
請從https://www.syncfusion.com/downloads/support/directtrac/general/ze/UnicodeText1046384115 中找到示例。請找到從https://www.syncfusion.com/downloads/support/directtrac/general/ze/Output-609045962創建的示例輸出 PDF 。
注意:示例中指定的字串格式不適用于具有阿拉伯文本的相應 PDF 網格單元格。我們必須根據提供的示例代碼提供單元格索引為 1。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/327216.html
標籤:pdf xamarin.forms 数据网格 同步融合
