2013-05-31 2 views
0

도움이 필요합니다. 지난 4 시간 동안 PDF 파일 총 페이지 수를 보냈습니다. 내 꼬리말에 "Page X/Y"와 같은 것을 넣고 싶습니다. 누군가이 코드로 무엇을 할 수 있는지 말해 줄 수 있습니까?PDF 파일의 총 페이지 수를 얻는 방법 - iTextSharp, C#

확인

public class pdfPage : PdfPageEventHelper 
{    
    public override void OnEndPage(PdfWriter writer, Document doc) 
    { 

     iTextSharp.text.Rectangle page = doc.PageSize; 
     //PdfPTable EndTable = new PdfPTable(2); 
     PdfPTable EndTable = new PdfPTable(2); 
     EndTable.DefaultCell.Padding = 2f; 
     EndTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED; 
     iTextSharp.text.Font smallfont2 = FontFactory.GetFont(FontFactory.HELVETICA, "CP1250", 10); 
     PdfPCell stopka1 = new PdfPCell(new Phrase("Left column - not important", smallfont2)); 
     stopka1.BorderWidthLeft = 0; 
     stopka1.BorderWidthBottom = 0; 
     stopka1.BorderWidthRight = 0; 
     stopka1.HorizontalAlignment = Element.ALIGN_LEFT; 
     stopka1.VerticalAlignment = Element.ALIGN_MIDDLE; 
     PdfPCell stopka2 = new PdfPCell(new Phrase("Page " + doc.PageNumber + "/", smallfont2)); 
     stopka2.BorderWidthLeft = 0; 
     stopka2.BorderWidthBottom = 0; 
     stopka2.BorderWidthRight = 0; 
     stopka2.HorizontalAlignment = Element.ALIGN_RIGHT; 
     stopka2.VerticalAlignment = Element.ALIGN_MIDDLE; 
     EndTable.AddCell(stopka1); 
     EndTable.AddCell(stopka2); 
     EndTable.TotalWidth = page.Width - doc.LeftMargin - doc.RightMargin; 
     EndTable.WriteSelectedRows(0, -1, doc.LeftMargin, EndTable.TotalHeight + doc.BottomMargin - 45, writer.DirectContent); 

    } 
    } 

편집, 나는 그것을 밖으로 분류. 단순히 작업중인 PDF 파일을 닫은 다음 임시 파일로 복사했습니다. 그런 다음 "OnEndPage"메서드에서이 임시 문서의 페이지를 계산했습니다. 나중에이 임시 파일의 모든 내용을 복사 한 새 문서를 열어 pdfPage 클래스의 객체를 만들고 writer2.PageEvent에 연결했습니다. 이제 작동합니다.

+1

당신은 우리에게 당신의 코드를주었습니다. 그러나 당신은 당신의 코드가 당신에게 어떤 의미로 실패했는지 (그렇게 좋지는 않다) 설명하지 않았다. 기존 코드가 얼마나 실패했는지 자세히 설명하십시오. –

+0

지금 무엇을 해야할지 모르겠습니다. 나는이 "PDF 파일의 전체 페이지 수"를 얻고 여기에 넣는 방법을 모른다. - PdfPCell stopka2 = 새로운 PdfPCell (새로운 문구 ("Page"+ doc.PageNumber + "/", smallfont2)); –

답변

3

documentation을 읽어보십시오. 찾고자하는 곳을 모른다면 keywords을 확인하십시오. 구체적으로는 Page X of Y입니다. 두 가지 예는 여기에있다

있습니다 : 당신이 이미 발견 한대로, 때문에

  • MovieCountries1는, 한 번에 PDF를 생성하고, onDocumentClose() 방법 페이지의 총 수를 추가하고, 방법이 없다 또는 어떤 종류의 소프트웨어라도 문서가 닫히기 전에 총 페이지 수를 알 수 있습니다.
  • TwoPasses은 솔루션으로 선택한 것보다 훨씬 강력한 솔루션 인 두 단계로 PDF를 만듭니다.

저는 Java를 이해하고 C#으로 변환 할만큼 기술에 정통한 사람 일 것이지만 다른 사람들이있는 사람들을 위해이 예제의 C# 버전을 찾을 수 있습니다 herehere.

관련 문제