我在使用 ITextSharp 创建一些推送到 MVC 浏览器应用程序的表和图像时遇到问题。我想我已经把它缩小到使用 WriteRowsSelected 添加表的范围内。
这是我的代码:
PDFEngine.cs
private byte[] BuildDocument(MemoryStream documentStream, IEnumerable<CustomPdfData> documents, bool addVoid, float defaultMagnification)
{
using (var stream = new MemoryStream())
{
var finishedDocument = new Document(new Rectangle(612f, 792f));
var writer = PdfWriter.GetInstance(finishedDocument, stream);
writer.ViewerPreferences = PdfWriter.HideWindowUI;
writer.ViewerPreferences = PdfWriter.HideMenubar;
// Open the Document for writing
finishedDocument.Open();
var zoomer = PdfAction.GotoLocalPage(1, new PdfDestination(PdfDestination.XYZ, -1, -1, defaultMagnification), writer);
writer.SetOpenAction(zoomer);
foreach (var document in documents)
{
if (document.IsNewPage)
{
finishedDocument.NewPage();
}
var results = TableEngine.BuildDocument(document);
// Build Canvas for Table
//PdfContentByte canvas = writer.DirectContent;
//canvas.SetTextMatrix(50, 50);
// Add each table to the canvas at specified coordinates
for (int tableCounter = 0; tableCounter < results.ITextTables.Count; tableCounter++)
{
PdfContentByte canvas = writer.DirectContent;
canvas.SetTextMatrix(50, 50);
var xAxis = results.PdfTables[tableCounter].XPosition;
var yAxis = results.PdfTables[tableCounter].YPosition;
results.ITextTables[tableCounter].WriteSelectedRows(0, results.PdfTables[tableCounter].ColumnSize, xAxis, yAxis, writer.DirectContent);
}
}
finishedDocument.Close();
documentStream.Close();
return stream.ToArray();
}
CheckBuilder.cs
/// <summary>
/// Used to create the fake checks needed for printing to a client
/// </summary>
/// <param name="checks">Checks to push to Builder</param>
/// <returns></returns>
public byte[] GetFakeChecks(List<HandsCheck> checks)
{
var doc = BuildCheck(checks, true, true);
return CreatePdf(doc, 0.5f, false);
}
DisplayChecks.cshtml
<object id="WebBrowser1" width="615" height="790" data= "@Url.Action("GetPdfData")" type="application/pdf" />
家庭控制器.cs
public FileContentResult GetPdfData()
{
int numberofChecks = int.Parse(Workspace.Get<string>("numberofChecks"));
var pdfCreator = new CheckBuilder();
var checks = GetAllChecks();
var newList = new List<Check>();
for (int counter = 0; counter < numberofChecks; counter++)
{
newList.Add(checks[counter]);
}
var rawData = pdfCreator.GetFakeChecks(newList);
return File(rawData, "application/pdf");
return null;
}
奇怪的是,它似乎只在我添加超过1 个表时才会发生。有谁知道每张画布是否有任何限制?有谁知道进一步调试可能导致此问题的任何方法?