如何更改PDF/PostScript文件中的线宽 [英] How to change the width of lines in a PDF/PostScript file

查看:75
本文介绍了如何更改PDF/PostScript文件中的线宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要处理技术图纸(机械工程),它们通常带有彩色线条,无法正确打印.因此,我写了下面列出的批处理脚本.它使用ghostscript将工程图转换为黑白图.白色的;请注意,灰度是不够的,因为在激光打印上可能几乎看不到灰度.批处理脚本甚至可以处理拖放到其上的多个文件.

I regularly need to handle technical drawings (mechanical engineering) and they often come with colored lines, which can't be printed properly. Therefore I wrote the batch script listed below. It uses ghostscript to convert drawings to black & white; note that greyscales aren't sufficient, as they might be barely visible on a laser print. The batch script even handles multiple files dragged and dropped on it.

现在,我遇到了大量的图纸,这些图纸似乎具有细线的线宽–在激光打印品上几乎看不到.

我试图采用该脚本,但无济于事!

I tried to adopt the script but to no avail!

我尝试过

-c "save /zz {setlinewidth} bind def /setlinewidth { 10 mul zz} bind def"

-c "/setlinewidth { 10 } bind"

还有其他

ghostscript更改线宽的正确参数是什么?

What are the correct parameters for ghostscript to change the linewidths?

我还附加了用于实验的PostScript文件.

I also attached the PostScript file I use to experiment with.

非常感谢您!

L.

::  PDFmono.bat
@echo off
if [%1]==[] goto :eof

set GS_EXEC=gswin64c.exe

:loop

set INPUT_FILE=%1
set INTERMEDIATE_FILE=%temp%\%~n1.ps
set OUTPUT_FILE=%~dp1\%~n1_mono%~x1
echo ==============================================================================
echo Converting PDF to monochrome:
echo %INPUT_FILE%
echo ------------------------------------------------------------------------------

%GS_EXEC% ^
    -sOutputFile="%INTERMEDIATE_FILE%" ^
    -sDEVICE=ps2write ^
    -dNOPAUSE -dBATCH -dSAFER ^
    "%INPUT_FILE%"

%GS_EXEC% ^
    -sOutputFile="%OUTPUT_FILE%" ^
    -sDEVICE=pdfwrite ^
    -c "/setrgbcolor { pop pop pop 0 setgray } def /setcmykcolor { pop pop pop pop 0 setgray } def /sethsbcolor { pop pop pop 0 setgray } def" ^
    -dNOPAUSE -dBATCH -dSAFER ^
    "%INTERMEDIATE_FILE%"

del "%INTERMEDIATE_FILE%"
echo ------------------------------------------------------------------------------
echo Done! Monochrome PDF saved as
echo %OUTPUT_FILE%
echo ==============================================================================

shift
if not [%1]==[] goto loop

推荐答案

PDF和PostScript是不同的东西,我不清楚您将哪个用作输入.如果是PDF,则基本上无法更改线宽.如果是PostScript,则可以在一定限制内.

PDF and PostScript are different things, and it isn't clear to me which you are using as your input. If it's PDF then you basically can't change the width of lines. If it's PostScript then, within certain limits, you can.

首先要注意的是,PostScript程序完全有可能(并非完全不常见)故意破坏您正在执行的改变颜色的操作员重新定义.程序可以直接从systemdict显式加载运算符,这将绕过您对运算符的重新定义.您的定义将存储在userdict中(因为您尚未指定其他字典).您不能覆盖systemdict中的定义,因为那是只读的.您可以使用 -dWRITESYSTEMDICT ,但我强烈建议您不要使用,因为这是一个巨大的安全漏洞.

First thing to note is that it's entirely possible (and not totally uncommon) for PostScript programs to deliberately defeat the kind of operator re-definition you are performing to change colour. A program can explicitly load the operators directly from systemdict, which will bypass your redefinition of the operators. Your definitions will be stored in userdict (because you haven't specified any other dictionary). You can't override the definitions in systemdict because that is read-only. You could use -dWRITESYSTEMDICT but I'd strongly recommend that you don't, because that's a massive security hole.

重新定义setlinewidth无效的原因几乎可以肯定是由于PostScript程序将执行类似 0 setlinewidth 之类的事实.在PostScript中将其定义为在设备上绘制最窄的线,通常为一个像素宽.

The reason your redefinition of setlinewidth doesn't work is almost certainly due to the fact that the PostScript program will be executing something like 0 setlinewidth. This is defined in PostScript as drawing the narrowest possible line on the device, usually one pixel wide.

看两个例子:

将0乘以10会得出...零!这不会改变线宽.请注意,您还执行了保存,但没有执行恢复,这很浪费.您不需要保存.

Multiplying 0 by 10 results in... zero! Which doesn't alter the line width. Note that you've also executed save, but not restore, which is wasteful. You don't need the save.

在第二个示例中,您将原始参数留在了堆栈上,因此有可能破坏程序执行,并且由于没有调用系统setlinewidth,因此实际上并未更改图形状态.这也将10留在堆栈上.

In the second example you've left the original parameter on the stack, so potentially corrupted the program execution, and you haven't actually altered the graphics state because you haven't called the system setlinewidth. Which also leaves the 10 on the stack.

类似的东西:

%% Grab a copy of the current setlinewidth
%%
/system_setlinewidth /setlinewidth load def

%% Define a version of setlinewidth which expands hairlines
%%
/setlinewidth {
  dup            %% copy the linewidth parameter
  0 eq           %% Test if its zero, defined as a hairline (consumes the copy)
  {
    pop 10       %% discard the linewidth and replace it with 10
  }
  system_setlinewidth  %% Still need to actually execute the setlinewidth
} bind def

可能会起作用.请注意,我尚未对此进行测试.

Would probably work. Note that I haven't tested this.

该问题对于Ghostscript和PostScript都是完全主题化的,因为它涉及PostScript编程问题.对于"monchrome","cmd",批处理文件"和(可能是)PDF,它不是主题.

The question is entirely on-topic for both Ghostscript and PostScript since it involves a PostScript programming question. Its not on-topic for 'monchrome', 'cmd', 'batch-file' and (probably) PDF.

虽然没有办法将这种欺骗手段应用到带有Ghostscript的PDF文件中,但同样存在潜在的问题.零宽度线定义为细线,无论设备的分辨率如何,其宽度始终为一个像素.如果可以的话,您应该建议创建这些文件的人员不要这样做,除非他们有充分的理由!

There is no way to apply this trickery to a PDF file with Ghostscript though, and the same potential problem applies; zero width lines are defined as hairlines and are always one pixel wide, no matter what the resolution of the device. If you can you should recommend to the people creating these files that they don't do that unless they have an awfully good reason!

这篇关于如何更改PDF/PostScript文件中的线宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