我使用.x网络应用程序使用itextsharp进行pdf生成。我使用nuget包Install-Package iTextSharp -Version 5.5.10安装了以下版本的itextsharp。我看过样品并开发了 用于生成pdf的代码示例,它正在运行。但是我无法理解或将页眉页脚添加到pdf中。这是我正在使用的示例代码
public class Header : PdfPageEventHelper
{
protected Phrase header;
public void setHeader(Phrase header)
{
this.header = header;
}
public void onEndPage(PdfWriter writer, Document document)
{
PdfContentByte canvas = writer.DirectContent;
ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, header, 559, 806, 0);
}
}
Document pdfReport = null;
MemoryStream msReport = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfReport, msReport);
if (!string.IsNullOrEmpty(HeaderText))
{
Header objHeaderFooter = new Header();
//Here i need to assign the string HeaderText to Header. I dont know how to do it.
}
请帮助了解如何使用itextSharp分配HeaderText。大多数开发人员的例子都是关于java的 但我正在使用.net c#。
答案 0 :(得分:1)
您想在{/ p>中将HeaderText
分配给objHeaderFooter
if (!string.IsNullOrEmpty(HeaderText))
{
Header objHeaderFooter = new Header();
//Here i need to assign the string HeaderText to Header. I dont know how to do it.
}
假设HeaderText
为字符串,您可以使用Header
方法setHeader
执行此操作:
objHeaderFooter.setHeader(new Phrase(HeaderText));
此外,您必须将objHeaderFooter
分配给PdfWriter
实例:
pdfWriter.PageEvent = objHeaderFooter;
因此:
if (!string.IsNullOrEmpty(HeaderText))
{
Header objHeaderFooter = new Header();
objHeaderFooter.setHeader(new Phrase(HeaderText));
pdfWriter.PageEvent = objHeaderFooter;
}
此外,每当您覆盖c#中的方法时,请将其标记为override
。特别是在页面事件监听器中,使用
public override void onEndPage(PdfWriter writer, Document document)
这很容易忘记,特别是在移植java示例时,因为在java中相应的标记@Override
是可选的。
以下是操作<。p>提供的示例代码
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=abc.pdf");
Response.Charset = "";
Response.BinaryWrite(getbinary());
Response.End();
public byte[] getbinary()
{
Document pdfReport = null;
pdfReport = new Document(PageSize.A4, 25, 25, 40, 25);
MemoryStream msReport = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfReport, msReport);
pdfReport.Open();
if (!string.IsNullOrEmpty("Header Text"))
{
Header objHeaderFooter = new Header();
objHeaderFooter.SetHeader(new Phrase("Header Text"));
pdfWriter.PageEvent = objHeaderFooter;
}
PdfPTable ptData1 = new PdfPTable(1);
ptData1.SpacingBefore = 8;
ptData1.DefaultCell.Padding = 1;
ptData1.WidthPercentage = 100;
ptData1.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
ptData1.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
PdfPCell cell1 = new PdfPCell();
cell1.BorderWidth = 0.001F;
cell1.BackgroundColor = new BaseColor(250, 250, 250);
cell1.BorderColor = new BaseColor(100, 100, 100);
cell1.Phrase = new Phrase("Sample text");
ptData1.AddCell(cell1);
PdfPCell cell = new PdfPCell();
cell.BorderWidth = 0.001F;
cell.BackgroundColor = new BaseColor(200, 200, 200);
cell.BorderColor = new BaseColor(100, 100, 100);
cell.Phrase = new Phrase("test value");
ptData1.AddCell(cell);
pdfReport.Add(ptData1);
pdfReport.Close();
return msReport.ToArray();
}
public class Header : PdfPageEventHelper
{
protected Phrase header;
public void SetHeader(Phrase header)
{
this.header = header;
}
public void onEndPage(PdfWriter writer, Document document)
{
PdfContentByte canvas = writer.DirectContent;
ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, header, 559, 806, 0);
}
}
答案 1 :(得分:0)
可能这可以用作解决方法。添加标头作为另一个单元格值 以下是代码
boolean twice;
@Override
public void onBackPressed() {
if(twice) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
System.exit(0);
}
twice = true;
toastMessage(getResources().getString(R.string.msg0a));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
twice = false;
}
}, 2000);
}