.NETいくつかの方法でPDFファイルを生成

オンラインで見つけるプロジェクトを行っていると、.NET MVC、いくつかの方法で生成されたPDFは、すべての情報を以下に示します

1、PDFへの単語の使用Microsoft.Office.Interop.Word.dll

dllが、一般的に、コンピュータを持っている、個別にダウンロードすることができます場所:C:\ WINDOWS \ Microsoft.NET \ 199bd4f2 \ edef3bc1アセンブリ\ DL3 \ 60e90863 \ 53bea978_07e9d401 \マイクロソフト\ VS ASP.NETの一時ファイル\ \ Frameworkの\ v4.0.30319 \。 Office.Interop.Word.DLL

パブリックブールWordToPdf(オブジェクトソースパス、文字列TARGETPATH)
        { 
            BOOL結果= FALSE; 
            WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF。
            オブジェクト行方不明= Type.Missing。
            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = NULL; 
            Microsoft.Office.Interop.Word.Documentドキュメント= NULL; 
            試す
            { 
                applicationClass =新しいMicrosoft.Office.Interop.Word.ApplicationClass(); 
                文書= applicationClass.Documents.Open(REF sourcePathは、refはrefは行方不明、refは行方不明、refは行方不明、refは行方不明、refは行方不明、refは行方不明、refは行方不明、refは行方不明、refは行方不明、refは行方不明、refが行方不明、行方不明、REF refが行方不明、行方不明、REF)が欠落しています。
                もし(ドキュメント!= NULL)
                { 
                    document.ExportAsFixedFormat(TARGETPATH、wdExportFormatPDF、偽、WdExportOptimizeFor.wdExportOptimizeForPrint、WdExportRange.wdExportAllDocument、0、0、WdExportItem.wdExportDocumentContent、真、真、WdExportCreateBookmarks.wdExportCreateWordBookmarks、真、真、偽、REF欠落)。
                } 
                結果= TRUE。
            } 
            キャッチ
            {
                    文書= NULL; 
                } 
                結果= FALSE;
            } 
            最後に
            { 
                IF(!文書= NULL)
                { 
                    document.Close(REF欠落、REF欠落、REF欠落)。
                (もし!applicationClass = NULL)
                { 
                    applicationClass.Quit(refの行方不明、refの行方不明、REF不足しています)。
                    applicationClass = NULL; 
                } 
            } 
            戻り値の結果; 
        }

  使用

公共FileResultデモ()
        { 
            文字列wordPath = Server.MapPathの(@ "\一時ファイル\ Wordの\ Test.docx"); 
            文字列pdfPath = Server.MapPathの(@ "\一時ファイル\ PDFの\のtest.pdfという"); 

            WordToPdf(wordPath、pdfPath)。

            FileStream fsの=新しいFileStreamを(pdfPath、FileMode.Open、FileAccess.Read)。
            バイト[] FILECONTENTS =新しいバイト[(INT)fs.Length]; 
            fs.Read(FILECONTENTS、0、fileContents.Length)。
            fs.Close(); 

            リターンファイル(FILECONTENTS、 "アプリケーション/ PDF"、 "test.pdfという"); 
        }

  2、itextsharpは、PDFを生成します

itextsharpを見つけ、アイテムを追加nuget

