Example #1
0
        private static void AddAnnot(AnnotObject annotObject, XmlElement annots, XmlDocument document)
        {
            if (annotObject.GetName() == null)
            {
                return;
            }
            XmlElement annot = document.CreateElement(annotObject.GetName());

            foreach (AttributeObject attr in annotObject.GetAttributes())
            {
                annot.SetAttribute(attr.GetName(), attr.GetValue());
            }
            if (annotObject.GetPopup() != null)
            {
                XmlElement popup = document.CreateElement("popup");
                AddPopup(annotObject.GetPopup(), popup, annot);
            }
            if (annotObject.GetContents() != null)
            {
                XmlElement contents = document.CreateElement("contents");
                contents.InnerText = annotObject.GetContents().ToString().Replace('\r', '\n');
                annot.AppendChild(contents);
            }
            if (annotObject.GetAppearance() != null)
            {
                XmlElement appearance = document.CreateElement("appearance");
                appearance.InnerText = annotObject.GetAppearance();
                annot.AppendChild(appearance);
            }
            //
            //        if (annotObject.getContentsRichText() != null) {
            //            Element contentsRichText = document.createElement("contents-richtext");
            //            contentsRichText.setNodeValue(annotObject.getContents().getValue());
            //            annot.appendChild(contentsRichText);
            //        }
            if (XfdfConstants.LINK.EqualsIgnoreCase(annotObject.GetName()))
            {
                if (annotObject.GetDestination() != null)
                {
                    AddDest(annotObject.GetDestination(), annot, document);
                }
                else
                {
                    if (annotObject.GetAction() != null)
                    {
                        XmlElement onActivation = document.CreateElement(XfdfConstants.ON_ACTIVATION);
                        AddActionObject(annotObject.GetAction(), onActivation, document);
                        annot.AppendChild(onActivation);
                    }
                    else
                    {
                        logger.Error("Dest and OnActivation elements are both missing");
                    }
                }
                if (annotObject.GetBorderStyleAlt() != null)
                {
                    AddBorderStyleAlt(annotObject.GetBorderStyleAlt(), annot, document);
                }
            }
            if (XfdfConstants.FREETEXT.EqualsIgnoreCase(annotObject.GetName()))
            {
                String defaultAppearanceString = annotObject.GetDefaultAppearance();
                if (defaultAppearanceString != null)
                {
                    XmlElement defaultAppearance = document.CreateElement(XfdfConstants.DEFAULT_APPEARANCE);
                    defaultAppearance.InnerText = defaultAppearanceString;
                    annot.AppendChild(defaultAppearance);
                }
                String defaultStyleString = annotObject.GetDefaultStyle();
                if (defaultStyleString != null)
                {
                    XmlElement defaultStyle = document.CreateElement(XfdfConstants.DEFAULT_STYLE);
                    defaultStyle.InnerText = defaultStyleString;
                    annot.AppendChild(defaultStyle);
                }
            }
            annots.AppendChild(annot);
        }
