Aspose.Pdf. Change sheet background for selected fragment

Hi.
You need to change the background of the sheet for the selected fragment of the document. The background should change in the selection, not in the entire sheet.

@PiterG

Can you please share a sample source and expected output PDF for our reference? We will test the scenario in our environment and address it accordingly.

I am programming Qt, C++. License Aspose.Total С++.

I can process in the selected area: text, image. But I need to change the background of a part of the page. For example, grey. I can change the background of an entire page of a document.

document->get_Pages()->idx_get( 1 )->set_Background( Aspose::Pdf::Color::get_Black() );

I only need parts of the document. Rectangle example:

auto rectangle = System::MakeObject< Aspose::Pdf::Rectangle >( 92, 183, 288, 313 );

The background change area is highlighted in the figure.

I don’t care if you give an example in any other programming language. I will be grateful for any help or idea.

Example.pdf (2.7 KB)

wish.png (29.6 KB)

@PiterG

As per our understandings, you want to highlight the background of particular text inside PDF document. You can achieve it using HighlightAnnotation. Please check the linked example for sample code and feel free to let us know in case you still are not able to achieve what you require. You can also check how to add a color filled rectangle inside PDF document:

1 Like

Hello!
I need to get document like in file. Impossible to implement through the creation of graphic shapes. Please tell me how to do it.resColored_Page_Background1.pdf (2.7 KB)

@PiterG

Please check the below code snippet and attached output PDF generated by it.

// C# Code
Document doc2 = new Document(dataDir + "example.pdf");
TextFragmentAbsorber tfa = new TextFragmentAbsorber(@"ClearRow\s+98\s+CreateTable\s+98\s+DeleteCol\s+99\s+DeleteRow\s+99", new TextSearchOptions(true));
doc2.Pages.Accept(tfa);
HighlightAnnotation ha = new HighlightAnnotation(doc2.Pages[1], tfa.TextFragments[1].Rectangle);
ha.Color = Color.FromArgb(255, 196, 212, 167);
doc2.Pages[1].Annotations.Add(ha);
doc2.Save(dataDir + "PDF_Highlighting.pdf");

PDF_Highlighting.pdf (41.6 KB)

In case it still does not meet your requirements, please let us know. We will log an investigation ticket and share the ID with you.

Thank you. This is an Annotation. I have already implemented it. I have a more complex problem. This is work with the background of the document. Take a look at the Background.pdf document. A clean sheet with a green background. The background was created in Aspose.Pdf. I cut out a fragment in it with the Foxid PDF Editor (white). In Aspose, I cannot implement this. This is the background of the document.Background.pdf (2.6 KB)

@PiterG

TextFragment also offers a property BackgroundColor which can be used to set its background. Please check if it suits you. e.g. TextFragment.TextState.BackgroundColor. In case it does not, please edit the “example.pdf” as per your desire at your end and share the file with us. That way, we will have an expected output PDF with respective input.

Thank you, I have it implemented. The presence of text in the document is not important.
I replaced the example.pdf file with Example.pdf.
I am interested in work:
document->get_Pages()->idx_get(i)->set_Background(color);
I need to recolor part of the background. On it, you do not need to draw a square, but recolor part of the background - in the form of a square or a stripe. I will be grateful for your help. You will also be interested in this opportunity. If I implement it, then I post the code on the Cpp.Example.pdf (2.7 KB)

@PiterG

We have logged a feature request as PDFCPP-2303 in our issue tracking system with all the details of your requirement. We will look into details and investigate the feasibility of this feature. We will let you know as soon as the ticket is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

1 Like

@PiterG

Please check the below code snippet for your requirements:

[C#]

public void BackGround()
{
    Document doc = new Document();
    Page page = doc.Pages.Add();

    int width = (int)page.Rect.Width;
    int height = (int)page.Rect.Height;

    System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
    System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
    gr.Clear(System.Drawing.Color.Green);
    // top left corner of the page is (0,0). default page width is 595 and height is 842
    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(470, 50, 100, 100);
    gr.FillRectangle(System.Drawing.Brushes.White, rect);

    MemoryStream imgStream = new MemoryStream();
    bmp.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);

    BackgroundArtifact background = new BackgroundArtifact();
    background.BackgroundImage = imgStream;
    page.Artifacts.Add(background);

    doc.Save("green_and_white.pdf");
}

[C++]

void BackGround()
{
    auto doc = System::MakeObject<Aspose::Pdf::Document>();
    auto page = doc->get_Pages()->Add();

    int width = (int)page->get_Rect()->get_Width();
    int height = (int)page->get_Rect()->get_Height();

    auto bmp = System::MakeObject<System::Drawing::Bitmap>(width, height);
    auto gr = System::Drawing::Graphics::FromImage(bmp);
    gr->Clear(System::Drawing::Color::get_Green());
    // top left corner of the page is (0,0). default page width is 595 and height is 842
    System::Drawing::Rectangle rect(470, 50, 100, 100);
    gr->FillRectangle(System::Drawing::Brushes::get_White(), rect);

    auto imgStream = System::MakeObject<System::IO::MemoryStream>();
    bmp->Save(imgStream, System::Drawing::Imaging::ImageFormat::get_Png());

    auto background = System::MakeObject<Aspose::Pdf::BackgroundArtifact>();
    background->set_BackgroundImage(imgStream);
    page->get_Artifacts()->Add(background);

    doc->Save(u"green_and_white.pdf");
} 
1 Like

Yes. Thank you.

Thanks for the solution.
Question for the code.
gr->FillRectangle(System::Drawing::Brushes::get_White(), rect);
I need to pass color to this function. But the color is stored in a type variable:
System::Drawing::Color.
Is it possible?

It is required to write: the color value of the variable b into the variable a (a <<<<< b):
System::Drawing::Brush a;
System::Drawing::Color b = System::Drawing::Color::get_Green();

@PiterG

We have logged this feedback under the ticket and will investigate feasibility. We will inform you soon once its resolved.

1 Like

@PiterG

Please check:

#include "drawing/solid_brush.h"

System::Drawing::Color color = System::Drawing::Color::FromArgb(0, 255, 255, 255);
System::SmartPtr<System::Drawing::Brush> brush = System::MakeObject<System::Drawing::SolidBrush>(color);

gr->FillRectangle(brush, rect);
1 Like