将PDF模板页面添加到新插入的页面。使用itextsharp

时间:2017-04-10 04:41:49

标签: c# pdf itext

我创建了一个我想用作模板页面的pdf文件(包含标题和边框),所以当我向现有的pdf添加新页面时,它会自动将标题和边框设置为新添加的页面。

我正在使用ITextSharp PDF库。

我尝试使用 PdfImportedPage ,如下所示,但它无效:

  PdfReader templateFile;
  templateFile = new PdfReader("D:\\templatefile.pdf");

    PdfImportedPage template_page;
    var fileStream1 = new FileStream("D:\\temp.pdf", FileMode.Create, FileAccess.Write);

    var stamper1 = new PdfStamper(templateFile, fileStream1);
    template_page = stamper1.GetImportedPage(templateFile, 1);

  // now i want to add this template_page to existing pdf at specific position  

  //this is the file(existingpdf.pdf) in which i want to add template_page create above using stamper
    var bytes = File.ReadAllBytes("D:\\existingpdf.pdf");

    var reader = new PdfReader(bytes);
    var numberofPages = reader.NumberOfPages;
    var fileStream = new FileStream("D:\\result.pdf", FileMode.Create, FileAccess.Write);

    //stamper of result pdf
    var stamper = new PdfStamper(reader, fileStream);

    stamper.InsertPage(numberofPages + 1, templateFile.GetPageSize(1));

    //below code i have tried to add template but its not working in result pdf
    stamper.GetUnderContent(2).AddTemplate(template_page, 0, 0);

     stamper.Close();

0 个答案:

没有答案