页面添加使用iTextSharp的PDF文档 [英] Add a page to PDF document using iTextSharp

查看:82
本文介绍了页面添加使用iTextSharp的PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个页面包含简单的文本现有的PDF文档。



我曾尝试下面的代码,我发现在互联网上,但到目前为止,我还没有得到它的工作:

  PdfReader读卡器=新PdfReader(次数1.pdf); 
文档的文档=新的文件(reader.GetPageSize(1));
PdfCopy复印机=新PdfCopy(文件,新的FileStream(2.pdf,FileMode.Create));

为(INT pageCounter = 1; pageCounter< reader.NumberOfPages + 1; pageCounter ++)
{
//字节[]页= reader.GetPageContent(pageCounter);
copier.AddPage(copier.GetImportedPage(读卡器,pageCounter));
}

copier.NewPage();
copier.Add(新段(这是添加文本));

document.Close();
reader.Close();

请让我知道如何做到这一点吧?


解决方案

 私有静态字符串AddCommentsToFile(字符串文件名,字符串userComments)
{
串outputFileName = Path.GetTempFileName( );
//第1步:创建一个Docuement对象
文档的文档=新的文件();

{
//第2步:我们创建了监听文件
PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream作家(outputFileName,FileMode.Create) );

//第3步:打开文件
document.Open();

PdfContentByte CB = writer.DirectContent;

//当前文件的路径
字符串文件名=文件名;

//我们创建的文档
PdfReader读卡器=新PdfReader(文件名)读者;

为(INT PAGENUMBER = 1; PAGENUMBER< reader.NumberOfPages + 1; PAGENUMBER ++)
{
document.SetPageSize(reader.GetPageSizeWithRotation(1));
document.NewPage();

//插入到目标上的第一页
如果(PAGENUMBER == 1)
{
块fileRef =新的块();
fileRef.SetLocalDestination(文件名);
document.Add(fileRef);
}

PdfImportedPage页= writer.GetImportedPage(读卡器,PAGENUMBER);
INT旋转= reader.GetPageRotation(PAGENUMBER);
如果(旋转== 90 ||旋转== 270)
{
cb.AddTemplate(页面,0,-1f,1F,0,0,reader.GetPageSizeWithRotation(PAGENUMBER)。高度);
}
,否则
{
cb.AddTemplate(页面,1F,0,0,1F,0,0);
}
}

//添加一个新的页面PDF文件
document.NewPage();

逐段=新的第();
字体titleFont =新字体(iTextSharp.text.Font.FontFamily.HELVETICA
,15
,iTextSharp.text.Font.BOLD
,BaseColor.BLACK
) ;
块titleChunk =新的块(注释,titleFont);
paragraph.Add(titleChunk);
document.Add(段落);

款=新的第();
字体TEXTFONT =新字体(iTextSharp.text.Font.FontFamily.HELVETICA
,12
,iTextSharp.text.Font.NORMAL
,BaseColor.BLACK
) ;
块textChunk =新的块(userComments,TEXTFONT);
paragraph.Add(textChunk);

document.Add(段落);
}
赶上(例外五)
{
扔Ë;
}
终于
{
document.Close();
}
返回outputFileName;
}


I would like to add a page to an existing PDF document containing simple text.

I have tried the following code that I found on the internet, but so far I haven't got it to work:

PdfReader reader = new PdfReader("1.pdf");
Document document = new Document(reader.GetPageSize(1));
PdfCopy copier = new PdfCopy(doc, new FileStream("2.pdf", FileMode.Create));

for (int pageCounter = 1; pageCounter < reader.NumberOfPages + 1; pageCounter++)
{
    //byte[] page = reader.GetPageContent(pageCounter);
    copier.AddPage(copier.GetImportedPage(reader, pageCounter));
}

copier.NewPage();
copier.Add(new Paragraph("This is added text"));

document.Close();
reader.Close();

Please let me know how to do this right?

解决方案

    private static string AddCommentsToFile(string fileName, string userComments)
    {
        string outputFileName = Path.GetTempFileName();
        //Step 1: Create a Docuement-Object
        Document document = new Document();
        try
        {
            //Step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create));

            //Step 3: Open the document
            document.Open();

            PdfContentByte cb = writer.DirectContent;

            //The current file path
            string filename = fileName;

            // we create a reader for the document
            PdfReader reader = new PdfReader(filename);

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

                //Insert to Destination on the first page
                if (pageNumber == 1)
                {
                    Chunk fileRef = new Chunk(" ");
                    fileRef.SetLocalDestination(filename);
                    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);
                }
            }

            // Add a new page to the pdf file
            document.NewPage();

            Paragraph paragraph = new Paragraph();
            Font titleFont = new 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();
            Font textFont = new Font(iTextSharp.text.Font.FontFamily.HELVETICA
                                     , 12
                                     , iTextSharp.text.Font.NORMAL
                                     , BaseColor.BLACK
                );
            Chunk textChunk = new Chunk(userComments, textFont);
            paragraph.Add(textChunk);

            document.Add(paragraph);
        }
        catch (Exception e)
        {
            throw e;
        }
        finally
        {
            document.Close();
        }
        return outputFileName;
    }

这篇关于页面添加使用iTextSharp的PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