Unable to manage Zorderof text and filled Path Shapes


#1

I have a solution that requires shaded regions to be placed in the background (behind) of text.

I am unable to manage the Zorder of the objects using the Zindex property when I am creating objects. (the text is obfuscated by the filled path regions)

Here is a sample snippet of code . this code writes shaded paths and then writes text within the shaded path region.

public static void ExecValidateZorder()
{
Aspose.Pdf.Document doc = new Document();
Aspose.Pdf.Page p = doc.Pages.Add();
p.PageInfo.Height = 11.0f * 72.0f;
p.PageInfo.Width = 8.5f * 72.0f;
p.PageInfo.Margin.Bottom = 0.0f;
p.PageInfo.Margin.Top = 0.0f;
p.PageInfo.Margin.Left = 0.0f;
p.PageInfo.Margin.Right = 0.0f;
WriteShadedRect(Convert.ToSingle( p.PageInfo.Width),Convert.ToSingle( p.PageInfo.Height), 100, 100, 180, 110, 170, 300, 50, 310, p.Paragraphs);
WriteShadedRect(Convert.ToSingle(p.PageInfo.Width), Convert.ToSingle(p.PageInfo.Height), 10, 20, 400,15, 420, 110, 5, 200, p.Paragraphs);
WriteMultiLineLText(p,
“THIS STRING OF TEXT WILL BE WRAPPED TO MULTIPLE LINES TO FIT INTO A MULTI-LINE TEXT CONFIGURATION”, 120, 120, 150, 100);
doc.Save(“C:\\temp\zordertext.pdf”);
}
private static void WriteShadedRect(float pageWidth, float pageHeight, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, Aspose.Pdf.Paragraphs ps)
{
var g = new Graph(pageWidth, pageHeight)
{
IsChangePosition = false,
ZIndex = 0,
};
var newPath = new Aspose.Pdf.Drawing.Path();
newPath.Shapes.Add(new Aspose.Pdf.Drawing.Line(new[]
{
x1,y1,x2,y2,x3,y3,x4,y4,x1,y1
}));
newPath.GraphInfo.Color = Color.Transparent;
newPath.GraphInfo.FillColor = Color.FromArgb(241, 241, 241);
g.Shapes.Add(newPath);
ps.Add(g);
}
private static void WriteMultiLineLText(Page p, string text, float x1, float y1, float width, float height)
{
var txtBldr = new TextBuilder§;
var txtPara = new TextParagraph
{
SubsequentLinesIndent = 0.0f,
FormattingOptions = {WrapMode = TextFormattingOptions.WordWrapMode.ByWords},
Rectangle = new Aspose.Pdf.Rectangle(x1, y1, x1 + width, y1 + height)
};
Aspose.Pdf.Text.TextFragment tf = new TextFragment(text) { ZIndex = 2 };
tf.TextState.Font = FontRepository.FindFont(“Arial”);
tf.TextState.FontSize = 12.4f;
tf.TextState.ForegroundColor = Color.Black;
tf.TextState.FontStyle = FontStyles.Regular;
tf.TextState.HorizontalAlignment = HorizontalAlignment.Center;
tf.TextState.HorizontalScaling = 90.5f;
tf.TextState.LineSpacing = 1.0f;
txtPara.AppendLine(tf);
txtBldr.AppendParagraph(txtPara);
}

I have attached the resulting pdf from this code.

I have also attached the resulting pdf from some more complex code that exhibits the same behavior. This code creates reports and utilzes shaded regions to highlight content.

I am in the process of replacing an existing pdf solution with Aspose. I have attached a pdf copy of what the pdf should look like

zordertext.pdf (48.4 KB)

Reportoutput.pdf (62.5 KB)

Reportoutput_itext.pdf (297.3 KB)
[/quote]


#2

@csludtke

Thank you for contacting support.

You may try to set background color of whole page or you may color a specific cell of a table as per your requirements. In case you need to add some color and some text in a rectangular area then you may also try working with FloatingBox Class which allows you to set background color as well as adding text at the same time. This approach will comparatively be simpler as FloatingBox can have absolute positioning as compared to relative positioning in cases of Graph, Image etc.

We hope suggested approach will be helpful. Please feel free to contact us if you need any further assistance.


#3

If I have no other option, I will see what I can do with the FloatingBox Class. The application I am working with has an architecture that has different data sources for text and background. It will take a lot of work to logically “Merge” the text and background.

In your online documentation, it is implied that the Zindex value allows you to control what content takes Zorder precedence. Your online documentation states that a negaive Zindes will give text precidence over a Graph with drawing Objects. I could not get that to work.
I


#4

Update:

I was able to write Shaded Rectangles using operators.

This is a snippet of my code, I had to convert from Argb to CMYK.

for some reason, this places the content in a manner that allows the text to be read.

    private static void WShRect(ShadedRect sr, Aspose.Pdf.Page p)
    {
        System.Drawing.Color sc= System.Drawing.Color.FromArgb(sr.Red, sr.Green, sr.Blue);
        Aspose.Pdf.Color aco = Aspose.Pdf.Color.FromArgb(sr.Red, sr.Green, sr.Blue);
        double rf = (sr.Red / 255F);
        double gf = (sr.Green / 255F);
        double bf = (sr.Blue / 255F);
        double CMYK_k = Convert.ToDouble(1.0F - Math.Max(Math.Max(rf, gf), bf));
        if (CMYK_k < 0 || double.IsNaN(CMYK_k)) CMYK_k = 0.0F;
        double CMYK_c = Convert.ToDouble((1 - rf - CMYK_k) / (1 - CMYK_k));
        if (CMYK_c < 0 || double.IsNaN(CMYK_c)) CMYK_c = 0.0F;
        double CMYK_m = Convert.ToDouble((1 - gf - CMYK_k) / (1 - CMYK_k));
        if (CMYK_m < 0 || double.IsNaN(CMYK_m)) CMYK_m = 0.0F;
        double CMYK_y = Convert.ToDouble((1 - bf - CMYK_k) / (1 - CMYK_k));
        if (CMYK_y < 0 || double.IsNaN(CMYK_y)) CMYK_m = 0.0F;
        p.Contents.Add(new Aspose.Pdf.Operators.GSave());
        p.Contents.Add(new Aspose.Pdf.Operators.SetCMYKColor(CMYK_c,CMYK_m,CMYK_y,CMYK_k));
        p.Contents.Add(new Aspose.Pdf.Operators.MoveTo(sr.P1.x_p,sr.P1.y_p));
        p.Contents.Add(new Aspose.Pdf.Operators.LineTo(sr.P2.x_p, sr.P2.y_p));
        p.Contents.Add(new Aspose.Pdf.Operators.LineTo(sr.P3.x_p, sr.P3.y_p));
        p.Contents.Add(new Aspose.Pdf.Operators.LineTo(sr.P4.x_p, sr.P4.y_p));
        p.Contents.Add(new Aspose.Pdf.Operators.EOFill());
        p.Contents .Add(new Aspose.Pdf.Operators.Stroke());
        p.Contents.Add(new Aspose.Pdf.Operators.GRestore());
    }

Thanks !


#5

@csludtke

Thank you for sharing the solution.

This will definitely help other community members with similar requirements. Please feel free to contact us if you need any further assistance.