Support
unread,Apr 14, 2009, 4:23:06 PM4/14/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to PDFTron PDFNet SDK
Q: Thanks PDF text highlighting for complex regions works fine now,
however I am getting some errors in the older versions of Acrobat
Reader. Can you spot anything in my highlighting code:
var highlightRegions = new List<HighlightRegionInfo>();
HighlightRegions(highlightRegions, highlightColor, file);
file.SaveAs(...);
file.Close();
public Rect RectUnion(List<Rect> boxes)
{
double y1 = boxes.Select(box => box.y1).Min();
double y2 = boxes.Select(box => box.y2).Max();
double x1 = boxes.Where(box => box.y1 == y1).Select(box =>
box.x1).Min();
double x2 = boxes.Where(box => box.y2 == y2).Select(box =>
box.x2).Max();
return new Rect(x1, y1, x2, y2);
}
public void CreateHighlightAnnotation(PDFDoc pdfDocument,
HighlightRegionInfo region, Color highlightColor, int pageNumber)
{
List<Rect> boxes = region.Words.Select(word => word.Box).ToList();
Annot annotation = Annot.Create(pdfDocument,
Annot.Type.e_Highlight, RectUnion(boxes));
ColorPt pdfHighlightColor = highlightColor.ToPdfColor();
annotation.SetColor(pdfHighlightColor);
var t = new Text(annotation);
t.SetTextContents(region.Text);
annotation.SetDate(DateTime.Now.ToPdfDate());
annotation.SetBorderStyle(new Annot.BorderStyle
(Annot.BorderStyle.Style.e_solid, 1));
Page page = pdfDocument.GetPage(pageNumber);
Obj quads = annotation.GetSDFObj().PutArray("QuadPoints");
List<Rect> lineRectangles = boxes.GroupBy(box => box.y1)
.Select(line => RectUnion(line.ToList()))
.ToList();
lineRectangles.ForEach(lineRect => PushBackBox(quads, lineRect));
annotation.SetAppearance(CreateHighlightAppearance(lineRectangles,
pdfHighlightColor, page));
page.AnnotPushBack(annotation);
}
private void PushBackBox(Obj quads, Rect box)
{
quads.PushBackNumber(box.x1);
quads.PushBackNumber(box.y2 + 2);
quads.PushBackNumber(box.x2);
quads.PushBackNumber(box.y2 + 2);
quads.PushBackNumber(box.x1);
quads.PushBackNumber(box.y1 + 2);
quads.PushBackNumber(box.x2);
quads.PushBackNumber(box.y1 + 2);
}
public Obj CreateHighlightAppearance( /*PDFDoc pdfDocument,*/
List<Rect> boxes, ColorPt highlightColor, Page page)
{
var elementBuilder = new ElementBuilder();
elementBuilder.PathBegin();
boxes.ForEach(box => elementBuilder.Rect(box.x1 - 2, box.y1,
box.x2 - box.x1 + 2, box.y2 - box.y1 + 2));
Element element = elementBuilder.PathEnd();
element.SetPathFill(true);
element.SetPathStroke(false);
GState elementGraphicState = element.GetGState();
elementGraphicState.SetFillColorSpace(ColorSpace.CreateDeviceRGB
());
elementGraphicState.SetFillColor(highlightColor);
elementGraphicState.SetBlendMode(GState.BlendMode.e_bl_multiply);
var elementWriter = new ElementWriter();
// elementWriter.Begin(page, true); <------------!!!!
elementWriter.Begin();
elementWriter.WriteElement(element);
Obj highlightAppearance = elementWriter.End();
elementBuilder.Dispose();
elementWriter.Dispose();
//Set the bounding box
Rect boundingBox = RectUnion(boxes);
highlightAppearance.PutRect("BBox", boundingBox.x1,
boundingBox.y1, boundingBox.x2, boundingBox.y2);
highlightAppearance.PutName("Subtype", "Form");
return highlightAppearance;
}
-------
A: The problem is that you are adding highlight appearances to an
existing page (elementWriter.Begin(page, true)) instead of separate
appearance stream (elementWriter.Begin()). To fix this simply replace:
elementWriter.Begin(page, true);
with
elementWriter.Begin();