使用ABC PDF将PDF转换为灰度pdf

时间:2017-03-21 07:25:47

标签: abcpdf

我正在尝试使用Websupergoo ABCpdf将PDF转换为灰度(黑/白)PDF 我指的是

http://www.websupergoo.com/helppdfnet/source/8-abcpdf.operations/3-recoloroperation/1-methods/recolor.htm?q=recoloroperation

        Doc theDoc = new Doc();
        theDoc.Read(Server.MapPath("src.pdf"));
        int pages = theDoc.PageCount;
       MyOp.Recolor(theDoc, (WebSupergoo.ABCpdf8.Objects.Page)theDoc.ObjectSoup[theDoc.Page]); //Here problem
      theDoc.Save(Server.MapPath("greyscale1.pdf"));
        theDoc.Clear();

以上代码适用于单页PDf。

此代码仅转换PDF的第一页

当我尝试使用循环时,出现以下错误

Code With error

2 个答案:

答案 0 :(得分:0)

页码与abcPDF中的页面不同,因此您不能将页码用作对象汤的索引。

尝试这样的事情(未经测试):

int pages = theDoc.PageCount;
for(int i=0; i < pages; i++)
{
    theDoc.PageNumber = i;
    MyOp.Recolor(theDoc, (WebSupergoo.ABCpdf8.Objects.Page)theDoc.ObjectSoup[theDoc.Page]);
}

编辑:上面显然不起作用,但是当你链接到的文档显示时,有一个方法需要一个Doc对象而不是一个Page对象。如果您将MyOp.Recolor()方法更改为:

,这应该有效
public class MyOp
{
  public static void Recolor(Doc doc) {
    RecolorOperation op = new RecolorOperation();
    op.DestinationColorSpace = new ColorSpace(doc.ObjectSoup, ColorSpaceType.DeviceGray);
    op.ConvertAnnotations = false;
    op.ProcessingObject += Recoloring;
    op.ProcessedObject += Recolored;
    op.Recolor(doc);
  }
}

我不确定您在Recoloring()方法或Recolored()方法中正在做什么(或需要做什么),但这对于此处的更改无关紧要。

答案 1 :(得分:0)

因为我疯狂将PDF转换为灰度

c# printing through PDF drivers, print to file option will output PS instead of PDF

我找到了上面的答案(谢谢),但需要对每个人可能需要的内容进行一些纠正:

    Doc theDoc = new Doc();

    theDoc.Read("test.pdf");

    //doc.Rendering.ColorSpace = XRendering.ColorSpaceType.Gray;
    //doc.SaveOptions.

    //MyOp.Recolor(theDoc, (Page)theDoc.ObjectSoup[theDoc.Page]);

    int pages = theDoc.PageCount;
    for (int i = 0; i < pages; i++)
    {
        theDoc.PageNumber = i+1; // this is because numbering is from 1 :)
        MyOp.Recolor(theDoc, (Page)theDoc.ObjectSoup[theDoc.Page]);
    }

    theDoc.Save("out.pdf");

    theDoc.Clear();

该课程仍为其示例

   public class MyOp
    {
        public static void Recolor(Doc doc, Page page)
        {
            RecolorOperation op = new RecolorOperation();
            op.DestinationColorSpace = new ColorSpace(doc.ObjectSoup, ColorSpaceType.DeviceGray);
            op.ConvertAnnotations = false;
            op.ProcessingObject += Recoloring;
            op.ProcessedObject += Recolored;
            op.Recolor(page);
        }


            //public static void Recolor(Doc doc)
            //{
            //    RecolorOperation op = new RecolorOperation();
            //    op.DestinationColorSpace = new ColorSpace(doc.ObjectSoup, ColorSpaceType.DeviceGray);
            //    op.ConvertAnnotations = false;
            //    op.ProcessingObject += Recoloring;
            //    op.ProcessedObject += Recolored;
            //    op.Recolor(doc);
            //}


        public static void Recoloring(object sender, ProcessingObjectEventArgs e)
        {
            PixMap pm = e.Object as PixMap;
            if (pm != null)
            {
                ColorSpaceType cs = pm.ColorSpaceType;
                if (cs == ColorSpaceType.DeviceCMYK)
                    e.Cancel = true;
                e.Tag = cs;
            }
        }

        public static void Recolored(object sender, ProcessedObjectEventArgs e)
        {
            if (e.Successful)
            {
                PixMap pm = e.Object as PixMap;
                if (pm != null)
                {
                    ColorSpaceType cs = (ColorSpaceType)e.Tag;
                    if (pm.Width > 1000)
                        pm.CompressJpx(30);
                    else if (cs == ColorSpaceType.DeviceRGB)
                        pm.CompressJpeg(30);
                    else
                        pm.Compress(); // Flate
                }
            }
        }
    }

不要忘记使用(不是其他版本)并且像魅力一样。

using WebSupergoo.ABCpdf9.Objects;
using WebSupergoo.ABCpdf9.Operations;