2015-01-20 4 views
1

이미지가 포함 된 pdf 파일을 만듭니다. 이미지를 iTextSharp 이미지를 생성 한 후 서버에 저장합니다.iTextSharp "문서에 페이지가 없습니다." 오류

iTextSharp.text.Image backgroundImage = iTextSharp.text.Image.GetInstance(path); 

이 줄에는 "문서에 페이지가 없습니다."라는 오류가 표시됩니다. 여기

는 스택 트레이스입니다 :

location: iTextSharp.text.pdf.PdfPages.WritePageTree() 
    location: iTextSharp.text.pdf.PdfWriter.Close() 
    location: iTextSharp.text.pdf.PdfDocument.Close() 
    location: iTextSharp.text.pdf.PdfWriter.Close() 
    location: iTextSharp.text.DocWriter.Dispose() 
    location: MyProject.Helpers.FileUploadHelper.SaveMarathonCertificateTemplate(HttpRequestBase Request, String _fileName, CertificateOrientation orientation) c:\MyProject\Helpers\FileUploadHelper.cs : line 68 
    location: MyProject.Controllers.CertificateController.Add(Int32 marathonId, MarathonCertificate marathonCertificate) c:\MyProject\Controllers\CertificateController.cs: line 74 

어제 코드가 잘 작동했지만, 이상하게 오늘은이 오류를 받고 있어요. 여기 내 코드는 다음과 같습니다.

using (var fs = new FileStream(pdfFileName, FileMode.Create)) 
{ 
    using (var pdfDoc = new iTextSharp.text.Document()) 
    { 
     if (orientation == CertificateOrientation.HORIZONTAL) 
      pdfDoc.SetPageSize(PageSize.A4.Rotate()); 
     using (var w = PdfWriter.GetInstance(pdfDoc, fs)) 
     { 
      pdfDoc.Open(); 
      pdfDoc.NewPage(); // add Page here 

      iTextSharp.text.Image backgroundImage = iTextSharp.text.Image.GetInstance(path); 

      if (orientation == CertificateOrientation.HORIZONTAL) 
      { 
       backgroundImage.ScaleAbsoluteWidth(Config.PdfActualSizeHorizontal[0]); 
       backgroundImage.ScaleAbsoluteHeight(Config.PdfActualSizeHorizontal[1]); 
      } 
      else if (orientation == CertificateOrientation.VERTICAL) 
      { 
       backgroundImage.ScaleAbsoluteWidth(Config.PdfActualSizeVertical[0]); 
       backgroundImage.ScaleAbsoluteHeight(Config.PdfActualSizeVertical[1]); 
      } 
      backgroundImage.SetAbsolutePosition(0, 0); 
      pdfDoc.Add(backgroundImage); 

      pdfDoc.Close(); 
     } 
    } 
} 

나는 문제가 없습니다. 어떤 해결책이 있습니까?

System.ObjectDisposedException was caught Message=Cannot access a closed file.

새로운 스택 트레이스 :

EDIT:

나는 오류가 이것되고 그 후 이미지 인스턴스

pdfDoc.Add(new Paragraph(" ")); 

를 얻기 전에 한 줄을 추가

location: System.IO.__Error.FileNotOpen() 
location: System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count) 
location: iTextSharp.text.pdf.OutputStreamCounter.Write(Byte[] buffer, Int32 offset, Int32 count) 
location: iTextSharp.text.pdf.PdfIndirectObject.WriteTo(Stream os) 
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Write(PdfIndirectObject indirect, Int32 refNumber, Int32 generation) 
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, Int32 refNumber, Int32 generation, Boolean inObjStm) 
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, PdfIndirectReference refa, Boolean inObjStm) 
location: iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta, PdfIndirectReference refa) 
location: iTextSharp.text.pdf.PdfWriter.AddToBody(PdfObject objecta, PdfIndirectReference refa) 
location: iTextSharp.text.pdf.Type1Font.WriteFont(PdfWriter writer, PdfIndirectReference piref, Object[] parms) 
location: iTextSharp.text.pdf.FontDetails.WriteFont(PdfWriter writer) 
location: iTextSharp.text.pdf.PdfWriter.AddSharedObjectsToBody() 
location: iTextSharp.text.pdf.PdfWriter.Close() 
location: iTextSharp.text.DocWriter.Dispose() 
location: MyProject.Helpers.FileUploadHelper.SaveMarathonCertificateTemplate(HttpRequestBase Request, String _fileName, CertificateOrientation orientation) c:\MyProject\Helpers\FileUploadHelper.cs: line 70 
location: MyProject.Controllers.CertificateController.Add(Int32 marathonId, MarathonCertificate marathonCertificate) c:\MyProject\Controllers\CertificateController.cs: line 74 
+0

오류는 단지 원래 오류의 부작용입니다 :'iTextSharp.text.Image.GetInstance (path)'중에 뭔가 잘못되어 예외가 발생합니다. 그것은 붙잡히지 않는다; 따라서'using' 매개 변수의 닫음이 발생합니다. 문서에 아무 것도 추가되지 않았으므로이 닫기는 실패하고 예외 만 볼 수 있습니다. – mkl

+0

@mkl 방금 질문을 편집했습니다. 다시 볼 수 있니? –

+1

가장 안쪽의'사용 중 '이 잘못되었습니다. 이로 인해'PdfWriter'가 먼저 닫히고'Document' 만 닫힙니다. 그러나 닫는 동안 후자는'PdfWriter'가 여전히 열려 있어야합니다. 명시 적'PdfWriter' (예 : [HelloWorldDirect.cs] (https://svn.code.sf.net/p/itextsharp/code/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter01/HelloWorldDirect)를 사용하여 iTextSharp 샘플을 보면, .cs),'PdfWriter'가'using' 절에서 ** not **으로 생성 된 것을 볼 수 있습니다. – mkl

답변

0

이 하나를 시도 :

var path ="path of final pdf to save"; 
var imagePath="path of image that should be paste in final pdf file"; 
iTextSharp.text.Document document = new iTextSharp.text.Document(); 
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create)); 
iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(imagePath); 
myImage.ScaleAbsoluteHeight(document.PageSize.Height); 
myImage.ScaleAbsoluteWidth(document.PageSize.Width); 
myImage.Alignment = Element.ALIGN_CENTER; 
document.Add(myImage); 
document.Close(); 

모두입니다.

+0

'사용하기'를 사용하지 않으면 과다 사용하는 것처럼 부적절합니다. – mkl

관련 문제