1

我有一个由 Microsoft Word 生成的 PDF 文件。用户指定了黑色的“突出显示”颜色,以使文本看起来像一个黑盒子(并使文本看起来像是被编辑过的)。我想将黑框更改为黄色,以便突出显示文本。

理想情况下,我想在 Python 中执行此操作。

谢谢!

4

1 回答 1

2

选项 1:如果可以选择商业库,您可以使用Amyuni PDF Creator .Net轻松实现此功能,C# 代码如下所示:

using System.IO;
using Amyuni.PDFCreator;
using System.Collections;

//open a pdf document
FileStream testfile = new FileStream("test1.pdf", FileMode.Open, FileAccess.Read, FileShare.Read);
IacDocument document = new IacDocument(null);
document.Open(testfile, "");

//get the first page
IacPage page1 = document.GetPage(1);

//get all graphic objects on the page
IacAttribute attribute = page1.AttributeByName("Objects");

// listobj is an arraylist of objects
ArrayList listobj = (ArrayList)attribute.Value;

foreach (IacObject iacObj in listobj)
{
    //if the object is a rectangle and the background color is black then set it to yellow
    if ((IacObjectType)iacObj.AttributeByName("ObjectType").Value == (IacObjectType.acObjectTypeFrame && (int)obj.Attribute("BackColor").Value == 0)
    {
        obj.Attribute("BackColor").Value = 0x00FFFF; //Yellow   
    }
}

我想你可以把它翻译成 IronPython。
通常的免责声明适用于此建议

选项 2:如果商业库不是一个选项,并且您没有开发商业闭源应用程序,您可以尝试使用 iText 对页面内容进行一些不可靠的黑客攻击:

您可以尝试解码页面内容(有关详细信息,请参阅 iText 中的 ContentByteUtils 类),在每个填充运算符之前插入一个颜色选择运算符,然后重新保存文件。有关这些运算符的更多详细信息,请参阅 Adob​​e PDF 参考文档的表 4.10 路径绘制运算符。

操作数 f:填充路径,使用非零绕组数规则确定要填充的区域(请参阅第 232 页的“非零绕组数规则”)。

操作数 rg:将非描边颜色空间设置为 DeviceRGB,并将非描边颜色设置为指定值

操作数q:保存当前图形状态

操作数 Q:恢复保存的图形状态

因此,如果您的页面上有一系列运算符:

0.0 0.0 0.0 rg % Set nonstroking color to black
25 175 175 −150 re % Construct rectangular path
f % Fill path

它应该变成:

0.0 0.0 0.0 rg % Set nonstroking color to black
25 175 175 −150 re % Construct rectangular path
q % Saves the current graphic state
1.0 1.0 0.0 rg % Set nonstroking color to yellow
f % Fill path
Q % Restores the saved graphic state

一些评论:
-这种方法会将每个非文本绘图变成黄色(包括线条、曲线等,不包括光栅图像),并且它还将使用与其他 PDF 相同的绘图运算符在页面上绘制的任何文本绘制为黄色图纸。
- 页面上使用的 Xforms 和注释将不会被处理。
- 如果您要处理的文件是由同一个工具以同样的方式生成的,您可以只测试几个文件,看看它是如何进行的。

重要提示:这只是我头脑中未经测试的想法,它可能有效,也可能无效。

于 2013-02-20T16:42:04.047 回答