Home » Asp.netRSS

Page break in pdf file.

Hey ,

I am using iTextSharp version 5.0.2.0 to get the data from database to pdf file. Well i pulled all those data in the repeater first and from repeater, I am showing those data in pdf file. My data is a list of data catagorized by the catagory name. Right now, the scenario of the output pdf file is it is showing all the data serially one after another without page break. But I want my data should be something like this.

Each catagory should start with the new page. And I am giving values in the repeater in the ascx page. So Is there any way I can page break in ascx page or how do I do page break in this case ?  Any ideas ? I tried document.NewPage() in the .cs file but it didn't work out. I saw the articles about the page break and even looked in the iText in Action book but couldn't get the way i wanted.  

 

Any help or idea is highly appreciated.

 

8 Answers Found

 

Answer 1

Hii,


In itextsharp u have a option you can go to new page  like this

Document doc = new Document();
        PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + @"\test.pdf", FileMode.Create));
        doc.Open();

doc.Add(table);

doc.NewPage();

doc.close();


if this didnt solve can u show u  r code ..

 

Answer 2

Ok. My code is like this:

protected void filetesting()
        {

           ....
           ....
           ....
           ....



            DataRelation dRel = new DataRelation("Category_Items", ds.Tables["Category"].Columns["ID"], ds.Tables["Item"].Columns["CatID"]);
            ds.Relations.Add(dRel);
            rptInfo.DataSource = ds;
            rptInfo.DataBind();
            
            HttpContext.Current.Response.Clear(); //clear anything in io buffer
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=example.pdf_" + DateTime.Now.ToString("MM-dd-yyyy") + "Time" + DateTime.Now.ToString("h-mmtt"));
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/pdf";
            StringWriter stringwrite = new StringWriter();
            HtmlTextWriter htmlwrite = new HtmlTextWriter(stringwrite);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            rptInfo.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 40f, 40f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.NewPage();
            pdfDoc.Close();
            Response.Write(pdfDoc);


        }


 

I send the data to the repeater like this:

void rptInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if ((item.ItemType == ListItemType.Item) ||
                (item.ItemType == ListItemType.AlternatingItem))
            {
                rptItemList = (Repeater)e.Item.FindControl("rptItemList");
                DataRowView drv = (DataRowView)item.DataItem;
                rptItemList.DataSource = drv.CreateChildView("Category_Items");
                rptItemList.DataBind();

                rptItemTotAmount = (Repeater)e.Item.FindControl("rptAmount");
                DataRowView drvTot = (DataRowView)item.DataItem;
                rptItemTotAmount.DataSource = drvTot.CreateChildView("Category_Items");
                rptItemTotAmount.DataBind();
            }
        }


 

And the ascx part code is like this:

<asp:Repeater runat="server" id="rptInfo">
        <HeaderTemplate>
            <font><b>Item lists based on catagory</b></font><br /><br />
        </HeaderTemplate>
       
        <ItemTemplate>
         <b><%# DataBinder.Eval(Container.DataItem, "CategoryName") %> <%# DataBinder.Eval(Container.DataItem, "CatName") %></b><br />
            <asp:Repeater id="rptItemList" runat="server">
                <ItemTemplate>
                    <asp:Table ID="tblItemList" runat="server"  Font-Size="Small">
                        <asp:TableRow>
                            <asp:TableCell><%# DataBinder.Eval(Container.DataItem,"Item")%> <%# DataBinder.Eval(Container.DataItem, "ItemDescription") %></asp:TableCell>
                            <asp:TableCell HorizontalAlign ="Right"><%# DataBinder.Eval(Container.DataItem,"Amount","{0:c}") %></asp:TableCell>
                        </asp:TableRow>
                   </asp:Table>
                 </ItemTemplate>
            </asp:Repeater>
            <asp:Repeater ID="rptItemTotAmount" runat="server">
                <ItemTemplate>
                    <asp:Table ID="tblItemListt" runat="server"  Font-Size="Small"> 
                        <asp:TableRow>
                            <asp:TableCell HorizontalAlign="Right"><%# DataBinder.Eval(Container.DataItem, "TotalAmount","<strong>Total: </strong>{0:c}") %></asp:TableCell>
                        </asp:TableRow>
                    </asp:Table>
                </ItemTemplate>
                
            </asp:Repeater>


 

Now I want the pagebreak right after the total amount displayed. That mean whenever new category start, it should start with the new page  in pdf  file. How to do that ?

 

Answer 3

SuchiSayami:

Now I want the pagebreak right after the total amount displayed. That mean whenever new category start, it should start with the new page  in pdf  file. How to do that ?

that's a lot of code to look through. it looks like you have two HTML tables consecutively, and after the second one (end of category) you want a page break? in any case if the markup is consistent you can use something like this:

<%@ WebHandler Language="C#" Class="checkTable" %>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

public class checkTable : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "application/pdf";
    string html = @"
<table border='1'>
<tr><td>ItemDescription</td><td align='right'>Amount</td></tr>
</table>
<table border='1'>
<tr><td align='right'>Amount</td></tr>
</table>
<table border='1'>
<tr><td>ItemDescription</td><td align='right'>Amount</td></tr>
</table>
<table border='1'>
<tr><td align='right'>Amount</td></tr>
</table>
    ";
    Document document = new Document();
    PdfWriter.GetInstance(document, context.Response.OutputStream);
    document.Open();
/* iterate over all elements */
    List<IElement> objects = HTMLWorker.ParseToList(
      new StringReader(html), null
    );
/* get PdfPTable object(s) */
    int count = 0;
    foreach (IElement element in objects) {
      PdfPTable tbl = element as PdfPTable;
      if (tbl != null) {
        ++count;
      }
      document.Add(element); 
/* change here if your markup is different */      
      if (count % 2 == 0) {
        document.NewPage();
      }
    } 
    document.Close();  
  }
  public bool IsReusable {
    get { return false; }
  }
}

if your markup is different just change the condition before calling document.NewPage().

 

Answer 4

Thanks for the reply.

But where in this code, I am passing my values from the repeater ? I am getting values combining two tables and pulling those values in the repeater. Plus I am calculating total price and displaying based on catagory. So my repeater has all those values right. But I couldn't figure it out where and how I'm going to insert NewPage. In the code you posted, where shall i give my repeater value ?


 

Answer 5

SuchiSayami:

But where in this code, I am passing my values from the repeater ? I am getting values combining two tables and pulling those values in the repeater. Plus I am calculating total price and displaying based on catagory. So my repeater has all those values right. But I couldn't figure it out where and how I'm going to insert NewPage. In the code you posted, where shall i give my repeater value ?

my code is using a StringReader instance just like yours is. just append my code to yours where you call:

StringReader sr = new StringReader(sw.ToString()); 
then use your StringReader instance instead of the one i created with the static string.

make sense?


 

 

Answer 6

I am so sorry I didn't get it. Can you please describe it?


 

Answer 7

SuchiSayami:
And I can't input this in the ascx.cs page  right ? So what could be the possible ways in this case ?
 

i didn't ask you to put the code in the user control. you posted a code snippet with a method named 'filetesting()' put it there.

sorry, but i can't write your code for you. please try and figure it out using the example code. it shouldn't be that hard.

 

 

Answer 8

ya thats where I kept. I kept the your part of the code in the filetesting() method and it didn't work out.


 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter