所有主人,我在使用iText和HtmForm导出为PDF的gridview时遇到麻烦。
我浏览过任何网站,他们建议使用HtmlForm和iText,但是这些方法是为aspx而不是webpart启用的。
当我在我的WebPart上实现时,导出到pdf的结果是许多代码(我的意思是代码生成默认的asp)。
这是我的代码。
Page.Response.ContentType = "application/pdf";
Page.Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
MyGridView.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(MyGridView);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Page.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Page.Response.Write(pdfDoc);
Page.Response.End();
这是导出为pdf的结果
//var theForm = document.forms['aspnetForm']; if (!theForm) { theForm = document.aspnetForm; } function
__doPostBack(eventTarget, eventArgument) { if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget; theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit(); } } ////var MSOWebPartPageFormName = 'aspnetForm'; var g_presenceEnabled = true;
var g_wsaEnabled = false; var g_wsaQoSEnabled = false; var g_wsaQoSDataPoints = []; var g_wsaLCID =
1033; var g_wsaListTemplateId = 850; var g_wsaSiteTemplateId = 'BLANKINTERNET#0'; var ULS;if (!ULS)
ULS=new Object();ULS.enable=true;ULS.Correlation="f1edb09c-3170-50d3-ebe2-8970f486cf98";var
_fV4UI=true;var _spPageContextInfo = {webServerRelativeUrl: "\u002fsites\u002ffifpeduli", webAbsoluteUrl:
"http:\u002f\u002febiz4:8882\u002fsites\u002ffifpeduli", siteAbsoluteUrl:
"http:\u002f\u002febiz4:8882\u002fsites\u002ffifpeduli", serverRequestPath:
"\u002fsites\u002ffifpeduli\u002fPages\u002fBookingBusOperational.aspx", layoutsUrl: "_layouts\u002f15",
webTitle: "Bus Peduli", webTemplate: "53", tenantAppVersion: "0", webLogoUrl:
"_layouts\u002f15\u002fimages\u002fsiteicon.png", webLanguage: 1033, currentLanguage: 1033,
currentUICultureName: "en-US", currentCultureName: "en-US", clientServerTimeDelta: new Date("2014-08-22T07:50:45.8058752Z") - new Date(), siteClientTag: "35$$15.0.4569.1000",
crossDomainPhotosEnabled:false, webUIVersion:15,
webPermMasks:{High:432,Low:1011028719},pageListId:"{cda1b8fe-a191-40ae-9d5b-32845313a173}",pageItemId:31, pagePersonalizationScope:1,userId:161, systemUserKey:"i:0\u0029.w|s-1-5-21-3191234019-2725770046-3684772125-1119", alertsEnabled:false, siteServerRelativeUrl:
"\u002fsites\u002ffifpeduli", allowSilverlightPrompt:'True'};function CallServer_59082126(arg, context)
{WebForm_DoCallback('ctl00$ctl21',arg,SP.UI.MyLinksRibbon.MyLinksRibbonPageComponent.ribbonActio
nCallback,context,null,false); }function _myLinksRibbonLoad2() { var fnd = function () { try {
mylinks_init.MyLinksInit('CallServer_59082126'); } catch (Ex) { } }; RegisterSod('mylinks_init',
'/_layouts/15/SP.UI.MyLinksRibbon.js'); LoadSodByKey('mylinks_init', fnd); } function
_myLinksRibbonLoad1() { ExecuteOrDelayUntilScriptLoaded(_myLinksRibbonLoad2, 'SP.Ribbon.js'); }
_spBodyOnLoadFunctionNames.push('_myLinksRibbonLoad1'); var L_Menu_BaseUrl="/sites/fifpeduli"; var
L_Menu_LCID="1033"; var L_Menu_SiteTheme="null"; document.onreadystatechange=fnRemoveAllStatus;
function fnRemoveAllStatus(){removeAllStatus(true)};function WebForm_OnSubmit() {
UpdateFormDigest('\u002fsites\u002ffifpeduli', 1440000); return true; } //
No. FieldOne FieldTwo FieldThree FieldFour FieldFive
1 BEKASI 500.000
2 HEAD OFFICE 225.500 7.205.000 670.001 121.230.712
Total Result 225.500 7.205.000 670.001 121.730.712
//WebForm_InitCallback();var _spFormDigestRefreshInterval = 1440000;//
在真实的我的pdf表中是真实的,因为无法上传图像。因为我的声誉。
答案 0 :(得分:0)
请使用以下代码。
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private void GetInPDf()
{
if (gridview.Rows.Count > 0)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gridview.RenderControl(htw);
var mem = new MemoryStream();
Document document = new Document(PageSize.LETTER, 50, 50, 50, 50);
PdfWriter.GetInstance(document, mem);
document.Open();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(sw.ToString()));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
Response.BinaryWrite(mem.ToArray());
Response.End();
Response.Flush();
Response.Clear();
}
}