FileResult ItextSharpDemoのパブリック()
        { 
            文字列のファイル名= Server.MapPathの(@ "\一時ファイル\ PDF \ ItextSharpTest.pdf"); 

            iTextSharp.text.Rectangle新しい新しいiTextSharp.text.RectangleのpageSize =(1000年、500); 
            iTextSharp.text.Document =新しい新規iTextSharp.text.Document文書(pageSizeを、10、10、10、10); 
            PDFWriterのライター= PdfWriter.GetInstance(文書、新しい新規のFileStream(ファイル名、FileMode.Create)); 
            document.open(); 

            //セットされた原稿関連情報
            document.AddTitle( "これはタイトルである"); 
            document.AddSubject( "テーマ"); 
            document.AddKeywords( "キーワード"); 
            document.AddCreator( "作成者"); 
            ドキュメント。AddAuthor( "著者");

            //添加内容
            document.Add(新iTextSharp.text.Paragraph( "Hello Worldのこんにちは人々 !!" +) "这是中文"); 
            
            //添加图片
            iTextSharp.text.Image IMG = iTextSharp.text.Image.GetInstance(Server.MapPathの(@ "\画像\ 1.png")); 
            img.SetAbsolutePosition(100、50)。
            writer.DirectContent.AddImage(IMG)。

            IMG = iTextSharp.text.Image.GetInstance(Server.MapPathの(@ "\画像\ 2.png")); 
            img.SetAbsolutePosition(200、50)。
            writer.DirectContent.AddImage(IMG)。

            IMG = iTextSharp.text.Image.GetInstance(Server.MapPathの(@ "\画像\ 3.png")); 
            img.SetAbsolutePosition(300、50)。
            ライター。

            document.Close(); 
            writer.Close(); 

            FileStream fsの=新しいFileStreamを(ファイル名、FileMode.Open、FileAccess.Read)。
            バイト[] FILECONTENTS =新しいバイト[(INT)fs.Length]; 
            fs.Read(FILECONTENTS、0、fileContents.Length)。
            fs.Close(); 

            リターンファイル(FILECONTENTS、 "アプリケーション/ PDF"、 "test.pdfという"); 
        }

  ソースは、左下隅で、計算された位置は不便です

3、Rotativaは、HTMLおよびPDFのダウンロードを生成します。

Rotativaを見つけ、アイテムを追加nuget

公共のActionResult DemoViewAsPdf()
        { 
            新ViewAsPdf( "DemoViewAsPdf")を返します。
            新しいActionAsPdf( "DemoViewAsPdf")を返す// 
            // {ファイル名= "demo.pdfを"}。
        }

  4、PDFSharpは、PDFを生成しました

PDFSharpを見つけ、アイテムを追加nuget

