2014-02-21 3 views
0

iTextSharp 라이브러리를 사용하여 tiff 파일을 pdf로 변환하려고하는데이 예외가 발생합니다.pdf 생성 중 예외 "문서 열기 중"

Dim saveFileDialog1 As New SaveFileDialog() 
saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*" 
saveFileDialog1.FilterIndex = 2 
saveFileDialog1.RestoreDirectory = True 

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then 
    ' creation of the document with a certain size and certain margins 
     Dim document As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0) 


     ' load the tiff image and count the total pages 
     Dim bm As New System.Drawing.Bitmap(OpenFileDialog1.FileName) 
     Dim total As Integer = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page) 

     document.Open() 
     ' creation of the different writers 
     Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, New System.IO.FileStream(saveFileDialog1.FileName, System.IO.FileMode.Create)) 

     Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent 
     For k As Integer = 0 To total - 1 
      bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k) 
      Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp) 
      ' scale the image to fit in the page 
      img.ScalePercent(72.0F/img.DpiX * 100) 
      img.SetAbsolutePosition(0, 0) 
      cb.AddImage(img) 
      document.NewPage() 
     Next k 
     document.Close() 

사람이 어디에 내가 잘못 갈 거 야 나를 말할 수 : 여기 "document is open"

내 코드?

편집 : 치수 추가하려고

: 나는 my book about iText에서 설명하는 것처럼이 iText를 사용하는 경우, 당신은 5 단계로 PDF를 생성해야

img.ScaleToFit(595, 842) 
img.SetAbsolutePosition(0, 0) 

enter image description here

+0

정확히 어디에서 오류가 발생합니까? 나는 거의 그것의 외모에서 내기 : 희미 작가로 iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance (document, New System.IO.FileStream (saveFileDialog1.FileName, System.IO.FileMode. 만들기)) – Codexer

+0

예. 왜 그런지 말해 주시겠습니까? 지금해야 할 일이 무엇입니까? \ – coder

+0

그 모습에서,이 예제를 따랐습니까? http://atashbahar.com/post/Converting-Multipage-TIFF-image-to-PDF.aspx; 내가 틀렸다면 나를 바로 잡아라. – Codexer

답변

1

을 (날카로운). writer을 만드는 것은 2 단계이며 문서를 여는 것은 3 단계입니다.이 두 단계를 전환했으며 이것이 오류의 원인입니다.

document.Open() 줄을 몇 줄 이동해야합니다. 여기서 writer을 작성한 후 cb 인스턴스를 작성하십시오.

+0

지금 나는 그것을 통과하고 운 좋게도 ur 대답을 얻었다. 많은 thnks 그 하나 더 의심의 여지가 tiff 파일을 추가 할 때 dosent 내가 이미지에 맞게 필요로 이미지를 확장합니다. img.ScalePercent (24.0F)를 사용해 보았지만 이미지가 가운데에 생성됩니다. 어떻게 수정해야하는지 말해 주실 수 있습니까? – coder

+0

A4 페이지를 만들고 크기를 알 수없는 이미지를 추가하고 있습니다. 내가 너라면, 이미지를 A4 페이지에 맞게 크기를 조정한다. img.ScaleToFit (595, 842); 값 595와 842는 A4 페이지의 너비와 높이다. 'ScaleToFit()'메서드는 이미지의 종횡비를 보존합니다. 그럴 필요가 없다면, 당신은'img.ScaleAbsolute (595, 842);'를 사용할 수있다. (그러나 그것은 분명히 다른 종횡비의 이미지를 왜곡시킬 것이다). 이미지를 하단에 추가하려면'img.SetAbsolutePosition (0, 0);'을 잊지 마라! –

+0

위의 그림과 같이 치수를 추가하려고하면 결과가 나타납니다. 왜 이미지 위에 흰 공백이 생깁니 까? 마지막으로 상업용 프로젝트에이 DLL을 포함 시키거나 지불해야합니까? – coder