We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add Background Color to Layer

Hi Aspose,

Pdf Layers are transparent as they are rendered on top of each other but is there a way I can set a background color or add a solid content object to a layer that occupies the whole size of the page? making it appear like a page?

Thanks

Update: I have tried using images as my layer content. Aspose turns white background of my png files transparent rending this solution also useless.

@sbwos

Would you kindly share a sample PDF along with expected output document. We will test the scenario in our environment and address it accordingly.

I have sent you the files via pm.

@sbwos

Thanks for sharing sample files.

Would you kindly share the sample code snippet as well which you are trying to process them. This would help us testing the scenario accordingly and share our feedback.

Document docA = new Document(fileAPath);
Document docB = new Document(fileBPath);
Document resultDoc = new Document();
int pageCount = Math.Min(docA.Pages.Count, docB.Pages.Count);

for (int i = 1; i <= pageCount; i++)
{
    Page docPage = resultDoc.Pages.Add();
    docPage.PageInfo.Width = docB.Pages[i].CropBox.Width;
    docPage.PageInfo.Height = docB.Pages[i].CropBox.Height;
    docPage.PageInfo.IsLandscape = docB.Pages[i].PageInfo.IsLandscape;
    docPage.PageInfo.Margin.Bottom = 0;
    docPage.PageInfo.Margin.Top = 0;
    docPage.PageInfo.Margin.Left = 0;
    docPage.PageInfo.Margin.Right = 0;

    docPage.Layers = new List<Layer>();

    Layer layerA = new Layer("A", "Dokument 1");
    layerA.Contents.AddRange(docA.Pages[i].Contents.OfType<Operator>());
    docPage.Layers.Add(layerA);
    
    Layer layerB = new Layer("B", "Dokument 2");
    layerB.Contents.AddRange(docB.Pages[i].Contents.OfType<Operator>());
    docPage.Layers.Add(layerB);

    Aspose.Pdf.Rectangle docReact = new Aspose.Pdf.Rectangle(0, 0, docB.Pages[i].CropBox.Width, docB.Pages[i].CropBox.Height);
    docPage.MediaBox = docReact;
}

// embed all external fonts from previous documents to result document
List<Aspose.Pdf.Text.Font> fonts = new List<Aspose.Pdf.Text.Font>();
foreach (Aspose.Pdf.Text.Font oldFont in docA.FontUtilities.GetAllFonts())
{
    if (!fonts.Contains(oldFont))
    {
        fonts.Add(oldFont);
    }
}
foreach (Aspose.Pdf.Text.Font oldFont in docB.FontUtilities.GetAllFonts())
{
    if (!fonts.Contains(oldFont))
    {
        fonts.Add(oldFont);
    }
}

TextFragment textFragment = new TextFragment();
foreach (Aspose.Pdf.Text.Font newFont in fonts)
{
    TextSegment textSegment = new TextSegment("  ");
    TextState textState = new TextState();
    textState.Font = newFont;
    textState.Font.IsEmbedded = true;
    textSegment.TextState = textState;
    textFragment.Segments.Add(textSegment);
}
resultDoc.Pages[1].Paragraphs.Add(textFragment);
resultDoc.Save(savePath);

This code will generate two layers and both will be transparent, I would like to have a solid background/object attached to the layer making any background layers invisible.

@sbwos

Thanks for sharing requested code snippet.

We have tested the scenario in our environment with Aspose.PDF for .NET and were able to notice the issue that you have reported. Adding Background Color to the layer inside PDF is more like an enhancement and it has been logged as PDFNET-47320 in our issue tracking system. We will investigate feasibility of this requirement and keep you posted on the status of its implementation. Please be patient and spare us little time.

We are sorry for the inconvenience.

While it would be nice to have such a property, I think we can draw an operator (rectangle, fill etc.) on a layer mimicking background property. I am struggling with creating custom operator for this task.

Is it possible? if yes, how should I create my operator?

Thanks.

I solved this issue by creating a 1x1 pixel white image and stretching it to my layer boundaries. As expected if this image is added first, it hides anything behind the layer but what’s in front remains visible. If you can suggest a better approach, I am still all ears.

@sbwos

It is really good to know that you were able to replicate the issue by using a workaround. As issue is still logged in our issue tracking system, we will surely investigate for a better way to achieve your requirements and share with you as soon as investigation is completed. Please spare us some time.