Aspose.Pdf. С++. Set text transparency

Hi.
Make text fragment transparent.
System::SmartPtrAspose::Pdf::Text::TextFragment textFragment;
Thank you.

@PiterG

Can you please share a PDF document as well that contains transparent text? Were you able to achieve it using Adobe Reader? Please share the sample PDF with us so that we can further investigate this feature.

Problem.

I have a document - example1.pdf

I have chosen an area - 1.png. The text in the region should be removed and the region’s size not reduced.

If I replace the characters with an empty character ‘ ‘ (“space”) . I get - 2.png. The area has decreased by 2 times.

If I do the replacement with the "" character. I have - 3.png. Selection size saved. But the '’ character is visible.

It needs to be transparent (not visible). Or which symbol to replace with, so that the area does not decrease and the symbol is not visible.

example1.pdf (347.9 KB)
1.jpg (127.3 KB)
2.jpg (105.3 KB)
3.jpg (125.1 KB)

@PiterG

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFCPP-2358

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you. We have a full package and paid technical support. These are organizational issues.

@PiterG

In case you have paid support subscription, you can create a topic there with the reference of ticket ID logged here and your issue will be escalated to the highest priority.

How should I specify: Paid support. Designed by the business owner.

@PiterG

You need to login to helpdesk in order to specify the paid support ticket. If you have paid support subscription, you can login there using the same email address that is used to purchase the subscription.

@PiterG

We may suggest using TextFragmentState::set_Invisible like in the example below

#include <iostream>
#include "Aspose.PDF.Cpp/Document.h"
#include "Aspose.PDF.Cpp/Page.h"
#include "Aspose.PDF.Cpp/PageCollection.h"
#include "Aspose.PDF.Cpp/Text/TextState.h"
#include "Aspose.PDF.Cpp/Text/TextFragment.h"
#include "Aspose.PDF.Cpp/Text/TextFragmentAbsorber.h"
#include "Aspose.PDF.Cpp/Text/TextFragmentCollection.h"
#include "Aspose.PDF.Cpp/Text/TextFragmentState.h"
#include "Aspose.PDF.Cpp/Text/TextOptions/TextSearchOptions.h"
#include "Aspose.PDF.Cpp/Rectangle.h"
#include "system/enumerator_adapter.h"

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

int main()
{
    auto doc = MakeObject<Document>(u"example1.pdf");

    auto fa = MakeObject<TextFragmentAbsorber>();
    auto so = fa->get_TextSearchOptions();

    so->set_LimitToPageBounds(true);
    so->set_Rectangle(MakeObject<Rectangle>(80, 600, 300, 100));

    doc->get_Pages()->idx_get(1)->Accept(fa);

    for(auto&& tf : fa->get_TextFragments())
    {
        std::cout << tf->get_Text() << std::endl;
        tf->get_TextState()->set_Invisible(true);
    }
    doc->Save(u"example2.pdf");
} 

example2.pdf (15.9 KB)

1 Like

Thank you!
I have a problem with the right border of the region when clearing.
Region so->set_Rectangle(MakeObject(80, 600, 300, 100)); - decreases (300) when clearing the text. And it depends on the font of the text.
for(auto&& tf : fa->get_TextFragments())
{
tf->get_TextState()->set_Invisible(true);
System::String string = tf->get_Text();
for( int i = 0; i < string.get_Length(); i++)
string.SetCharAt( i, ’ ’ ); // space
tf->set_Text( string );
}
How to remove the text so that visually the area remains the same width (80 - 300) and does not depend on the font.
When replaced with ‘_’ (bottom line) the situation is similar.

@PiterG

We have updated the ticket information in order to investigate against your feedback.

We request you please create a topic in our Purchase forum where you can have assistance in this regard.

@PiterG

