locked
Asp.net iTextSharp ASPXTOPDF with Foreach Querystring RRS feed

  • Question

  • User-47589850 posted

    Hi,

    I want something like subject. I do not know if it is possible.
    I'm using aspxtopdf. It works for single page and export the single pdf. Standard itextsharp code. 

    But I want multiple page and multiple pdf. And I want to mail them. I tried a little but it didn't work. Can anyone do this before?
    Or do I have to create a pdf manually ? :(

    The sample code. But webrequest auth problem redirect to login page and not include css etc.

    if I can find code that works. I will make the selection with checkboxes over Gridview, and from there I will put it into foreach loop with id querystring.

     string body = string.Empty;
    
                using (StreamReader reader = new StreamReader(Server.MapPath("~/template.html")))
                {
                    body = reader.ReadToEnd();
                }
                body = body.Replace("{UserName}", "test");
            
    
    
    
                Response.ContentType = "application/pdf";
                string pdfname = "test";
               
                Response.AddHeader("content-disposition", "attachment;filename=" + pdfname + ".pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                //Page.RenderControl(hw);  //one aspx one pdf 
    
    
    
                HashSet<string> urls = new HashSet<string>() { "http://localhost:52833/admin/admin-print-pdf?id=126" };
                StringBuilder contentToWrite = new StringBuilder();
    
                foreach (var url in urls)
                {
                    WebRequest request = WebRequest.Create(url);
                    WebResponse response = request.GetResponse();
    
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        contentToWrite.Append(streamReader.ReadToEnd());
                    }
                }
    
                StringReader sr = new StringReader(contentToWrite.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 0f, 0f);
    
                MemoryStream memoryStream = new MemoryStream();
    
    
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
    
    
    
                iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica", "CP1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font fontTitle = new iTextSharp.text.Font(STF_Helvetica_Turkish, 7, iTextSharp.text.Font.NORMAL);
    
    
    
    
                XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
                fontProvider.Register("C:\\Windows\\Fonts\\Arial.ttf");
                BaseFont bF = BaseFont.CreateFont("C:\\windows\\fonts\\arial.ttf", "windows-1254", true);
                iTextSharp.text.Font f = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.NORMAL);
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(
                  writer, pdfDoc, sr
                );
    
                writer.CloseStream = false; //important
                pdfDoc.Close();
    
                memoryStream.Position = 0; //reset stream... important
    
    
    
    
    
                MailSend mailSend = new MailSend();
                mailSend.mail("test", body, "me mail", "", pdfname + ".pdf", memoryStream);
    
                Response.Redirect(Request.RawUrl);
            }

    Sunday, October 25, 2020 12:22 PM

All replies

  • User-718146471 posted

    I want multiple page and multiple pdf.

    My first question is have you purchased the component? Second, have you checked their FAQ section? It's possible you're running into a limitation of the free version of this component. To do multiples, you have to purchase it; I speak from experience. Their component is pretty smart and can detect if you are trying to bypass the freemium protections. In order to use this component in the web application I was expanding, it required us to purchase the full component. Keep me posted, I can help you, I just need the answer to those two first questions. Cheers!

    Friday, October 30, 2020 12:02 PM
  • User-47589850 posted

    Hi,

    First all, Thanks for your answer.

    Your first question: No, I did not buy. 

    Second: I searched for the faq page but could not find it. I think they removed it when they switched to the new paid version.

    I just wanted to ask if anyone is using it in this forum.

    Wednesday, November 4, 2020 8:40 AM