/// <まとめ> 
        /// 1、Windowsフォント、エラー内部で使用する場合
        /// 2、デフォルトは中国をサポートしていません
        /// </要約> 
        公共ボイドCreatePDFは()
        { 
            //新しいPDFドキュメントの作成
            PdfDocumentドキュメントを新しい新しいPDFDocument =(); 

            //は空ページ作成
            PdfPageページ= document.AddPageを(); 

            //キャンバスを設定
            XGraphics GFX = XGraphics.FromPdfPage(ページ); 

            //フォントセット単位:PX 
            //System.Drawing.Text。 pfcFonts =新しい新System.Drawing.Text.PrivateFontCollection PrivateFontCollection(); 
            //文字列strFontPath = @ "C:/Windows/Fonts/msyh.ttc"; //フォントは、マイクロソフトエレガントな黒に設定されている
            //pfcFonts.AddFontFile(strFontPath)。

            // XPdfFontOptionsオプション=新しいXPdfFontOptions(PdfFontEncoding.Unicode、PdfFontEmbedding.Always)。
            // XFontフォント=新しいXFont(pfcFonts.Families [0]、15、XFontStyle.Regular、オプション)。

            System.Drawing.Text.PrivateFontCollection pfcFonts =新しいSystem.Drawing.Text.PrivateFontCollection(); 
            文字列strFontPath = @ "C:/Windows/Fonts/msyh.ttc"; //字体设置为微软雅黑
            pfcFonts.AddFontFile(strFontPath)。

            XPdfFontOptionsオプション=新しいXPdfFontOptions(PdfFontEncoding.Unicode、PdfFontEmbedding.Always)。
            XFontフォント=新しいXFont(pfcFonts.Families [0]、20、XFontStyle.Bold、オプション)。

            //设置(添加)文本 
            gfx.DrawString( "こんにちは、世界!"、フォント、XBrushes.Black、
              新しいXRect(0、0、page.Width、page.Height)、
              XStringFormat.TopLeft)。

            //图片
            文字列imgPath = Server.MapPathの(@ "\画像\ 1.png"); 

            XImage画像= XImage.FromFile(imgPath)。
            //二重X =(gfx.PageSize.Width - image.PixelWidth * 72 / image.Horizo​​ntalResolution)/ 2。
            //二重Y =(gfx.PageSize.Height - image.PixelHeight * 72 / image.VerticalResolution)/ 2。
            gfx.DrawImage(画像、10、30)。

            //设置(添加)文本
            //gfx.DrawString("123124121" 、フォント、XBrushes.Black、
            //新しいXRect(0、0、page.Width、page.Height)
            //)。 
            gfx.DrawStringを("これは、「中国語フォントのラインです、 XBrushes.Black、0、60 + image.PixelHeight); 

            //ドキュメントを保存
            文字列filename = Server.MapPathの(@ "\一時ファイル\ HelloWorld.pdf"); 
            document.Save(ファイル名); 
        }

  5、Spire.Pdf

Spire.Pdfを見つけ、アイテムを追加nuget

ボイドCreatePDF公開()
        { 
            PDFDocument文献PDFDocument新しい新=(); 
            
            //様々なサイズを変換するため
            PdfUnitConvertor unitCvtr新しい新しいPdfUnitConvertorは=(); 

            //余白設定用
            PdfMarginsマージンを新しいPdfMarginsは=()新規; 

            //設定ページ余白単位:ポンド/ポイント
            margins.Top = unitCvtr.ConvertUnits(20F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point); 
            margins.Bottom = margins.Top; 
            margins.Left = 0; 
            margins.Right = margins.Left; 
            
            //新しいA4サイズのページを追加し、A4サイズが211ミリメートル* 297ミリメートルである
            PdfPageBaseページ= document.Pages.Add(PdfPageSize.A4、余白); 
            
            //フォント、フォントサイズ、フォントはポンド単位でのフォントサイズを設定しました
            PdfTrueTypeFont titleFont =新しいPdfTrueTypeFont(新しいフォント ( " ゴシック"、unitCvtr.ConvertUnits(24F、真のPdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point))); 
            PdfTrueTypeFont contentFont新しい新しいPdfTrueTypeFont =(新しい新しいフォント( "ゴシック"、unitCvtr.ConvertUnits(14F trueに、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point))); 

            //フォントの色
            //のPDFpen中空単語
            //のPDFpen PEN =新しい新規のPDFpen(Color.Black); 
            PdfBrushブラシ=新しい新しいPdfSolidBrush(Color.Black); 

            //コンテンツを書き込み、xはyはポンドで、上記からの距離であり、ポンドで、左からの距離である
            文字列テキスト=(「ここでは、タイトル」); 
            page.Canvas.DrawString(テキスト、titleFont、ブラシ、unitCvtr。 ConvertUnits(14F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point)、0); 

            テキスト=( "ここでコンテンツ「)です。
            page.Canvas.DrawString(テキスト、contentFont、ブラシ、unitCvtr.ConvertUnits(14F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point)、unitCvtr.ConvertUnits(30F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point)); 

            ここにテキスト=(「コンテンツ2「); 
            page.Canvas.DrawString(テキスト、contentFont、ブラシ、unitCvtr.ConvertUnits(14F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point)、unitCvtr.ConvertUnits(50F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point)); 

            //プレス指定されたアドレスのロード画面
            PdfImageイメージ= PdfImage.FromFile(Server.MapPathの(@ "\ロイヤリティーフリーの\ 1.png")); 

            //ピクチャストリームによって負荷絵
            //画像のimg; 
            // PdfImage IMG = PdfImage.FromImage(IMG)

            負荷絵によって//ストリームストリーム
            //System.IO.Streamストリーム。
            IMG = PdfImage.FromStream PdfImage //(ストリーム)

            フロートimage.width *幅= 0.55F; 
            フロートimage.height *高さ= 0.55F; 

            フロートunitCvtr.ConvertUnits Y =((20F + 30F + 20F)、PdfGraphicsUnit.Pixel、 PdfGraphicsUnit.Point); 

            //画像を挿入し、xはポンドで、左からの距離であり、yはポンドで、上記からの距離であり、幅、ピクセルのライトPDF画像の幅と高さの高さ
            page.Canvas.DrawImage (画像、unitCvtr.ConvertUnits(14F、PdfGraphicsUnit.Pixel、PdfGraphicsUnit.Point)、y軸、幅、高さ); 

            //ドキュメントを保存して開く
            document.SaveToFile(Server.MapPathの(@ "\一時ファイル \ PDFの作成.PDF") )
            ; //System.Diagnostics.Process.Start("PDF)」.PDFを作成
        }

  Spire.Pdfは、私は、プラグインをテストするときに中国の文字化けが発生して、左上隅のソース、および単位変換ツールを提供しなかった唯一のものですので、個人的にSpire.Pdfを好みます

おすすめ

転載: www.cnblogs.com/zhoushangwu/p/11666637.html