The following example demonstrates how to replace text in fragments with spaces to keep the exact width of the fragment

    for(auto&& tf : fa->get_TextFragments())
    {
        String text = tf->get_Text();
        auto ts = tf->get_TextState();
        double fragment_width = ts->MeasureString(text);
        double space_width = ts->MeasureString(u" ");
        int number_of_spaces = (int)(fragment_width / space_width);

        std::cout << "fragment ='" << text << "', fragment_width =" << fragment_width << ", space_width=" << space_width << ", number_of_spaces=" << number_of_spaces << std::endl;
        tf->set_Text(String(u' ', number_of_spaces));
    } 
1 Like

Good afternoon!
Thanks for the help.
With your help and the Aspose library works great.
I have a problem when working with some files:
example3.pdf file.
It is necessary to replace all characters in the selected text with the specified one.
Selection example: image1, image2. File with result result.png and result.pdf.
Problem: when replacing - spaces between characters; string has grown in size.
I understand why, but I don’t know how to solve it.
Help is needed.

    for( auto textFragmens : textFragmentAbsorber->get_TextFragments() )
     {
        System::String string = textFragmens->get_Text();
        textFragmens->get_TextState()->set_Invisible( false );
        for( int i = 0; i < string.get_Length(); i++)
           if( string[i] != ' ' )
              string.SetCharAt( i, nSymbol );
        textFragmens->set_Text( string );
        textFragmens->get_TextState()->set_ForegroundColor( colorText );
        textFragmens->get_TextState()->set_BackgroundColor( colorFill );
     }

image2.png (137.1 KB)
result.pdf (40.0 KB)
result.png (136.7 KB)
image1.png (130.5 KB)

@PiterG

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFCPP-2390

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

@PiterG

It is likely there is an issue in Aspose.PDF for .NET API. Created task PDFNET-54940 in order to address it. Since, we are not clearly aware of your target, we can suggest you please have a look on redaction annotation.

The issues you have found earlier (filed as PDFNET-54940) have been fixed in Aspose.PDF for .NET 23.7.

@PiterG

The issue is fixed in Aspose.PDF for C++ 23.7. The next example works pretty well.

#include "Aspose.PDF.Cpp/Document.h"
#include "Aspose.PDF.Cpp/Page.h"
#include "Aspose.PDF.Cpp/PageCollection.h"
#include "Aspose.PDF.Cpp/Generator/Paragraphs.h"
#include "Aspose.PDF.Cpp/Text/TextFragment.h"
#include "Aspose.PDF.Cpp/Text/TextFragmentAbsorber.h"
#include "Aspose.PDF.Cpp/Text/TextFragmentCollection.h"
#include "Aspose.PDF.Cpp/Text/TextFragmentState.h"
#include "Aspose.PDF.Cpp/Text/TextOptions/TextReplaceOptions.h"
#include "Aspose.PDF.Cpp/SaveFormat.h"
#include "Aspose.PDF.Cpp/Color.h"
#include "Aspose.PDF.Cpp/PdfLicense.h"

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

int main()
{
    auto lic = MakeObject<License>();
    lic->SetLicense(u"aspose.pdf.c++.lic");

    auto colorText = Color::get_Black();
    auto colorFill = Color::get_Yellow();
    char_t nSymbol = 'x';

    auto doc = MakeObject<Document>(u"result.pdf");
    auto textFragmentAbsorber = MakeObject<TextFragmentAbsorber>(u"Rumata");
    doc->get_Pages()->idx_get(1)->Accept(textFragmentAbsorber);

    for (auto&& textFragment : textFragmentAbsorber->get_TextFragments())
    {
        System::String string = textFragment->get_Text();
        for (int i = 0; i < string.get_Length(); i++)
            if (string[i] != ' ')
                string.SetCharAt(i, nSymbol);

        textFragment->set_Text(string);
        textFragment->get_TextState()->set_ForegroundColor(colorText);
        textFragment->get_TextState()->set_BackgroundColor(colorFill);
    }
    doc->Save(u"result1.pdf");
}
1 Like