I am using asp.net 3.5 with c#. I am displaying my existing pdf in iframe. Now i want to add new page to the existing pdf. for that i am using itextsharp. I am succssful in doing so but my problem is, the new page is added and became the first page. actually
i want the new page to be added at the end of pdf.for e.g i am having two pages in my existing pdf then the new page must be added after the two pags not before that. I am getting the newly added page first which is not the correct solution.
The code is as below:
PdfReader reader = new PdfReader(pathname);
Rectangle size1 = reader.GetPageSizeWithRotation(1);
Document mksheetpdf = new Document(size1);
Thanks for your solution. But that didnt work. I am having pdf and in that existing pdf i want to insert a new page. The code that i have given does the same but there is one problem in that. The new page is inserted at the starting and my existing page
shifts next to it. I want to add new page at the end.How to do that?
priyankafans...
Member
4 Points
7 Posts
how to add new page to the existing pdf using itextsharp
Jan 04, 2012 12:24 PM|LINK
Hello Everyone,
I am using asp.net 3.5 with c#. I am displaying my existing pdf in iframe. Now i want to add new page to the existing pdf. for that i am using itextsharp. I am succssful in doing so but my problem is, the new page is added and became the first page. actually i want the new page to be added at the end of pdf.for e.g i am having two pages in my existing pdf then the new page must be added after the two pags not before that. I am getting the newly added page first which is not the correct solution.
The code is as below:
PdfReader reader = new PdfReader(pathname);
Rectangle size1 = reader.GetPageSizeWithRotation(1);
Document mksheetpdf = new Document(size1);
string newpath = "TempPdf/" + splitpdfname[0].ToString() + sessionticks1 + ".pdf";
FileStream out1 = new FileStream(newpath, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(mksheetpdf, out1);
mksheetpdf.Open();
PdfContentByte cb = writer.DirectContent;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
// cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 12);
cb.BeginText();
string text = "Your Authentication Code is" + " " + sessionticks1;
cb.ShowTextAligned(1, text, 300, 500, 0);
//cb.ShowText(text);
// cb.NewlineText();
string text1 = "Test on url" + " " + schoolwebsite1;
cb.ShowTextAligned(1, text1, 300, 400, 0);
// cb.NewlineText();
string[] splitexp_date = expdate.Split(' ');
string text2 = "Your marksheet is valid till date" + " " + splitexp_date[0].ToString();
cb.ShowTextAligned(1, text2, 300, 300, 0);
cb.EndText();
for (int pagenumber = 1; pagenumber < reader.NumberOfPages + 1; pagenumber++)
{
mksheetpdf.SetPageSize(reader.GetPageSizeWithRotation(1));
if (pagenumber == 1)
{
mksheetpdf.NewPage();
}
PdfImportedPage page = writer.GetImportedPage(reader, pagenumber);
int rotation = reader.GetPageRotation(pagenumber);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(
page, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(pagenumber).Height
);
}
cb.AddTemplate(page, 0, 0);
}
// mksheetpdf.NewPage();
mksheetpdf.Close();
out1.Close();
writer.Close();
reader.Close();
Please help me out in getting this. i have searched a lot but didn't get any success.
Thanks in advance
RogerC
Member
34 Points
12 Posts
Re: how to add new page to the existing pdf using itextsharp
Jan 04, 2012 12:51 PM|LINK
document.NewPage();
see example below
http://forums.asp.net/t/1428577.aspx/1
priyankafans...
Member
4 Points
7 Posts
Re: how to add new page to the existing pdf using itextsharp
Jan 05, 2012 02:31 AM|LINK
Hello Roger,
Thanks for your solution. But that didnt work. I am having pdf and in that existing pdf i want to insert a new page. The code that i have given does the same but there is one problem in that. The new page is inserted at the starting and my existing page shifts next to it. I want to add new page at the end.How to do that?