![创建无打印页面比例的 PDF](https://linux22.com/image/1333218/%E5%88%9B%E5%BB%BA%E6%97%A0%E6%89%93%E5%8D%B0%E9%A1%B5%E9%9D%A2%E6%AF%94%E4%BE%8B%E7%9A%84%20PDF.png)
我需要生成一个 PDF 文档,其中页面缩放的打印选项设置为“无”或“实际大小”
文档中的此设置意味着,当您在 Adobe Reader 或其他 PDF 阅读器中打开文档时,默认设置将是“不缩小此页面以适合打印机边距”
在 Adobe Reader 10 中,我想要控制的设置位于“打印”对话框和“页面大小和处理”下的“大小选项”选项,我希望文档默认为“实际大小”而不是“缩小超大页面”。
我已经与我的消息来源进行了交流,并测试了使用 Adobe 标准版创建/保存 PDF 文档,您可以将文档的视图属性设置为该精确属性的默认值,而 Adobe Reader 将遵守该默认值,而不是使用 Adobe Reader 默认值 shink。
我使用 PDF Creator 1.5.0 来“打印”pfd 文档,该文档似乎允许将 PostScript 应用于文档。但是,如果其他 PDF 打印机适合我的目的并且有此选项,我可以使用它。我在 PDF Creator 设置中没有看到任何用于控制它的 GUI 选项。
我很确定我可以使用 ghostscript 来应用属性,但我需要知道如何去做。
更新:我一直在查看 Adobe pdf 规范,我正在寻找的文档属性似乎在 PDF 1.6 规范中。
12.2 查看器首选项 文档目录中的 ViewerPreferences 条目(参见 7.7.2,“文档目录”)指定查看器首选项词典 (PDF 1.2),用于控制文档在屏幕上或打印时的显示方式。如果没有指定这样的词典,则符合要求的读者应根据他们自己的当前用户首选项设置行事。表 150 显示了查看器首选项词典的内容。
PrintScaling -(可选;PDF 1.6)显示此文档的打印对话框时应选择的页面缩放选项。有效值为 None(表示无页面缩放)和 AppDefault(表示符合要求的阅读器的默认打印缩放)。如果此条目具有无法识别的值,则应使用 AppDefault。默认值:AppDefault。如果打印对话框被抑制并且其参数由其他来源提供,则仍应遵守此条目。
这正是我想要通过第三方 PDF 打印机软件或 ghostscript 进行控制的。我不是 PDF 专家,也不知道该怎么做。
更新:我使用 Pdf Creator 开发了一个解决方案,并将该解决方案编辑为可接受的答案。我还研究了以下内容,这些内容在使用 API 生成 pdf 时具有应用潜力。
实体文档 pdf
編輯- 还提供 C# 版本
答案1
/PrintScaling /None
这可以通过在字典中指定来实现ViewerPreferences
(参见第 12.2 节ISO 32000-1:2008。
更新: 尝试这个:
[ {Catalog} << /ViewerPreferences << /PrintScaling /None >> >> /PUT
pdfmark
看http://www.digipedia.pl/usenet/thread/1191/1835/
更新:来自 Poster
最终的解决方案是使用 PDF Creator 的“操作”功能将上述后记行添加到 PDF Creator 生成的后记中。(保存前的操作)
http://www.pdfforge.org/content/actions
我也用这个链接作为行动的灵感,并查看了预先安装的 vbs 脚本,以弄清楚需要做什么才能将信息添加到转换为 pdf 的 postscript 文件中。
PDFCreator 文档视图设置- 似乎 PdfCreator 中曾经包含类似的脚本。
无论如何,这是我的最终脚本,可以添加到 Pdf Creator 以添加 PrintScaling 默认属性。
' SetPrintScalingNone.vbs script
' Author: Collin Kulbacki
' Version: 1.0.0.0
' Date: Oct 18, 2012
' Comment: This script Sets the ViewerPrefferences Print Scalling to none.
Option Explicit
Const AppTitle = "PDFCreator - SetPrintScallingNone"
Const ForReading = 1, ForAppending = 8
Dim objArgs, objNetwork, section, ini, fso, f
Dim fname, key, psFile, strTitle, printScalingFile
Set objArgs = WScript.Arguments
If objArgs.Count = 0 Then
MsgBox "This script needs a parameter!", vbExclamation, AppTitle
WScript.Quit
End If
fname = objArgs(0)
Set ini = New IniFile
ini.Load fName, true
Set fso = CreateObject("Scripting.FileSystemObject")
printScalingFile = fso.GetParentFolderName(fName) & "\" & GenerateGUID & ".ps"
Set f = fso.OpenTextFile(printScalingFile, ForAppending, True)
f.writeline "[ {Catalog} << /ViewerPreferences << /PrintScaling /None >> >> /PUT pdfmark"
f.Close
Set objNetwork = CreateObject("WScript.Network")
set section = ini.AddSection(GenerateGUID)
section.AddKey("SessionId").Value = " "
section.AddKey("WinStation").Value = " "
section.AddKey("UserName").Value = objNetwork.UserName
section.AddKey("ClientComputer").Value = objNetwork.ComputerName
section.AddKey("SpoolFileName").Value = printScalingFile
section.AddKey("PrinterName").Value = " "
section.AddKey("JobId").Value = " "
section.AddKey("DocumentTitle").Value = "PrintScaling"
ini.Save fName, true
WScript.Quit
Private Function GenerateGUID()
GenerateGUID = Replace(Mid(CreateObject("Scriptlet.TypeLib").GUID, 2, 36), "-", "")
End Function
' http://www.codeproject.com/Articles/21896/INI-Reader-Writer-Class-for-C-VB-NET-and-VBScript
' IniFile class used to read a nd write ini files by loading the file into memory
Class IniFile
'List of IniSection objects keeps track of all the sections in the INI file
Private m_pSections, OpenAsUnicode
'Public constructor
Public Sub Class_Initialize()
Set m_pSections = CreateObject("Scripting.Dictionary")
m_pSections.CompareMode = vbTextCompare
End Sub
'Returns an array of the IniSections in the IniFile
Public Property Get Sections
Sections = m_pSections.Items
End Property
'Load IniFile object with existing INI Data
Public Sub Load( ByVal sFileName , ByVal bAppend )
Dim intAsc1Chr, intAsc2Chr
If Not bAppend Then RemoveAllSections() ' Clear the object...
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim tempsection : Set tempsection = Nothing
'Dim oReader : Set oReader = objFSO.OpenTextFile( sFileName, , , format )
Dim oReader, Stream
Set Stream = objFSO.OpenTextFile(sFileName, 1, False)
intAsc1Chr = Asc(Stream.Read(1))
intAsc2Chr = Asc(Stream.Read(1))
Stream.Close
' (255, 254 == FF, FE -> UniCode) Or (91, 0 == [ '\0' -> Widechar inf file from pdfcmon.dll)
If (intAsc1Chr = 255 And intAsc2Chr = 254) Or (intAsc1Chr = 91 And intAsc2Chr = 0) Then
OpenAsUnicode = True
Else
OpenAsUnicode = False
End If
Set oReader = objFSO.OpenTextFile( sFileName, 1, 0, OpenAsUnicode )
Dim regexsection : set regexsection = new regexp
Dim regexkey : Set regexkey = new regexp
Dim regexcomment : Set regexcomment = new regexp
regexcomment.Pattern = "^([\s]*#.*)"
regexcomment.Global = False
regexcomment.IgnoreCase = True
regexcomment.MultiLine = False
' Left for history
'regexsection.Pattern = "\[[\s]*([^\[\s].*[^\s\]])[\s]*\]"
'regexsection.Pattern = "^[\s]*\[[\s]*([^\[\s].*[^\s\]])[\s]*\][\s]*$"
regexsection.Pattern = "^\s*\[\s*(.*[^\s])\s*\]\s*$"
regexsection.Global = False
regexsection.IgnoreCase = True
regexsection.MultiLine = False
regexkey.Pattern = "^\s*([^=\s]*)[^=]*=(.*)"
regexkey.Global = False
regexkey.IgnoreCase = True
regexkey.MultiLine = False
While Not oReader.AtEndOfStream
Dim line : line = oReader.ReadLine()
If line <> "" Then
Dim m
If regexcomment.Test(line) Then
Set m = regexcomment.Execute(line)
'WScript.Echo("Skipping Comment " & m.Item(0).subMatches.Item(0) )
ElseIf regexsection.Test(line) Then
Set m = regexsection.Execute(line)
'WScript.Echo("Adding section [" & m.Item(0).subMatches.Item(0) &"]" )
Set tempsection = AddSection( m.Item(0).subMatches.Item(0) )
ElseIf regexkey.Test(line) And Not tempsection Is Nothing Then
Set m = regexkey.Execute(line)
'WScript.Echo("Adding Key ["& m.Item(0).subMatches.Item(0) &"]=["& m.Item(0).subMatches.Item(1) &"]" )
tempsection.AddKey( m.Item(0).subMatches.Item(0) ).Value = m.Item(0).subMatches.Item(1)
ElseIf Not tempsection Is Nothing Then
'WScript.Echo("Adding Key ["& line &"]" )
tempsection.AddKey( line )
'Else
' WScript.Echo("Skipping unknown type of data: " & line)
End If
End If
Wend
oReader.Close()
End Sub
'Allows you to do a save the IniFile resident in memory to file
Public Sub Save(ByVal sFileName, ByVal AsUnicode)
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim oWriter : Set oWriter = objFSO.CreateTextFile( sFileName , True, AsUnicode )
Dim s 'IniSection
Dim k 'IniKey
For Each s In Sections
'WScript.Echo("Writing Section: " & s.Name)
oWriter.WriteLine("[" & s.Name & "]")
For Each k In s.Keys
If k.Value <> vbNullString Then
'WScript.Echo("Writing Key: "&k.Name&"="&k.Value)
oWriter.WriteLine(k.Name & "="& k.Value )
Else
'WScript.Echo("Writing Key: "&k.Name)
oWriter.WriteLine(k.Name)
End if
Next
Next
oWriter.Close()
End Sub
'Returns the IniSection object associated with a section name
Public Function GetSection(ByVal sSection )
Set GetSection = Nothing
sSection = Trim(sSection) 'Trim spaces
If Len( sSection ) <> 0 Then
If m_pSections.Exists( sSection ) Then
Set GetSection = m_pSections.Item(sSection)
End If
End If
End Function
' Adds a section to the IniFile object, returns a IniSection object
Public Function AddSection(ByVal sSection )
Set AddSection = Nothing
If StrComp(TypeName(sSection),"IniSection",1) = 0 Then
If Not sSection Is Nothing Then
' Only purpose is to be used by child to re-insert
If Not sSection Is Nothing Then
If Not m_pSections.Exists( sSection.Name ) Then
Set m_pSections.Item( sSection.Name ) = sSection
Set AddSection = sSection
End If
End If
End If
ElseIf StrComp(TypeName(sSection),"String",1) = 0 Then
sSection = Trim(sSection)
If Len( sSection ) <> 0 Then
If m_pSections.Exists( sSection ) Then
Set AddSection = m_pSections.Item(sSection)
Else
Dim s : Set s = New IniSection
Call s.Init( Me , sSection )
Set m_pSections.Item(sSection) = s
Set AddSection = s
End If
End If
End If
End Function
' Removes all existing sections (clears the object)
Public Sub RemoveAllSections()
Call m_pSections.RemoveAll()
End Sub
' Remove a section by name or section object
Public Function RemoveSection(ByVal Obj)
RemoveSection = False
If StrComp(TypeName(Obj),"IniSection",1) = 0 Then
If Not Obj Is Nothing Then
m_pSections.Remove(Obj.Name)
RemoveSection = True
End If
ElseIf StrComp(TypeName(Obj),"String",1) = 0 Then
RemoveSection = RemoveSection( GetSection(Obj) )
End If
End Function
' Remove a key by section namd and key name
Public Function RemoveKey(ByVal sSection , ByVal sKey)
RemoveKey = False
Dim s : Set s = GetSection(sSection)
If Not s Is Nothing Then
RemoveKey = s.RemoveKey( sKey )
End If
End Function
' Returns a KeyValue in a certain section
Public Function GetKeyValue(ByVal sSection , ByVal sKey )
GetKeyValue = vbNullString
Dim s : Set s = GetSection(sSection)
If Not s Is Nothing Then
Dim k : Set k = s.GetKey(sKey)
If Not k Is Nothing Then
GetKeyValue = k.Value
End If
End If
End Function
' Sets a KeyValuePair in a certain section
Public Function SetKeyValue(ByVal sSection , ByVal sKey , ByVal sValue )
SetKeyValue = False
Dim s : Set s = AddSection(sSection)
If Not s Is Nothing Then
Dim k : Set k = s.AddKey(sKey)
If Not s Is Nothing Then
k.Value = sValue
SetKeyValue = True
End If
End If
End Function
' Renames an existing section returns true on success, false if the section didn't exist or there was another section with the same sNewSection
Public Function RenameSection(ByVal sSection , ByVal sNewSection)
' Note string trims are done in lower calls.
RenameSection = False
Dim s : Set s = GetSection(sSection)
If Not s Is Nothing Then
RenameSection = s.SetName(sNewSection)
End If
End Function
' Renames an existing key returns true on success, false if the key didn't exist or there was another section with the same sNewKey
Public Function RenameKey(ByVal sSection , ByVal sKey, ByVal sNewKey)
' Note string trims are done in lower calls.
RenameKey = False
Dim s : Set s = GetSection(sSection)
If Not s Is Nothing Then
Dim k : Set k = s.GetKey(sKey)
If Not k Is Nothing Then
RenameKey = k.SetName(sNewKey)
End If
End If
End Function
End Class
'IniSection Class
Class IniSection
' IniFile IniFile object instance
Private m_pIniFile
' Name of the section
Private m_sSection
' List of IniKeys in the section
Private m_keys
'Friend constuctor so objects are internally managed
Public Sub Class_Initialize
Set m_pIniFile = Nothing
m_sSection = ""
Set m_keys = CreateObject("Scripting.Dictionary")
m_keys.CompareMode = vbTextCompare
End Sub
' Function only works once...
Public Sub Init( ByVal oIniFile , ByVal sSection )
If m_pIniFile is Nothing Then
Set m_pIniFile = oIniFile
m_sSection = sSection
End If
End Sub
'Returns an array of the IniKeys in the IniFile
Public Property Get Keys
Keys = m_keys.Items
End Property
'Returns the section name
Public Property Get Name
name = m_sSection
End Property
'Set the section name
'Returns true on success, False if key already exists in the section
Public Function SetName(ByVal sSection)
SetName = False ' Default
sSection = Trim(sSection)
If Len( sSection ) <> 0 Then
Dim s : Set s = m_pIniFile.GetSection(sSection)
If Not s Is Me And Not s Is Nothing Then Exit Function
Call m_pIniFile.RemoveSection(Me)
m_sSection = sSection
Call m_pIniFile.AddSection(Me)
SetName = True
End If
End Function
'Returns the section name
Public Function GetName()
GetName = m_sSection
End Function
'Adds a key to the IniSection object
'Returns Nothing on failure
Public Function AddKey(ByVal sKey)
Set AddKey = Nothing
' Is this a string or object of IniKey
If StrComp(TypeName(sKey),"IniKey",1) = 0 Then
' Only purpose is to be used by child to re-insert
If Not sKey Is Nothing Then
If Not m_keys.Exists( sKey.Name ) Then
Set m_keys.Item(sKey.Name) = sKey
Set AddKey = sKey
End If
End If
ElseIf StrComp(TypeName(sKey),"String",1) = 0 Then
' String was passed...
sKey = Trim(sKey)
If Len(sKey) <> 0 Then
If m_keys.Exists( sKey ) Then
Set AddKey = m_keys.Item(sKey)
Else
Dim k : Set k = New IniKey
Call k.Init( Me , sKey )
Set m_keys.Item(sKey) = k
Set AddKey = k
End If
End If
End If
End Function
'Returns a IniKey
'Returns Nothing on failure
Public Function GetKey(ByVal sKey)
Set GetKey = Nothing
sKey = Trim(sKey)
If Len(sKey) <> 0 Then
If m_keys.Exists( sKey ) Then
Set GetKey = m_keys.Item(sKey)
End If
End If
End Function
'Removes all the keys in the section
Public Sub RemoveAllKeys()
Call m_keys.RemoveAll()
End Sub
'Removes a single key by IniKey object by string or object
Public Function RemoveKey(ByVal Obj)
RemoveKey = False
If StrComp(TypeName(Obj),"IniKey",1) = 0 Then
If Not Obj Is Nothing Then
m_keys.Remove(Obj.Name)
RemoveKey = True
End If
ElseIf StrComp(TypeName(Obj),"String",1) = 0 Then
RemoveKey = RemoveKey( GetKey(Obj) )
End If
End Function
End Class ' End of IniSection
'IniKey Class
Class IniKey
' Name of the Key
Private m_sKey
' Value associated
Private m_sValue
' Pointer to the parent CIniSection
Private m_pSection
'Friend constuctor so objects are internally managed
Public Sub Class_Initialize
m_sKey = ""
m_sValue = ""
Set m_pSection = Nothing
End Sub
'Returns the key's parent IniSection
Public Sub Init( ByVal oIniSection , ByVal sKey )
If m_pSection Is Nothing Then
Set m_pSection = oIniSection
m_sKey = sKey
End If
End Sub
'Returns the name of the Key
Public Property Get Name
name = m_sKey
End Property
' 'Gets\Sets the value associated with the Key
Public Property Let Value( strKeyValue )
m_sValue = strKeyValue
End Property
'Gets\Sets the value associated with the Key
Public Property Get Value()
value = m_sValue
End Property
'Sets the key name
'Returns true on success, fails if the section name sKeyName already exists
Public Function SetName(ByVal sKey )
SetName = False
sKey = Trim(sKey)
If Len(sKey) <> 0 Then
Dim s : Set s = m_pSection.GetKey(sKey)
If Not s Is Me And Not s Is Nothing Then Exit Function
Call m_pSection.RemoveKey(Me)
' Set our new name
m_sKey = sKey
' Put our own object back
Call m_pSection.AddKey(Me)
SetName = True
End If
End Function
' Returns the current key name
Public Function GetName()
GetName = m_sKey
End Function
End Class
我不能承担用于解析 PdfCreator 输出的 ini 文件类的功劳,该类是通过 PdfCreator 中现有的 vbs 脚本提供的。
答案2
检查 pdfmark 参考版本 9,我看不到任何影响查看器首选项的 pdfmark。因此,您无法使用 Ghostscript 来实现这一点,而无需修改 pdfwrite 设备。
Ghostscript 的 pdfwrite 设备根本不会在 Catalog 对象中发出 ViewerPreferences 字典,因此您需要添加一个。