2012-05-07 318 views
3

我想將頁碼添加到itextsharp pdf文件的頁腳。我從html(asp.net repeater)生成pdf。並使用XMLWorkerHelper來解析html內容。搜查了很多,但找不到有用的東西來實現這一點。將頁碼添加到pdf文檔(itextsharp)

+1

嗯,奇怪。你究竟如何搜索?因爲當我訪問[以下鏈接]時(http://www.google.com/#hl=zh-CN&sclient=psy-ab&q=itextsharp+add+page+numbers&oq=itextsharp+add+page+numbers&aq=f&aqi=g1&aql=&gs_l = hp.3..0.438.7461.0.7578.27.15.0.12.12.0.158.1445.11j4.15.0 ... 0.0.xJrtHtJSBd0&PBX = 1&BAV = on.2,or.r_gc.r_pw.r_qf。,cf.osb&FP = d0f8cd49a0fc8cd2&BIW = 1440&bih = 795)我有相當多的有趣的指針。 –

+0

如果您在飛行中創建文檔,有關於添加頁碼的示例。但im從頁面中的html創建pdf文檔。我不做任何事情,我只是通過此repeater.RenderControl屬性獲取中繼器的html輸出並用xhtmlworkerhelp類來解析它。很快,pdf文檔正在由xhtmlworkerhelp創建,我不參與文檔的細節。我希望我能告訴我在做什麼。 – slayer35

回答

11

您必須使用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(); 
} 
+0

謝謝你,它爲我工作 – slayer35

+3

我如何轉換我的飛行生成PDF到字節數組? –

+0

很好的回答!但ms.Write(pdf,0,pdf.Length)沒有必要,並導致我的情況下文件損壞。 – Pinch

1

像這樣的東西應該工作:

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(); 
相關問題