My GridView does not display well in PDF printed copy, the GridView appears to be on the left of PDF paper.
I have style my GridView in and other control outside of the GridView but it seems the css styles do not work in PDF print.
The GridView Rows appears to be in the left side of the paper.
Please how can I correct this?
My PDF printing code
protected void Button3_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand("SELECT * FROM DocumentTable WHERE Id =@Id", con))
{
cmd.Parameters.AddWithValue("@Id", lblprefix.Text);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
byte[] image = (byte[])dr["logo"];
imgFileUpload.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);
byte[] bytes = (byte[])dr["signature"];
Image2.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(bytes);
File.WriteAllBytes(Server.MapPath("logo.jpg"), image);
File.WriteAllBytes(Server.MapPath("sign.jpg"), bytes);
Image2.ImageUrl = GetUrl("sign.jpg");
imgFileUpload.ImageUrl = GetUrl("logo.jpg");
}
var ImagUrl = imgFileUpload.ImageUrl;
var ImgeUrl = Image2.ImageUrl;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + nameLbl.Text + " Invoice" + DateTime.Now.ToString("MMM d, yyyy") + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
File.Delete(Server.MapPath("sign.jpg"));
File.Delete(Server.MapPath("logo.jpg"));
Image2.ImageUrl = ImgeUrl;
imgFileUpload.ImageUrl = ImagUrl;
Response.End();
con.Close();
}
}
}
catch (SqlException ex)
{
string msg = "Error:";
msg += ex.Message;
throw new Exception(msg);
}
}