如何使用itext sharp将标题放入Pdf文件中? [英] How to Put Header in Pdf file using itext sharp?

查看:178
本文介绍了如何使用itext sharp将标题放入Pdf文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想从GridView Data中获取Pdf文件。我希望使用这个genarated file.Iam使用下面的代码。





protected void btnpdf_Click(object sender,EventArgs e)

{

Response.ContentType =application / pdf;

Response.AddHeader(content-disposition,string.Format(attachment; filename = {0},StudentInfo.pdf));

Response.Cache.SetCacheability (HttpCacheability.NoCache);

使用(StringWriter sw = new StringWriter())

{

HtmlTextWriter hw = new HtmlTextWriter(sw);

//导出所有页面

gridshow.AllowPaging = false;

if(ViewState [GridPageData]!= null)

{

gridshow.DataSource = ViewState [GridPageData];

gridshow.DataBind();

}

else

{

ExecuteCheckedColumn() ;

}

pnlexport.RenderControl(hw);

StringReader sr = new StringReader(sw.ToString());

iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A2,7f,7f,7f,0f);

/ * pdfDoc .AddTitle(+ ddltables.SelectedItem.Text +); * /

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter.GetInstance(pdfDoc,Response。 OutputStream);

pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

}

}



谢谢和问候

Hari

Hi All,
Iam genarating Pdf file from GridView Data.I want heading of that genarated file.Iam using this below code.


protected void btnpdf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "StudentInfo.pdf"));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
//To Export all pages
gridshow.AllowPaging = false;
if (ViewState["GridPageData"] != null)
{
gridshow.DataSource = ViewState["GridPageData"];
gridshow.DataBind();
}
else
{
ExecuteCheckedColumn();
}
pnlexport.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A2, 7f, 7f, 7f, 0f);
/*pdfDoc.AddTitle(""+ddltables.SelectedItem.Text+"");*/
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
}

Thanks&Regards
Hari

推荐答案

Hi Hari,



你可以试试这个:



Hi Hari,

You could try this :

private void addHeader(pdf iPdf)
         {
             try
             {
                 iPdf.addCell("US President", 14, 1, 3, 0);
                 iPdf.addCell("Wars started", 3);
                 iPdf.addCell("Body count", 3);
                 iPdf.addCell("Weapons used(WMD)", 3);
             }
             catch (Exception ex)
             {
                 lblErrMessage.Text = ex.Message;
             }
         }



请注意:



对于iTextSharp版本5+,页眉/页脚属性已被删除。现在,这可以通过我们的PageEventHandler类来完成。虽然现在不是直截了当,但现在你可以在页眉和页脚中添加更多的计划文字。



请查看以下链接:



http://stackoverflow.com/questions/2321526/pdfptable-as-a-header-in-itextsharp

http:// stackoverflow。 com / questions / 2598917 / itextsharp-is-missing-headerfooter-class



希望这会有所帮助。


Please note :

For iTextSharp version 5+, Header/Footer property has been removed. Now this can be done by us PageEventHandler class. Though it's not straight forward now but the upside is that now you can add more than just plan text in header and footer.

Please check the following links :

http://stackoverflow.com/questions/2321526/pdfptable-as-a-header-in-itextsharp
http://stackoverflow.com/questions/2598917/itextsharp-is-missing-headerfooter-class

Hope this helps.


在你的代码之后pdfDoc.Close()语句粘贴此代码:

它将在底部添加页眉和页码 < br $>


In your code after pdfDoc.Close() statement paste this code:
It Will Add Header as well as page numbers at bottom

// add page numbers
          Document copyDoc = new Document();
          PdfCopy copyPdf = new PdfCopy(copyDoc, new FileStream(filepath, FileMode.Create));
          copyPdf.SetPageSize(PageSize.A4.Rotate());
          copyDoc.Open();
          Font textFont = FontFactory.GetFont("Helvetica", 9, Font.NORMAL, BaseColor.BLACK);
          Font headerFont = FontFactory.GetFont("Helvetica", 12, Font.BOLD, BaseColor.BLACK);
          // read the initial pdf document
          PdfReader reader = new PdfReader(pdfStream.ToArray());
          int totalPages = reader.NumberOfPages;

          PdfImportedPage copiedPage = null;
          iTextSharp.text.pdf.PdfCopy.PageStamp stamper = null;

          for (int i = 0; i < totalPages; i++)
          {
              // get the page and create a stamper for that page
              copiedPage = copyPdf.GetImportedPage(reader, (i + 1));
              stamper = copyPdf.CreatePageStamp(copiedPage);

              // add a page number to the page
              //ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase((i + 1) + "/" + totalPages, textFont), 820f, 15, 0);
              ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i + 1, reader.NumberOfPages)), 300f, 40, 0);
              ColumnText.ShowTextAligned(stamper.GetOverContent(), Element.ALIGN_CENTER, new Phrase("your page heading name", headerFont), 300f, 780, 0);

              stamper.AlterContents();

              // add the altered page to the new document
              copyPdf.AddPage(copiedPage);
          }

          copyDoc.Close();
          reader.Close();

          // flush and clear the document from memory
          pdfStream.Flush();
          pdfStream.Close();


这篇关于如何使用itext sharp将标题放入Pdf文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