.net打印pdf文件

.net打印pdf文件,第1张

方法一(web):window.print()

    print()方法是浏览器打印功能的一种程序调用。print方法用于打印当前窗口的内容亩答。

打印当前页:

function printPage(){

    window.print()

}

打印局部页面:

        前端页面:<iframe style="width:100%height:100%" id="fileId" src="文件路径">

        </iframe>

        <input type="button" name="print" id="print" value="打印" />

        js:$("#print").click(function () {

        var iframe = document.getElementById("fileId")

        iframe.contentWindow.print()

    })

方法二:调用系统API(得保证本地装有相关的软件)

PrintDocument pd = new PrintDocument()

            pd.PrinterSettings.PrinterName = "Microsoft Print to PDF"

            Process p = new Process

            {

                StartInfo = new ProcessStartInfo

                {

           迅蔽慧         CreateNoWindow = false,

                    WindowStyle = ProcessWindowStyle.Hidden,

                    UseShellExecute = true,

                    FileName = filePath,//文件路径

                    Verb = "print",

                    Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\""

                }

            }

            p.Start()

            p.WaitForExit()

方法三:spire打印方式(收费)下面是简单的使用例子

 var pdf = new PdfDocument(filePath)

//设置打印机

pdf.PrintSettings.PrinterName = "Microsoft Print to PDF"

pdf.print()

方法四:安装RawPrint

var printer = new Printer()

var file = File.Open(filePath, FileMode.Open)

byte[] array = new byte[file.Length]

file.Read(array, 0, array.Length)

printer.PrintRawStream(printerName, file, "打印机上显示的任务名")

file.Close()

printer.PrintRawFile(printerName, fileFullPath, "打印机上显示并段的任务名")

这个测试时虚拟打印机上正常,使用公司打印机时出现乱码问题以及打印任务不停的问题

因为有人问到,所以写了个例子。亏耐具体的要求是从.NET(比如C#)里面调用AutoCAD ActiveX API实现和空猛后台打印DWG文件为PDF文件,而且要把打印页面的大小设置成和DWG视图的页面的大小一致。当然除了ActiveX API,其它接口,比如ObjectARX和AutoCAD.NET API也支持打印并能实现上述功能的。不过我们今天就限定一下范围,用一用ActiveX API,而且指定产品是唤桥AutoCAD 2010吧。

执行步骤:打开一个dwg文件,用netload加载下面代码所在的.dll文件,再输入命令plottest,就得到输出结果(一个.pdf文件)。

要用到的参考:

AcDbMgd.dllAcMgd.dllAutoCAD 2010 Type LibrarySystem.Windows.FormsAutoCAD/ObjectDBX Common 18.0 Type Library.

VB.NET:

Imports System

Imports Autodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.Interop

Imports Autodesk.AutoCAD.Interop.Common

Imports Autodesk.AutoCAD.ApplicationServices

Imports Autodesk.AutoCAD.DatabaseServices

Imports Autodesk.AutoCAD.EditorInput

<Autodesk.AutoCAD.Runtime.CommandMethod("Plottest")>_

Public Sub PlotToPDF()

Dim activeDoc As Document = Application.DocumentManager.MdiActiveDocument

Dim ThisDrawing As AcadDocument = CType(activeDoc.AcadDocument, AcadDocument)

Dim layout As AcadLayout = ThisDrawing.ActiveLayout

Dim MediaName As String = layout.CanonicalMediaName

If MediaName.Equals("") Then

activeDoc.Editor.WriteMessage("There is no media set for the active layout.")

Return

Else

activeDoc.Editor.WriteMessage(("The media for the active layout is: " + MediaName))

End If

Try

Dim oplot As AcadPlotConfiguration = ThisDrawing.PlotConfigurations.Add("PDF", layout.ModelType)

oplot.PaperUnits = AcPlotPaperUnits.acMillimeters

oplot.StyleSheet = "monochrome.ctb"

oplot.PlotWithPlotStyles = True

oplot.ConfigName = "DWG To PDF.pc3"

oplot.UseStandardScale = True

oplot.StandardScale = AcPlotScale.acScaleToFit

oplot.PlotType = AcPlotType.acExtents

oplot.CenterPlot = True

Dim oMediaNames As Object = layout.GetCanonicalMediaNames

Dim mediaNames As ArrayList = New ArrayList(CType(oMediaNames, String()))

For Each sName As String In mediaNames

If sName.Contains(MediaName) Then

oplot.CanonicalMediaName = sName

layout.CopyFrom(oplot)

layout.PlotRotation = AcPlotRotation.ac0degrees

layout.RefreshPlotDeviceInfo()

ThisDrawing.SetVariable("BACKGROUNDPLOT", 0)

ThisDrawing.Plot.QuietErrorMode = True

ThisDrawing.Plot.PlotToFile("c:/temp/d1.pdf", "DWG To PDF.pc3")

oplot.Delete()

oplot = Nothing

Return

End If

Next

Catch es As System.Exception

System.Windows.Forms.MessageBox.Show(es.ToString)

End Try

End Sub

C#:

using System

using System.Collections

using System.Collections.Specialized

using Autodesk.AutoCAD.Runtime

using Autodesk.AutoCAD.DatabaseServices

using Autodesk.AutoCAD.ApplicationServices

using Autodesk.AutoCAD.EditorInput

using Autodesk.AutoCAD.Interop

using Autodesk.AutoCAD.Interop.Common

// Define Command "plotTest"

[CommandMethod("plotTest")]

static public void PlotToPDF()

{

Document activeDoc = Application.DocumentManager.MdiActiveDocument

AcadDocument ThisDrawing = activeDoc.AcadDocument as AcadDocument

AcadLayout layout = ThisDrawing.ActiveLayout

String MediaName = layout.CanonicalMediaName

if (MediaName.Equals(""))

{

activeDoc.Editor.WriteMessage("There is no media set for the active layout.")

return

}

else

{

activeDoc.Editor.WriteMessage("The media for the active layout is: " + MediaName)

}

try

{

AcadPlotConfiguration oplot = ThisDrawing.PlotConfigurations.Add("PDF", layout.ModelType)

oplot.PaperUnits = AcPlotPaperUnits.acMillimeters

oplot.StyleSheet = "monochrome.ctb"

oplot.PlotWithPlotStyles = true

oplot.ConfigName = "DWG To PDF.pc3"

oplot.UseStandardScale = true

oplot.StandardScale = AcPlotScale.acScaleToFit

oplot.PlotType = AcPlotType.acExtents

oplot.CenterPlot = true

Object oMediaNames = layout.GetCanonicalMediaNames()

ArrayList mediaNames = new ArrayList((string[])oMediaNames)

foreach (String sName in mediaNames)

{

if (sName.Contains(MediaName))

{

oplot.CanonicalMediaName = sName

layout.CopyFrom(oplot)

layout.PlotRotation = AcPlotRotation.ac0degrees

layout.RefreshPlotDeviceInfo()

ThisDrawing.SetVariable("BACKGROUNDPLOT", 0)

ThisDrawing.Plot.QuietErrorMode = true

ThisDrawing.Plot.PlotToFile("c://temp//d1.pdf","DWG To PDF.pc3")

oplot.Delete()

oplot=null

return

}

}

}

catch (System.Exception es)

{

System.Windows.Forms.MessageBox.Show(es.ToString())

}

}

输出结果:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12194530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存