英文:
generate and share a pdf from sqlite datatable of inventory count using skiasharp .net maui
问题
Here is the translated content from your provided code:
我想要设计一个使用SkiaSharp创建专业外观的PDF报告,显示物品数量,但是我没有找到合适的代码示例。
我想要使用SQLite表格来生成按物品类别分组的报告,包括类别总计。
代码在以下部分出现错误:
pdfDocument.ToArray())
> SKDocument不包含“ToArray”的定义
这是我尝试的代码。
```csharp
public async Task<SKDocument> GenerateInventoryItemCountPdfAsync(DateTime date, List<Item> items, string pdfLogoPath, string pdfTitle)
{
var stream = new MemoryStream();
using var document = SKDocument.CreatePdf(stream);
var pageSize = new SKSize(595, 842);
using var canvas = document.BeginPage((int)pageSize.Width, (int)pageSize.Height);
using (var logoBitmap = SKBitmap.Decode(pdfLogoPath))
{
//var logoRect = SKRect.Create(pageSize.Width / 2 - logoBitmap.Width / 2, 50, logoBitmap.Width, logoBitmap.Height);
//canvas.DrawBitmap(logoBitmap, logoRect);
var titlePaint = new SKPaint
{
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
TextSize = 24,
IsAntialias = true,
Color = SKColors.Black
};
var titleBounds = new SKRect();
titlePaint.MeasureText(pdfTitle, ref titleBounds);
canvas.DrawText(pdfTitle, pageSize.Width / 2 - titleBounds.Width / 2, 150, titlePaint);
var countPaint = new SKPaint
{
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
TextSize = 18,
IsAntialias = true,
Color = SKColors.Black
};
var y = 200;
foreach (var item in items)
{
var itemCount = await GetInventoryItemCountForDateAsync(date, item.Id);//.ConfigureAwait(false);
string condition;
if (itemCount != null)
{
condition = itemCount.InventoryItemUnitQty.ToString();
}
else
{
condition = "N/A";
}
var countText = $"{item.ItemFullName}:{condition}";
var countBounds = new SKRect();
countPaint.MeasureText(countText, ref countBounds);
canvas.DrawText(countText, pageSize.Width / 2 - countBounds.Width / 2, y, countPaint);
y += 30;
}
document.EndPage();
document.Close();
return document;
}
}
public List<InventoryActivityReport> GetAllInventoryActivityReports()
{
InitAsync();
return conn.Table<InventoryActivityReport>().ToList();
}
public List<InventoryCount> GetAllInventoryCounts()
{
InitAsync();
return conn.Table<InventoryCount>().ToList();
}
public InventoryActivityReport GetInventoryActivityReport(int id)
{
InitAsync();
return conn.Table<InventoryActivityReport>().Where(i => i.Id == id).FirstOrDefault();
}
public InventoryCount GetInventoryCount(int id)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.Id == id).FirstOrDefault();
}
public List<InventoryCount> GetInventoryCountsByInventoryActivityReportId(int id)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.InventoryActivityReportID == id).ToList();
}
public List<InventoryCount> GetInventoryCountsByInventoryReport(InventoryActivityReport inventoryActivityReport)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.InventoryActivityReportID == inventoryActivityReport.Id).ToList();
}
public async Task<InventoryCount> GetInventoryItemCountForDateAsync(DateTime date, int id)
{
InitAsync();
return conn.Table<InventoryCount>()
.Where(ic => ic.CountedDate == date.Date && ic.ItemId == id)
.FirstOrDefault();
//if (count != null)
//{
// return count;
//}
//else
//{
// return new InventoryCount();
//}
}
public InventoryCount GetInventoryItemCountForDate(DateTime date, int id)
{
InitAsync();
//InventoryCount count = conn.Table<InventoryCount>().Where(ic => ic.CountedDate.Date == date.Date && ic.ItemId == id).FirstOrDefault();
InventoryCount count = conn.Table<InventoryCount>()
.Where(ic => ic.CountedDate == date.Date && ic.ItemId == id)
.FirstOrDefault();
if (count != null)
{
return count;
}
else
{
return new InventoryCount();
}
}
public async Task ShareInventoryItemCountPdfAsync(DateTime date, List<Item> items, string pdfLogoPath, string pdfTitle)
{
var pdfDocument = await GenerateInventoryItemCountPdfAsync(date, items, pdfLogoPath, pdfTitle).ConfigureAwait(false);
// var pdfStream = new MemoryStream(pdfDocument.ToArray());
var pdfFile = new ShareFile("InventoryItemCount.pdf", "application/pdf");
await Share.RequestAsync(new ShareFileRequest { Title = "Share Inventory Item Count PDF", File = pdfFile }).ConfigureAwait(false);
}
public void UpdateInventoryCount(InventoryCount inventoryCount)
{
int result = 0;
try
{
InitAsync();
if (inventoryCount != null)
{
result = conn.Update(inventoryCount);
}
}
catch (Exception ex)
{
}
}
If you need further assistance or have any specific questions about this code, feel free to ask.
英文:
I want to design a professional looking pdf report of item count using skiaSharp but I have failed to get the proper code example of it.
I want to use an sqlite table, to generate the report grouped into item categories with category totals.
The code returns error on:
pdfDocument.ToArray())
> SKDocument doesn't contain a definition for 'ToArray'
Here is the code I tried tried.
public async Task<SKDocument> GenerateInventoryItemCountPdfAsync(DateTime date, List<Item> items, string pdfLogoPath, string pdfTitle)
{
var stream = new MemoryStream();
using var document = SKDocument.CreatePdf(stream);
var pageSize = new SKSize(595, 842);
using var canvas = document.BeginPage((int)pageSize.Width, (int)pageSize.Height);
using (var logoBitmap = SKBitmap.Decode(pdfLogoPath))
{
//var logoRect = SKRect.Create(pageSize.Width / 2 - logoBitmap.Width / 2, 50, logoBitmap.Width, logoBitmap.Height);
//canvas.DrawBitmap(logoBitmap, logoRect);
var titlePaint = new SKPaint
{
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
TextSize = 24,
IsAntialias = true,
Color = SKColors.Black
};
var titleBounds = new SKRect();
titlePaint.MeasureText(pdfTitle, ref titleBounds);
canvas.DrawText(pdfTitle, pageSize.Width / 2 - titleBounds.Width / 2, 150, titlePaint);
var countPaint = new SKPaint
{
Typeface = SKTypeface.FromFamilyName("Arial", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
TextSize = 18,
IsAntialias = true,
Color = SKColors.Black
};
var y = 200;
foreach (var item in items)
{
var itemCount = await GetInventoryItemCountForDateAsync(date, item.Id);//.ConfigureAwait(false);
string condition;
if (itemCount != null)
{
condition = itemCount.InventoryItemUnitQty.ToString();
}
else
{
condition = "N/A";
}
var countText = $"{item.ItemFullName}:{condition}";
var countBounds = new SKRect();
countPaint.MeasureText(countText, ref countBounds);
canvas.DrawText(countText, pageSize.Width / 2 - countBounds.Width / 2, y, countPaint);
y += 30;
}
document.EndPage();
document.Close();
return document;
}
}
public List<InventoryActivityReport> GetAllInventoryActivityReports()
{
InitAsync();
return conn.Table<InventoryActivityReport>().ToList();
}
public List<InventoryCount> GetAllInventoryCounts()
{
InitAsync();
return conn.Table<InventoryCount>().ToList();
}
public InventoryActivityReport GetInventoryActivityReport(int id)
{
InitAsync();
return conn.Table<InventoryActivityReport>().Where(i => i.Id == id).FirstOrDefault();
}
public InventoryCount GetInventoryCount(int id)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.Id == id).FirstOrDefault();
}
public List<InventoryCount> GetInventoryCountsByInventoryActivityReportId(int id)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.InventoryActivityReportID == id).ToList();
}
public List<InventoryCount> GetInventoryCountsByInventoryReport(InventoryActivityReport inventoryActivityReport)
{
InitAsync();
return conn.Table<InventoryCount>().Where(i => i.InventoryActivityReportID == inventoryActivityReport.Id).ToList();
}
public async Task<InventoryCount> GetInventoryItemCountForDateAsync(DateTime date, int id)
{
InitAsync();
return conn.Table<InventoryCount>()
.Where(ic => ic.CountedDate == date.Date && ic.ItemId == id)
.FirstOrDefault();
//if (count != null)
//{
// return count;
//}
//else
//{
// return new InventoryCount();
//}
}
public InventoryCount GetInventoryItemCountForDate(DateTime date, int id)
{
InitAsync();
//InventoryCount count = conn.Table<InventoryCount>().Where(ic => ic.CountedDate.Date == date.Date && ic.ItemId == id).FirstOrDefault();
InventoryCount count = conn.Table<InventoryCount>()
.Where(ic => ic.CountedDate == date.Date && ic.ItemId == id)
.FirstOrDefault();
if (count != null)
{
return count;
}
else
{
return new InventoryCount();
}
}
public async Task ShareInventoryItemCountPdfAsync(DateTime date, List<Item> items, string pdfLogoPath, string pdfTitle)
{
var pdfDocument = await GenerateInventoryItemCountPdfAsync(date, items, pdfLogoPath, pdfTitle).ConfigureAwait(false);
// var pdfStream = new MemoryStream(pdfDocument.ToArray());
var pdfFile = new ShareFile("InventoryItemCount.pdf", "application/pdf");
await Share.RequestAsync(new ShareFileRequest { Title = "Share Inventory Item Count PDF", File = pdfFile }).ConfigureAwait(false);
}
public void UpdateInventoryCount(InventoryCount inventoryCount)
{
int result = 0;
try
{
InitAsync();
if (inventoryCount != null)
{
result = conn.Update(inventoryCount);
}
}
catch (Exception ex)
{
}
}
答案1
得分: 0
你已经将pdf文档放入了一个流中:
var stream = new MemoryStream();
返回 stream
而不是返回 pdfDocument:
public async Task<MemoryStream> ...
var stream = new MemoryStream();
using var document = SKDocument.CreatePdf(stream);
...
return stream;
// usage
var pdfStream = await GenerateInventoryItemCountPdfAsync(...
英文:
You are already putting the pdfdocument into a stream:
var stream = new MemoryStream();
.
Return stream
instead of returning pdfDocument:
public async Task<MemoryStream>` ...
var stream = new MemoryStream();
using var document = SKDocument.CreatePdf(stream);
...
return stream;
// usage
var pdfStream = await GenerateInventoryItemCountPdfAsync(...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论