2013-08-22 2 views
2

아래 코드를 시도했지만 오류가 발생했습니다. 최신 DLL을 사용하고 있습니다.ITextsharp를 사용하여 HTML을 PDF로 내보내기

The given key was not present in the dictionary.

+1

HTMLWorker는 사용되지 않으며 더 이상 지원되지 않습니다. 최신 버전을 사용하기 때문에 대체 XMLWorker를 찾을 수 있습니다. –

+0

어떤 줄에 오류가 있습니까? –

+0

@ QaisarShabbirAwan PDF에 이미지를 배치하는 방법을 알고 있습니까? 아마도''또는''전체를 사용해 보시겠습니까? – Pierre

답변

-1

은 당신이 PDF 파일을 HTML로 변환 도움이 될 것입니다 코드 아래에 시도 할 수 있습니다 :

String strSelectUserListBuilder = @"<html><body> 
           <h1>My First Heading</h1> 
           <p>My first paragraph.</p> 
          </body> 
         </html>"; 

String htmlText = strSelectUserListBuilder.ToString(); 

List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null); 

나는이 오류가 발생했습니다.

String strSelectUserListBuilder = @"<html><body> 
           <h1>My First Heading</h1> 
           <p>My first paragraph.</p> 
          </body> 
         </html>"; 

String htmlText = strSelectUserListBuilder.ToString(); 
CreatePDFFromHTMLFile(htmlText , pdfFileName); 


public void CreatePDFFromHTMLFile(string HtmlStream, string FileName) 
{ 
    try 
    { 
     object TargetFile = FileName; 
     string ModifiedFileName = string.Empty; 
     string FinalFileName = string.Empty; 

     /* To add a Password to PDF -test */ 
     TestPDF.HtmlToPdfBuilder builder = new TestPDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4); 
     TestPDF.HtmlPdfPage first = builder.AddPage(); 
     first.AppendHtml(HtmlStream); 
     byte[] file = builder.RenderPdf(); 
     File.WriteAllBytes(TargetFile.ToString(), file); 

     iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString()); 
     ModifiedFileName = TargetFile.ToString(); 
     ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1"); 

     string password = "password"; 
     iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, password, "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);ss 
     reader.Close(); 
     if (File.Exists(TargetFile.ToString())) 
      File.Delete(TargetFile.ToString()); 
     FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1); 
     File.Copy(ModifiedFileName, FinalFileName); 
     if (File.Exists(ModifiedFileName)) 
      File.Delete(ModifiedFileName); 

    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 
+1

하지만 HTML 이미지 태그 마크 업은 파싱되지 않습니다. 이미지를 파싱하는 방법을 알고 있습니까? & TestPDF는 무엇입니까? –

6

이 시도 :

Document document = new Document(); 
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create)); 
document.Open(); 
iTextSharp.text.html.simpleparser.HTMLWorker hw = 
      new iTextSharp.text.html.simpleparser.HTMLWorker(document); 
hw.Parse(new StringReader(htmlText)); 
document.Close(); 
+1

호기심에서 벗어나, 반환 값을 전혀 사용하지 않는 PdfWriter.GetInstance 호출은 무엇입니까? 전달 된 문서에 부작용이 있습니까? – computrius

+0

안녕하세요 @ Kapil Khandelwal! 이 잘 작동하지만 만약 내가 이미지와 HTML을 추가

이 실 거예요 오류 준다 ...! 어떤 해결책? –

관련 문제