Example #2
0
        private void AddAnnotationToPdf(AnnotObject annotObject, PdfDocument pdfDocument)
        {
            String annotName = annotObject.GetName();

            if (annotName != null)
            {
                switch (annotName)
                {
                case XfdfConstants.TEXT: {
                    //TODO add all attributes properly one by one
                    PdfTextAnnotation pdfTextAnnotation = new PdfTextAnnotation(XfdfObjectUtils.ConvertRectFromString(annotObject
                                                                                                                      .GetAttributeValue(XfdfConstants.RECT)));
                    AddCommonAnnotationAttributes(pdfTextAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfTextAnnotation, annotObject);
                    pdfTextAnnotation.SetIconName(new PdfName(annotObject.GetAttributeValue(XfdfConstants.ICON)));
                    if (annotObject.GetAttributeValue(XfdfConstants.STATE) != null)
                    {
                        pdfTextAnnotation.SetState(new PdfString(annotObject.GetAttributeValue(XfdfConstants.STATE)));
                    }
                    if (annotObject.GetAttributeValue(XfdfConstants.STATE_MODEL) != null)
                    {
                        pdfTextAnnotation.SetStateModel(new PdfString(annotObject.GetAttributeValue(XfdfConstants.STATE_MODEL)));
                    }
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttributeValue(XfdfConstants.PAGE))).AddAnnotation(pdfTextAnnotation
                                                                                                                          );
                    break;
                }

                case XfdfConstants.HIGHLIGHT: {
                    PdfTextMarkupAnnotation pdfHighLightAnnotation = new PdfTextMarkupAnnotation(XfdfObjectUtils.ConvertRectFromString
                                                                                                     (annotObject.GetAttributeValue(XfdfConstants.RECT)), PdfName.Highlight, XfdfObjectUtils.ConvertQuadPointsFromCoordsString
                                                                                                     (annotObject.GetAttributeValue(XfdfConstants.COORDS)));
                    AddCommonAnnotationAttributes(pdfHighLightAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfHighLightAnnotation, annotObject);
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (pdfHighLightAnnotation);
                    break;
                }

                case XfdfConstants.UNDERLINE: {
                    PdfTextMarkupAnnotation pdfUnderlineAnnotation = new PdfTextMarkupAnnotation(XfdfObjectUtils.ConvertRectFromString
                                                                                                     (annotObject.GetAttributeValue(XfdfConstants.RECT)), PdfName.Underline, XfdfObjectUtils.ConvertQuadPointsFromCoordsString
                                                                                                     (annotObject.GetAttributeValue(XfdfConstants.COORDS)));
                    AddCommonAnnotationAttributes(pdfUnderlineAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfUnderlineAnnotation, annotObject);
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (pdfUnderlineAnnotation);
                    break;
                }

                case XfdfConstants.STRIKEOUT: {
                    PdfTextMarkupAnnotation pdfStrikeoutAnnotation = new PdfTextMarkupAnnotation(XfdfObjectUtils.ConvertRectFromString
                                                                                                     (annotObject.GetAttributeValue(XfdfConstants.RECT)), PdfName.StrikeOut, XfdfObjectUtils.ConvertQuadPointsFromCoordsString
                                                                                                     (annotObject.GetAttributeValue(XfdfConstants.COORDS)));
                    AddCommonAnnotationAttributes(pdfStrikeoutAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfStrikeoutAnnotation, annotObject);
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (pdfStrikeoutAnnotation);
                    break;
                }

                case XfdfConstants.SQUIGGLY: {
                    PdfTextMarkupAnnotation pdfSquigglyAnnotation = new PdfTextMarkupAnnotation(XfdfObjectUtils.ConvertRectFromString
                                                                                                    (annotObject.GetAttributeValue(XfdfConstants.RECT)), PdfName.Squiggly, XfdfObjectUtils.ConvertQuadPointsFromCoordsString
                                                                                                    (annotObject.GetAttributeValue(XfdfConstants.COORDS)));
                    AddCommonAnnotationAttributes(pdfSquigglyAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfSquigglyAnnotation, annotObject);
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (pdfSquigglyAnnotation);
                    break;
                }

                case XfdfConstants.CIRCLE: {
                    //                case XfdfConstants.LINE:
                    //                    pdfDocument.getPage(Integer.parseInt(annotObject.getAttribute(XfdfConstants.PAGE).getValue()))
                    //                            .addAnnotation(new PdfLineAnnotation(XfdfObjectUtils.convertRectFromString(annotObject.getAttributeValue(XfdfConstants.RECT)), XfdfObjectUtils.convertVerticesFromString(annotObject.getVertices())));
                    //                    break;
                    PdfCircleAnnotation pdfCircleAnnotation = new PdfCircleAnnotation(XfdfObjectUtils.ConvertRectFromString(annotObject
                                                                                                                            .GetAttributeValue(XfdfConstants.RECT)));
                    AddCommonAnnotationAttributes(pdfCircleAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfCircleAnnotation, annotObject);
                    if (annotObject.GetAttributeValue(XfdfConstants.FRINGE) != null)
                    {
                        pdfCircleAnnotation.SetRectangleDifferences(XfdfObjectUtils.ConvertFringeFromString(annotObject.GetAttributeValue
                                                                                                                (XfdfConstants.FRINGE)));
                    }
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (pdfCircleAnnotation);
                    break;
                }

                case XfdfConstants.SQUARE: {
                    PdfSquareAnnotation pdfSquareAnnotation = new PdfSquareAnnotation(XfdfObjectUtils.ConvertRectFromString(annotObject
                                                                                                                            .GetAttributeValue(XfdfConstants.RECT)));
                    AddCommonAnnotationAttributes(pdfSquareAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(pdfSquareAnnotation, annotObject);
                    if (annotObject.GetAttributeValue(XfdfConstants.FRINGE) != null)
                    {
                        pdfSquareAnnotation.SetRectangleDifferences(XfdfObjectUtils.ConvertFringeFromString(annotObject.GetAttributeValue
                                                                                                                (XfdfConstants.FRINGE)));
                    }
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (pdfSquareAnnotation);
                    break;
                }

                case XfdfConstants.POLYGON: {
                    //XfdfConstants.CARET
                    Rectangle             rect              = XfdfObjectUtils.ConvertRectFromString(annotObject.GetAttributeValue(XfdfConstants.RECT));
                    float[]               vertices          = XfdfObjectUtils.ConvertVerticesFromString(annotObject.GetVertices());
                    PdfPolyGeomAnnotation polygonAnnotation = PdfPolyGeomAnnotation.CreatePolygon(rect, vertices);
                    AddCommonAnnotationAttributes(polygonAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(polygonAnnotation, annotObject);
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (polygonAnnotation);
                    break;
                }

                case XfdfConstants.POLYLINE: {
                    Rectangle polylineRect = XfdfObjectUtils.ConvertRectFromString(annotObject.GetAttributeValue(XfdfConstants
                                                                                                                 .RECT));
                    float[] polylineVertices = XfdfObjectUtils.ConvertVerticesFromString(annotObject.GetVertices());
                    PdfPolyGeomAnnotation polylineAnnotation = PdfPolyGeomAnnotation.CreatePolyLine(polylineRect, polylineVertices
                                                                                                    );
                    AddCommonAnnotationAttributes(polylineAnnotation, annotObject);
                    AddMarkupAnnotationAttributes(polylineAnnotation, annotObject);
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (polylineAnnotation);
                    break;
                }

                case XfdfConstants.STAMP: {
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (new PdfStampAnnotation(XfdfObjectUtils.ConvertRectFromString(annotObject.GetAttributeValue(XfdfConstants
                                                                                                                    .RECT))));
                    break;
                }

                case XfdfConstants.FREETEXT: {
                    //XfdfConstants.INK
                    pdfDocument.GetPage(Convert.ToInt32(annotObject.GetAttribute(XfdfConstants.PAGE).GetValue())).AddAnnotation
                        (new PdfFreeTextAnnotation(XfdfObjectUtils.ConvertRectFromString(annotObject.GetAttributeValue(XfdfConstants
                                                                                                                       .RECT)), annotObject.GetContents()));
                    break;
                }

                default: {
                    //XfdfConstants.FILEATTACHMENT
                    //XfdfConstants.SOUND
                    //XfdfConstants.LINK
                    //XfdfConstants.REDACT
                    //XfdfConstants.PROJECTION
                    logger.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.XFDF_ANNOTATION_IS_NOT_SUPPORTED, annotName
                                                         ));
                    break;
                }
                }
            }
        }