I have written this code to merge insert a pdf at page 2 of an existing pdf. It works. But the problem is that the inserted pdf is in landscape mode i.e. size is coming different but actually it is in portrait mode but after inserting it comes in landscape, don't know why. How to set that?
private static string AppendToDocument(string FirstSource, string SecondSource)
{
String first_source = FirstSource;
String second_source = SecondSource;
String pathout = "D:/Shared Data/test.pdf";
//create a document object
//var doc = new Document(PageSize.A4);
//create PdfReader objects to read pages from the source files
PdfReader reader = new PdfReader(first_source);
PdfReader reader1 = new PdfReader(second_source);
//create PdfStamper object to write to the pages read from readers
PdfStamper stamper = new PdfStamper(reader1, new FileStream(pathout, FileMode.Create));
//get one page from htmlpdf.pdf
PdfImportedPage page = stamper.GetImportedPage(reader, 1);
//the page gotten from htmlpdf.pdf will be inserted at the second page in the first source doc
stamper.InsertPage(2, reader1.GetPageSize(1));
//insert the page
PdfContentByte pb = stamper.GetUnderContent(2);
pb.AddTemplate(page, 0, 0);
//close the stamper
stamper.Close();
return pathout;
}
FirstSource
and SecondSource
are the physical paths.