开发者

adding new pages to existing pdf using iTextSharp

I have a pdf file with 1 page.Now I want to add two more pages to it with some data or image. I tried this but it doesnot adding new pages :

private void addPage()
   {
     Document document = new Document();
     writer = PdfWriter
        .GetInstance(document, new FileStream(outputFileName, FileMode.Create));

        document.Open();

        PdfContentByte cb = writer.DirectContent;


     for (int pageNumber = 1;pageNumber < reader.NumberOfPages + 2;pageNumber++)
        {
            document.SetPageSize(reader.GetPageSizeWithRotation(1));
            document.NewPage();

            if (pageNumber == 1)
            {
                Chunk fileRef = new Chunk(" ");
                fileRef.SetLocalDestination(newPdf);
                document.Add(fileRef);
            }

            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
                );
            }
            else
            {
                cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
            }
        }
        document.NewPage();

        Paragraph paragraph = new Paragraph();
        iTextSharp.text.Font titleFont 开发者_如何学Go=
            new iTextSharp.text.Font(
                iTextSharp.text.Font.FontFamily.HELVETICA
                , 15
                , iTextSharp.text.Font.BOLD
                , BaseColor.BLACK
            );
        Chunk titleChunk = new Chunk("Comments", titleFont);
        paragraph.Add(titleChunk);
        document.Add(paragraph);

        paragraph = new Paragraph();
        iTextSharp.text.Font textFont =
            new iTextSharp.text.Font(
                iTextSharp.text.Font.FontFamily.HELVETICA
                , 12
                , iTextSharp.text.Font.NORMAL
                , BaseColor.BLACK
            );
        Chunk textChunk = new Chunk("", textFont);
        paragraph.Add(textChunk);

        document.Add(paragraph);
        }

        document.Close();

  }


Try this to add pages to your PDF documents:

PdfDocument pdfDoc = new PdfDocument(); // create document
PdfPage pdfPage = new PdfPage(); // create page 
pdfPage.Size = PageSize.Letter; // set page size
pdfDoc.Pages.Add(pdfPage); // add page to document
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