Converting PDF to Grayscale pdf using ABC PDF

谁说我不能喝 提交于 2019-12-25 18:35:15

问题


I am trying convert PDF to grayscale(Black/White) PDF using Websupergoo ABCpdf.
I am referring

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();

Above code works fine for single page PDf.

This Code Converts only first page of PDF

When I tried to use a loop the below error is occurring


回答1:


Page Number is not the same as Page in abcPDF, so you cannot use the page number as an index into the object soup.

Try something like this instead (untested):

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]);
}

Edit: The above apparently didn't work, but as the documentation you linked to shows, there's a method that takes a Doc object instead of a Page object. This should work if you change your MyOp.Recolor() method to this:

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);
  }
}

I am not sure what you are doing (or need to do) in the Recoloring() method or Recolored() method, but that should not matter for the changes here.




回答2:


Since I went crazy with converting PDF to grayscale here

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

I found above answer (thank you) but needs to be corrected a little bit for everyone may need:

    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();

The class remains as in their example

   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
                }
            }
        }
    }

Don't forget to use (not other version) and works like a charm.

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


来源:https://stackoverflow.com/questions/42920706/converting-pdf-to-grayscale-pdf-using-abc-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!