2

私はこのライブラリを初めて使用し、いくつかの例を見つけました(PDFをページごとに分割しています)。

しかし、誰かが使い始めると常にFilestreamエラーが発生します(Object reference not ...)。作成中Filestreamはエラーは発生しません。どうしたの?ここからダウンロードしたライブラリ。

編集

   private static void Test()
    {
        Document pdfDocument = new Document( );
        PdfWriter.GetInstance(pdfDocument,
        new FileStream("D:\\WDPT.PDF", FileMode.Create));
        // here is eror
        pdfDocument.Open( );
        pdfDocument.Add(new Paragraph("Here is a test of creating a PDF"));
        pdfDocument.Close( );
    }

このコードはコンソールアプリケーションで実行されます。ここでのエラーは、ファイルストリームの使用を開始したときに発生します(他の場合と同様)。

4

2 に答える 2

0

DLL参照を使用するには、これを記述する必要があります(これはVBのコードです)

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.html

次に、ストリーム オブジェクトを渡して PDF を生成します。

エラーが発生した場合:「オブジェクト参照セットはオブジェクトのインスタンスではありません」つまりどこか宣言後、nullに設定します(またはnullとして渡されます)。以下のようにコードを書きます

    Dim pDoc As New Document(pRec)
    Dim pWriter As PdfWriter
    Response.AddHeader("Content-Disposition", "attachment;filename=" & Title & " .pdf")
    pWriter = PdfWriter.GetInstance(pDoc, Response.OutputStream)
    pRec.Border = 1
    pRec.BorderColor = BaseColor.MAGENTA
    pDoc.Open()
    'Add pdf Detail
    pDoc.AddTitle("REPORTS")
    pDoc.AddSubject(Title)
    pDoc.AddAuthor("ADMIN")
    pDoc.AddHeader("Company", "DK LTD")
    pDoc.AddHeader("PageSize", DocSize)

ここにストリーム ライター オブジェクトを追加し、EOF までループします。

   pdoc.close()

私が知っている限りのことを説明しようと思います。

于 2014-04-21T09:43:56.140 に答える
0

これを試してみてください。オブジェクトライターを使用する必要があります。

            Document pdfDocument= new Document(PageSize.A4);

            new PdfWriter.GetInstance(pdfDocument, new FileStream(OutPutFileNameFullPath+ ".pdf", FileMode.Create));

            pdfDocument.Open();
            pdfDocument.Add(new Paragraph("Here is a test of creating a PDF"));
            pdfDocument.Close( ); 
于 2013-12-28T19:19:33.937 に答える