我的ASP.net MVC Web应用程序需要允许管理员为年度竞赛提供PDF格式的参赛卡。每个PDF页面需要包含4张卡片。我选择了PDF Sharp。
我有一个名为“ myTemplate”的模板,该模板将从Base64字符串加载到PdfDocument中,并创建了一个名为“ outputDocument”的新文档,该文档将显示在屏幕上供用户打印。
数据端工作正常,调试时我可以看到字段正在填充,但是,我遇到的问题是,当输出文档显示在屏幕上时,它似乎在同一页面上添加了多个实例,而不是数据是不同的。
在PdfDocumentOpenMode.Modify中,我尝试克隆页面,但是这给了我空白页面,并且我尝试重新实例化模板页面,以确保在填充之前清除其值。
将模板文件加载到:
var bytes = Convert.FromBase64String(b64String);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(bytes, 0, bytes.Length);
pdfStream.Position = 0;
PdfDocument myTemplate =
PdfReader.Open(pdfStream,PdfDocumentOpenMode.Import);
为输出文件创建内存流和文档:
MemoryStream outputStream = new MemoryStream();
PdfDocument outputDocument = new PdfDocument(outputStream);
从模板创建页面实例
PdfPage firstPage = myTemplate.Pages[0];
PdfPage lastPage = myTemplate.Pages[1];
循环将姓名和身份证添加到卡中
int personCount = 0;
foreach (DataRow dbRow in objDB.AllTables[dbQuery.table].Rows)
{
if (personCount < 4)
{
string nameField = $"Pass{personCount + 1}Name";
string idField = $"Pass{personCount + 1}Number";
myTemplate.AcroForm.Fields[nameField].Value = new
PdfString(dbRow["Title"].ToString().ToUpper() + " " +
dbRow["Forename"].ToString() + " " + dbRow["Surname"].ToString());
myTemplate.AcroForm.Fields[idField].Value = new
PdfString(dbRow["EntrantID"].ToString());
personCount++;
}
else
{
outputDocument.AddPage(firstPage);
outputDocument.AddPage(lastPage);
outputDocument.Save(outputStream);
//at next page
//This part was added for testing to manually set the values to blank
myTemplate.AcroForm.Fields["Pass1Name"].Value = new PdfString(dbRow["Title"].ToString().ToUpper() + " " + dbRow["Forename"].ToString() + " " + dbRow["Surname"].ToString());
myTemplate.AcroForm.Fields["Pass1Number"].Value = new PdfString(dbRow["EntrantID"].ToString());
myTemplate.AcroForm.Fields["Pass2Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass2Number"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass3Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass3Number"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass4Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass4Number"].Value = new PdfString("");
personCount = 1;
}
}
outputDocument.AddPage(firstPage);
outputDocument.AddPage(lastPage);
outputDocument.Save(outputStream);
return File(outputStream, "application/pdf");
上面的代码在浏览器中生成一个pdf文档,第一页上有四张卡片,其中包含正确的名称和ID。第二页是第一页的副本。数据会带回5个名字,因此最后一个名字应该放在第二页上,其他三张卡片为空白。我已逐步完成,可以看到第五个名称和三个空白填充了肢形。
我尝试手动对值进行编码,并尝试创建第一页,最后一页甚至模板的新实例,但是仍然无法使用。
编辑-我在下面添加了代码,由于可能不允许与外界共享信息,我不得不将部分内容从原来的内容中删除,这可能会更加有用。下面的代码复制了确切的信息-第1页和第2页包含相同的值。
public FileResult Index()
{
string base64String getBase64String();
var bytes = Convert.FromBase64String(base64String);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(bytes, 0, bytes.Length);
pdfStream.Position = 0;
List<string> names = new List<string>();
names.Add("Person 1");
names.Add("Person 2");
names.Add("Person 3");
names.Add("Person 4");
names.Add("Person 5");
names.Add("Person 6");
names.Add("Person 7");
MemoryStream outputStream = new MemoryStream();
outputStream.Position = 0;
PdfDocument myTemplate = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import);
PdfDocument outputDocument = new PdfDocument(outputStream);
PdfPage firstPage = myTemplate.Pages[0];
PdfPage lastPage = myTemplate.Pages[1];
int numberOfEntrants = names.Count;
int loopCount = 0;
foreach (string name in names)
{
if (loopCount < 4)
{
string nameField = $"Pass{loopCount + 1}Name";
string idField = $"Pass{loopCount + 1}Number";
myTemplate.AcroForm.Fields[nameField].Value = new PdfString(name);
myTemplate.AcroForm.Fields[idField].Value = new PdfString("some ID");
loopCount++;
}
else
{
loopCount = 0;
outputDocument.AddPage(firstPage);
outputDocument.AddPage(lastPage);
myTemplate.AcroForm.Fields["Pass1Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass1Number"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass2Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass2Number"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass3Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass3Number"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass4Name"].Value = new PdfString("");
myTemplate.AcroForm.Fields["Pass4Number"].Value = new PdfString("");
loopCount = 1;
}
}
outputDocument.AddPage(firstPage);
outputDocument.AddPage(lastPage);
outputDocument.Save(outputStream);
return File(outputStream, "application/pdf");
}