0

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.

mkl
  • 90,588
  • 15
  • 125
  • 265
Stacky
  • 57
  • 1
  • 8
  • You use `GetPageSize`. Try `GetPageSizeWithRotation` instead. – mkl Jul 22 '19 at 15:28
  • @mkl: hi, I tried that but don't understand how would I do it? Can you help me on it? – Stacky Jul 22 '19 at 18:38
  • @mkl: I used GetPageSizeWithRotation and it works but page is out of alignment and doesn't properly fit in. – Stacky Jul 22 '19 at 20:32
  • Essentially you merely want to merge the documents, don't you? In that case you should use `PdfCopy`. Or do you use the `PdfStamper` for a reason? – mkl Jul 22 '19 at 20:44
  • I use it because my manager wants It this way. I know it's possible using PdfStamper. Kindly help me on this because I have spent a lot of time. – Stacky Jul 22 '19 at 20:49
  • everything is fine, just the page doesn't fit in. – Stacky Jul 22 '19 at 20:49

0 Answers0