Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将页码添加到pdf文档(itextsharp)_C#_Asp.net_Itextsharp - Fatal编程技术网

C# 将页码添加到pdf文档(itextsharp)

C# 将页码添加到pdf文档(itextsharp),c#,asp.net,itextsharp,C#,Asp.net,Itextsharp,我想在itextsharp pdf文件的页脚添加页码。我从html(asp.net repeater)生成pdf。我使用XMLWorkerHelper解析html内容。我搜索了很多,但找不到任何有用的内容。你必须用itextsharp打开pdf,然后自己添加页码。不久前我做过类似的事情,这是我的函数,可能会给你一个开始。 该函数将当前页面添加到左下角,因此您可能需要将其放置在其他适合您需要的位置 public static byte[] AddPageNumbers(byte[] pdf) {

我想在itextsharp pdf文件的页脚添加页码。我从html(asp.net repeater)生成pdf。我使用XMLWorkerHelper解析html内容。我搜索了很多,但找不到任何有用的内容。你必须用
itextsharp
打开pdf,然后自己添加页码。不久前我做过类似的事情,这是我的函数,可能会给你一个开始。 该函数将当前页面添加到左下角,因此您可能需要将其放置在其他适合您需要的位置

public static byte[] AddPageNumbers(byte[] pdf)
{
MemoryStream ms = new MemoryStream();
// we create a reader for a certain document
PdfReader reader = new PdfReader(pdf);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
// we retrieve the size of the first page
Rectangle psize = reader.GetPageSize(1);

// step 1: creation of a document-object
Document document = new Document(psize, 50, 50, 50, 50);
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// step 3: we open the document

document.Open();
// step 4: we add content
PdfContentByte cb = writer.DirectContent;

int p = 0;
Console.WriteLine("There are " + n + " pages in the document.");
for (int page = 1; page <= reader.NumberOfPages; page++)
{
    document.NewPage();
    p++;

    PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
    cb.AddTemplate(importedPage, 0, 0);

    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.BeginText();
    cb.SetFontAndSize(bf, 10);
    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, +p + "/" + n, 7, 44, 0);
    cb.EndText();
}
// step 5: we close the document
document.Close();
return ms.ToArray();
}
公共静态字节[]addPageNumber(字节[]pdf)
{
MemoryStream ms=新的MemoryStream();
//我们为某个文档创建一个阅读器
PdfReader reader=新PdfReader(pdf);
//我们检索总页数
int n=reader.NumberOfPages;
//我们检索第一页的大小
矩形psize=reader.GetPageSize(1);
//步骤1:创建文档对象
文件=新文件(psize,50,50,50);
//步骤2:我们创建一个侦听文档的编写器
PdfWriter writer=PdfWriter.GetInstance(文档,ms);
//步骤3:我们打开文档
document.Open();
//步骤4:我们添加内容
PdfContentByte cb=writer.DirectContent;
int p=0;
Console.WriteLine(“文档中有“+n+”页”);

对于(int page=1;page这样的东西应该可以:

var sourceFileList = new List<string>();

//add files to merge

int sourceIndex = 0;
PdfReader reader = new PdfReader(sourceFileList[sourceIndex]);
int sourceFilePageCount = reader.NumberOfPages;

Document doc = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(destinationFileName, FileMode.Create));
doc.Open();

PdfImportedPage page;
PdfContentByte contentByte = writer.DirectContent;                

int rotation;
while (sourceIndex < sourceFileList.Count)
{
    int pageIndex = 0;
    while (pageIndex < sourceFilePageCount)
    {
        pageIndex++;

        doc.SetPageSize(reader.GetPageSizeWithRotation(pageIndex));
        doc.NewPage();

        page = writer.GetImportedPage(reader, pageIndex);
        rotation = reader.GetPageRotation(pageIndex);

        if (rotation.Equals(90 | 270))
            contentByte.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(pageIndex).Height);
        else
            contentByte.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }

    sourceIndex++;
    if (sourceIndex < sourceFileList.Count)
    {
        reader = new PdfReader(sourceFileList[sourceIndex]);
        sourceFilePageCount = reader.NumberOfPages;
    }
}

doc.Close();
var sourceFileList=newlist();
//添加要合并的文件
int sourceIndex=0;
PdfReader reader=新的PdfReader(sourceFileList[sourceIndex]);
int sourceFilePageCount=reader.NumberOfPages;
Document doc=新文档(reader.GetPageSizeWithRotation(1));
PdfWriter writer=PdfWriter.GetInstance(doc,新文件流(destinationFileName,FileMode.Create));
doc.Open();
PDF导入页面;
PdfContentByte contentByte=writer.DirectContent;
整数旋转;
while(sourceIndex
Hmm,奇怪。你到底是如何搜索的?因为当我访问时,我得到了相当多的有趣的指针。如果你动态创建文档,会有添加页码的示例。但是我从页面中的html创建pdf文档。我什么都不做,我只是通过这个转发器获得转发器的html输出。RenderControl属性并用xhtmlworkerhelp类解析它。xhtmlworkerhelp很快创建了pdf文档,我不涉及文档的细节。我希望我能告诉你我在做什么。回答得很好!但是ms.Write(pdf,0,pdf.Length)不是必需的,并且在我的情况下导致了文件损坏。