Home > Net >  C# .net How to rename/change the spacecolor name in the pdf
C# .net How to rename/change the spacecolor name in the pdf

Time:01-06

i want to add the suffix such as "_KM001" in the color name of a pdf,pls look at the img of the pdf object struct.How to find the ColorSpace object? And How to find this /Separation array? And I want to change the color name of "PANTONE Cool Gray 10 C"and "Black"?The result of the color name is "Cool Gray 10 C_KM001"and "Black_KM001".Thanks all bosses.

enter image description here

CodePudding user response:

This depends on the PDF library you are using.

The code below shows how to accomplish this task using PDF4NET library:

public static void SetColorantNameSuffix(string inputFileName, string outputFileName, string[] colorantNamesToReplace, string suffix)
{
    PDFFixedDocument document = new PDFFixedDocument(inputFileName);
    for (int i = 0; i < document.Pages.Count; i  )
    {
        PDFCosDictionary resourcesDictionary = document.Pages[i].CosDictionary[PDFNames.Resources] as PDFCosDictionary;
        if (resourcesDictionary != null)
        {
            SetColorspaceNameSuffixInResources(resourcesDictionary, colorantNamesToReplace, suffix);
        }
    }
    document.Save(outputFileName);
}

private static void SetColorspaceNameSuffixInResources(PDFCosDictionary resourcesDictionary, string[] colorantNamesToReplace, string suffix)
{
    PDFCosDictionary colorspaceResourcesDictionary = resourcesDictionary[PDFNames.ColorSpace] as PDFCosDictionary;
    if (colorspaceResourcesDictionary != null)
    {
        string[] csIDs = colorspaceResourcesDictionary.Keys;
        foreach (string csID in csIDs)
        {
            PDFCosArray csArray = colorspaceResourcesDictionary[csID] as PDFCosArray;
            if ((csArray != null) && (csArray.Length == 4))
            {
                PDFCosName csName = csArray[0] as PDFCosName;
                if (PDFNames.Separation.Equals(csName))
                { 
                    PDFCosName csColorantName = csArray[1] as PDFCosName;
                    if ((csColorantName != null) && IsReplaceable(csColorantName.Value, colorantNamesToReplace))
                    {
                        csColorantName.Value = csColorantName.Value   suffix;
                    }
                }
            }
        }
    }

    PDFCosDictionary resourcesXObjectDictionary = resourcesDictionary[PDFNames.XObject] as PDFCosDictionary;
    if (resourcesXObjectDictionary != null)
    {
        string[] xObjectIDs = resourcesXObjectDictionary.Keys;
        foreach (string xObjectID in xObjectIDs)
        {
            PDFCosDictionary xObjectDictionary = resourcesXObjectDictionary[xObjectID] as PDFCosDictionary;
            if (xObjectDictionary != null)
            {
                PDFCosDictionary xObjectResourcesDictionary = xObjectDictionary[PDFNames.Resources] as PDFCosDictionary;
                if (xObjectResourcesDictionary != null)
                {
                    SetColorspaceNameSuffixInResources(xObjectResourcesDictionary, colorantNamesToReplace, suffix);
                }
            }
        }
    }
}

private static bool IsReplaceable(string colorantName, string[] colorantNamesToReplace)
{
    foreach (string name in colorantNamesToReplace) 
    { 
        if (name == colorantName)
        { 
            return true; 
        }
    }

    return false;
}

The method SetColorantNameSuffix is called like this:

string[] colorantNamesToReplace = { "/Black", "/PANTONE Cool Gray 10 C" };
SetColorantNameSuffix("sample.pdf", "sample_replaced.pdf", colorantNamesToReplace, "_KM001");

Disclaimer: I work the company that develops PDF4NET.

  • Related