[英]Positioning data using PDFsharp
我正在使用 PDFsharp 為模型中的每個項目生成多頁文檔。
我已經得到了最終 PDF 需要看起來像什么的粗略模型:
長話短說,我必須生成兩種不同類型的 PDF,我需要遵循模板的一種是多頁 PDF,因此變量_numberOfPages
在下面的代碼片段中。
我已經為多頁 PDF 版本的每個單獨項目實際生成信息。 根據請求中的項目數,我添加一個新頁面,然后獲取當前頁面的項目數組的索引並附加這樣的行。
PDF文檔類
public PdfDocument BuildPdfDocument()
{
if (_numberOfPages > 1)
{
PdfDocument pdfDocument = new PdfDocument();
for (int i = 0; i < _numberOfPages; i++)
{
PdfPage page = pdfDocument.AddPage();
page.Orientation = PageOrientation.Landscape;
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Arial", 30, XFontStyle.Regular);
XTextFormatter tf = new XTextFormatter(gfx);
XRect rect = new XRect(0, (page.Height / 4), page.Width, page.Height);
gfx.DrawRectangle(XBrushes.White, rect);
tf.Alignment = XParagraphAlignment.Left;
tf.DrawString(_request.GetText(i), font, XBrushes.Black, rect);
}
return pdfDocument;
}
else
{
// stuff relating to the other pdfDocument that is OK
}
}
FileRequest 類包含一個名為 GetText 的方法
public string GetText(int pageNo)
{
int[] fileRequestItems = this.FileRequestItems.Select(x => x.Id).ToArray();
var currentFileRequestItem = fileRequestItems[pageNo];
var items = FileRequestItems;
FileRequestItem item = items.FirstOrDefault(s => s.Id == currentFileRequestItem);
StringBuilder sb = new StringBuilder();
if (item.ObjectContextGetType() == typeof(FileRequestDocument)) // its a document so provide text for the PDF like this
{
var fileRequestDocument = item as FileRequestDocument;
sb.AppendLine("Date Requested: " + this.DateOfRequest.ToString("D"));
if (fileRequestDocument != null) sb.AppendLine("Box No: " + fileRequestDocument.Record.Box.BoxNumber);
sb.AppendLine("Location: " + this.Location.LocationName);
if (fileRequestDocument != null) sb.AppendLine("Description: " + fileRequestDocument.Record.Description);
sb.AppendLine("Requested By: " + this.Name);
sb.AppendLine("Tel: " + this.TelephoneNumber);
sb.AppendLine("Department: " + this.Department);
}
if (item.ObjectContextGetType() == typeof(FileRequestBox)) // its a box so provide text for the PDF like this
{
var fileRequestBox = item as FileRequestBox;
sb.AppendLine("Date Requested: " + this.DateOfRequest.ToString("D"));
if (fileRequestBox != null) sb.AppendLine("Box No: " + fileRequestBox.Box.BoxNumber);
sb.AppendLine("Location: " + this.Location.LocationName);
if (fileRequestBox != null) sb.AppendLine("Description: " + $"Entire Contents of Box Number {fileRequestBox.Box.BoxNumber}");
sb.AppendLine("Requested By: " + this.Name);
sb.AppendLine("Tel: " + this.TelephoneNumber);
sb.AppendLine("Department: " + this.Department);
}
return sb.ToString();
}
這一切的結果是下面的PDF,其中包含5項的文件請求將超過5頁,像名稱/日期等重復,但是獨有的描述東西FileRequestItem
或頁面。
如您所見,雖然信息都在那里,但定位與所需的模板不匹配。
如何為每個頁面創建指定的模板或類似的東西,然后相應地定位我的數據?
很好的嘗試將一個長的多行字符串傳遞給XTextFormatter
。 如果您需要控制位置,則方法錯誤。
要完全控制定位,請為單個項目調用DrawString
。
我會使用 MigraDoc:MigraDoc 允許您創建一個表格並將信息放在單個表格單元格中。
MigraDoc 使用 PDFsharp 創建 PDF 文件,但提供更高的 API 級別。
http://www.pdfsharp.net/wiki/Invoice-sample.ashx
這是一個老問題,但嘗試提供不同的方法,您可以嘗試在 HTML 表格字符串中構建布局並在單獨的文件中創建其 css 樣式,然后執行以下操作:
string stylesUrl = Server.MapPath("pathToPdfStyle");
CssData css = PdfGenerator.ParseStyleSheet(File.ReadAllText(stylesUrl));
PdfDocument pdfTemp = PdfGenerator.GeneratePdf({HTMLString}, PageSize.A4, 20, css);
當您在瀏覽器中查看 html 文檔和在 pdf 閱讀器中查看 html 內容時,根據大小和格式,會有小的視覺差異,但它們會非常相似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.