2012-04-03 3 views
1

저에게 어떤 해결책도주십시오; 나는이 코드를 사용하고 있습니다 :itextsharp에있는 머리말 심상을 적용하는 방법 Html to pdf 변환기

HeaderFooter header = new HeaderFooter(new Phrase("This is a header"), false); 
document.Header = header; 

을하지만,이 오류가 발생했습니다 :

CS0246 :
형식 또는 네임 스페이스 이름 'HeaderFooter이'을 (를) 찾을 수 없습니다 (당신에게 사용 지침 누락 또는이다 어셈블리 참조?

답변

1

그 코드가 deprecated and removed many years ago했지만 여전히 불행하게도 소스 코드의 의견에 살고있다.

은 무엇 당신이 원하는 것은 iTextSharp.text.pdf.PdfPageEventHelper 클래스를 서브 클래스 화해와 문서의 모든 페이지에 대해 한 번 호출되는 것이다 OnEndPage 방법을 처리 할 수 ​​있습니다 :

public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper { 
    public override void OnEndPage(PdfWriter writer, Document document) { 
     //Create a simple ColumnText object 
     var CT = new ColumnText(writer.DirectContent); 
     //Bind it to the top of the document but take up the entire page width 
     CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height); 
     //Add some text 
     CT.AddText(new Phrase("This is a test")); 
     //Draw our ColumnText object 
     CT.Go(); 
    } 
} 

방금에의 새로운 인스턴스를 바인딩이를 사용하려면 당신의 PdfWriterPageEvent 특성 :

,369 : 아래

writer.PageEvent = new MyPageEventHandler(); 

이 표시 iTextSharp 5.1.2.0 대상으로 전체 작업의 C# 2010 윈폼 응용 프로그램입니다

using System; 
using System.IO; 
using System.Windows.Forms; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 
     public Form1() { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) { 
      //Test file to create 
      string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); 
      //Standard PDF file stream creation 
      using (FileStream output = new FileStream(outputFile, FileMode.Create,FileAccess.Write,FileShare.None)){ 
       using (Document document = new Document(PageSize.LETTER)) { 
        using (PdfWriter writer = PdfWriter.GetInstance(document, output)) { 

         //Bind our custom event handler to the PdfWriter 
         writer.PageEvent = new MyPageEventHandler(); 
         //Open our PDF for writing 
         document.Open(); 

         //Add some text to page 1 
         document.Add(new Paragraph("This is page 1")); 
         //Add a new page 
         document.NewPage(); 
         //Add some text to page 2 
         document.Add(new Paragraph("This is page 2")); 

         //Close the PDF 
         document.Close(); 
        } 
       } 
      } 

      this.Close(); 
     } 
    } 
    public class MyPageEventHandler : iTextSharp.text.pdf.PdfPageEventHelper { 
     public override void OnEndPage(PdfWriter writer, Document document) { 
      //Create a simple ColumnText object 
      var CT = new ColumnText(writer.DirectContent); 
      //Bind it to the top of the document but take up the entire page width 
      CT.SetSimpleColumn(0, document.PageSize.Height - 20, document.PageSize.Width, document.PageSize.Height); 
      //Add some text 
      CT.AddText(new Phrase("This is a test")); 
      //Draw our ColumnText object 
      CT.Go(); 
     } 
    } 
} 
+0

우리는 어떻게 PageEventHandler를 사용하여 파일의 맨 위에 HTML을 추가합니까? CSS는 상대적인 위치 지정을하기에는 너무 제한적입니까? –

+0

@DanielCasserly, 귀하의 의견은 당신이 시도한 것을 포함하여 헌신적 인 질문으로 게시되었거나, 효과가있는 것, 효과가없는 것, 기대하는 것 등을 포함하여 게시 된 것이 더 좋을 것입니다. 또한 iText는 어떤 방식 으로든 HTML. 가능하다면 HTML 문자열을 PDF 객체로 변환하는 iText의 도우미 라이브러리가 있지만 여전히 PDF 객체입니다. –

+0

그 사실에 감사드립니다. 실제로 XMLStorPage를 재정의하는 메소드가 같은 클래스에서 (XMLWorkerHelper 파싱을 사용하여) 찾고있는 것을 발견했습니다. 어쨌든 고마워. –