Пример #1
0
        public override void Close(PdfWriter writer)
        {
            base.Close(writer);
            bool     ok      = false;
            IXmpMeta xmpMeta = writer.XmpWriter.XmpMeta;

            try {
                String docFileName = xmpMeta.GetPropertyString(PdfAXmpWriter.zugferdSchemaNS,
                                                               PdfAXmpWriter.zugferdDocumentFileName);
                foreach (PdfFileSpecification attachment in attachments)
                {
                    if (docFileName.Equals(attachment.GetAsString(PdfName.UF).ToString()))
                    {
                        PdfName relationship = attachment.GetAsName(PdfName.AFRELATIONSHIP);
                        if (!AFRelationshipValue.Alternative.Equals(relationship))
                        {
                            attachments.Clear();
                            throw new PdfAConformanceException(attachment,
                                                               MessageLocalization.GetComposedMessage("afrelationship.value.shall.be.alternative"));
                        }
                        ok = true;
                        break;
                    }
                }
            } catch (Exception e) {
                attachments.Clear();
                throw e;
            }
            attachments.Clear();
            if (!ok)
            {
                throw new PdfAConformanceException(xmpMeta,
                                                   MessageLocalization.GetComposedMessage("zugferd.xmp.schema.shall.contain.attachment.name"));
            }
        }
Пример #2
0
        private byte[] GetPropertyBase64(IXmpMeta meta, string schemaNs, string propName)
        {
            // Use System.Convert rather than IXmpMeta Base64 decoder.
            // The base64 strings have the padding removed which causes an exception in the IXmpMeta decoder.
            string base64 = meta.GetPropertyString(schemaNs, propName);

            base64 = base64.PadRight(base64.Length + (4 - base64.Length % 4) % 4, '=');
            return(Convert.FromBase64String(base64));
        }
Пример #3
0
 private void ParseDirectory(IXmpMeta meta)
 {
     try
     {
         FocusBlurAtInfinity = meta.GetPropertyDouble(nsFocus, "GFocus:BlurAtInfinity");
         FocusFocalDistance  = meta.GetPropertyDouble(nsFocus, "GFocus:FocalDistance");
         FocusFocalPointX    = meta.GetPropertyDouble(nsFocus, "GFocus:FocalPointX");
         FocusFocalPointY    = meta.GetPropertyDouble(nsFocus, "GFocus:FocalPointY");
         ImageMime           = meta.GetPropertyString(nsImage, "GImage:Mime");
         DepthFormat         = meta.GetPropertyString(nsDepthmap, "GDepth:Format");
         DepthNear           = meta.GetPropertyDouble(nsDepthmap, "GDepth:Near");
         DepthFar            = meta.GetPropertyDouble(nsDepthmap, "GDepth:Far");
         DepthMime           = meta.GetPropertyString(nsDepthmap, "GDepth:Mime");
     }
     catch (Exception e)
     {
         Debug.WriteLine(e);
         // Silent catch. Not sure what to do.
     }
 }
Пример #4
0
        private void ParseDirectory(IXmpMeta meta)
        {
            try
            {
                PanoCroppedAreaLeftPixels        = meta.GetPropertyInteger(nsPano, "GPano:CroppedAreaLeftPixels");
                PanoCroppedAreaTopPixels         = meta.GetPropertyInteger(nsPano, "GPano:CroppedAreaTopPixels");
                PanoCroppedAreaImageWidthPixels  = meta.GetPropertyInteger(nsPano, "GPano:CroppedAreaImageWidthPixels");
                PanoCroppedAreaImageHeightPixels = meta.GetPropertyInteger(nsPano, "GPano:CroppedAreaImageHeightPixels");
                PanoFullPanoWidthPixels          = meta.GetPropertyInteger(nsPano, "GPano:FullPanoWidthPixels");
                PanoFullPanoHeightPixels         = meta.GetPropertyInteger(nsPano, "GPano:FullPanoHeightPixels");
                PanoInitialViewHeadingDegrees    = meta.GetPropertyInteger(nsPano, "GPano:InitialViewHeadingDegrees");
                ImageMime = meta.GetPropertyString(nsImage, "GImage:Mime");

                if (meta.DoesPropertyExist(nsAudio, "GAudio:Mime"))
                {
                    AudioMime = meta.GetPropertyString(nsAudio, "GAudio:Mime");
                }
            }
            catch
            {
                // Silent catch. Not sure what to do.
            }
        }
Пример #5
0
        public override void Close(PdfWriter writer)
        {
            base.Close(writer);
            bool ok = false;

            IXmpMeta xmpMeta = null;

            if (writer.XmpWriter == null)
            {
                if (writer is PdfAStamperImp)
                {
                    xmpMeta = ((PdfAStamperImp)writer).GetXmpMeta();
                    PdfReader pdfReader = ((PdfAStamperImp)writer).GetPdfReader();
                    PdfArray  pdfArray  = pdfReader.Catalog.GetAsArray(PdfName.AF);
                    if (pdfArray != null)
                    {
                        for (int i = 0; i < pdfArray.Size; i++)
                        {
                            PdfFileSpecification pdfFileSpecification = new PdfFileSpecification();
                            pdfFileSpecification.PutAll((PdfDictionary)pdfArray.GetDirectObject(i));
                            attachments.Add(pdfFileSpecification);
                        }
                    }
                }
            }
            else
            {
                xmpMeta = writer.XmpWriter.XmpMeta;
            }

            if (xmpMeta == null)
            {
                writer.CreateXmpMetadata();
                xmpMeta = writer.XmpWriter.XmpMeta;
            }

            try {
                String docFileName = xmpMeta.GetPropertyString(PdfAXmpWriter.zugferdSchemaNS,
                                                               PdfAXmpWriter.zugferdDocumentFileName);
                foreach (PdfFileSpecification attachment in attachments)
                {
                    if ((attachment.GetAsString(PdfName.UF) != null && docFileName.Equals(attachment.GetAsString(PdfName.UF).ToString())) ||
                        (attachment.GetAsString(PdfName.F) != null && docFileName.Equals(attachment.GetAsString(PdfName.F).ToString())))
                    {
                        PdfName relationship = attachment.GetAsName(PdfName.AFRELATIONSHIP);
                        if (!AFRelationshipValue.Alternative.Equals(relationship))
                        {
                            attachments.Clear();
                            throw new PdfAConformanceException(attachment,
                                                               MessageLocalization.GetComposedMessage("afrelationship.value.shall.be.alternative"));
                        }
                        ok = true;
                        break;
                    }
                }
            } catch (Exception e) {
                attachments.Clear();
                throw e;
            }
            attachments.Clear();
            if (!ok)
            {
                throw new PdfAConformanceException(xmpMeta,
                                                   MessageLocalization.GetComposedMessage("zugferd.xmp.schema.shall.contain.attachment.name"));
            }
        }
        private static void ProcessXmpTag([NotNull] IXmpMeta meta, [NotNull] XmpDirectory directory, int tagType, FormatType formatCode)
        {
            string schemaNs;
            string propName;

            if (!XmpDirectory.TagSchemaMap.TryGetValue(tagType, out schemaNs) || !XmpDirectory.TagPropNameMap.TryGetValue(tagType, out propName))
            {
                return;
            }

            var property = meta.GetPropertyString(schemaNs, propName);

            if (property == null)
            {
                return;
            }

            switch (formatCode)
            {
            case FormatType.Rational:
            {
                // TODO introduce Rational.TryParse
                var rationalParts = property.Split('/').Take(2).ToArray();
                if (rationalParts.Length == 2)
                {
                    // TODO should this really be parsed as float?
                    float numerator;
                    float denominator;
                    if (float.TryParse(rationalParts[0], out numerator) && float.TryParse(rationalParts[1], out denominator))
                    {
                        directory.Set(tagType, new Rational((long)numerator, (long)denominator));
                    }
                    else
                    {
                        directory.AddError($"Unable to parse XMP property {propName} as a Rational.");
                    }
                }
                else
                {
                    directory.AddError($"Error in rational format for tag {tagType}");
                }
                break;
            }

            case FormatType.Int:
            {
                int value;
                if (int.TryParse(property, out value))
                {
                    directory.Set(tagType, value);
                }
                else
                {
                    directory.AddError($"Unable to parse XMP property {propName} as an int.");
                }
                break;
            }

            case FormatType.Double:
            {
                double value;
                if (double.TryParse(property, out value))
                {
                    directory.Set(tagType, value);
                }
                else
                {
                    directory.AddError($"Unable to parse XMP property {propName} as a double.");
                }
                break;
            }

            case FormatType.String:
            {
                directory.Set(tagType, property);
                break;
            }

            case FormatType.StringArray:
            {
                // XMP iterators are 1-based
                var count = meta.CountArrayItems(schemaNs, propName);
                var array = new string[count];
                for (var i = 1; i <= count; i++)
                {
                    array[i - 1] = meta.GetArrayItem(schemaNs, propName, i).Value;
                }
                directory.Set(tagType, array);
                break;
            }

            default:
            {
                directory.AddError($"Unknown format code {formatCode} for tag {tagType}");
                break;
            }
            }
        }
        /// <summary>Reads an property value with given namespace URI and property name.</summary>
        /// <remarks>Reads an property value with given namespace URI and property name. Add property value to directory if exists</remarks>
        /// <exception cref="XmpException"/>
        private static void ProcessXmpTag([NotNull] IXmpMeta meta, [NotNull] XmpDirectory directory, int tagType, FormatType formatCode)
        {
            string schemaNs;
            string propName;

            if (!XmpDirectory.TagSchemaMap.TryGetValue(tagType, out schemaNs) || !XmpDirectory.TagPropNameMap.TryGetValue(tagType, out propName))
            {
                return;
            }

            var property = meta.GetPropertyString(schemaNs, propName);

            if (property == null)
            {
                return;
            }

            switch (formatCode)
            {
            case FormatType.Rational:
            {
                var rationalParts = property.Split(new[] { '/' }, 2);
                if (rationalParts.Length == 2)
                {
                    try
                    {
                        var rational = new Rational((long)float.Parse(rationalParts[0]), (long)float.Parse(rationalParts[1]));
                        directory.Set(tagType, rational);
                    }
                    catch (FormatException)
                    {
                        directory.AddError($"Unable to parse XMP property {propName} as a Rational.");
                    }
                }
                else
                {
                    directory.AddError("Error in rational format for tag " + tagType);
                }
                break;
            }

            case FormatType.Int:
            {
                try
                {
                    directory.Set(tagType, int.Parse(property));
                }
                catch (FormatException)
                {
                    directory.AddError($"Unable to parse XMP property {propName} as an int.");
                }
                break;
            }

            case FormatType.Double:
            {
                try
                {
                    directory.Set(tagType, double.Parse(property));
                }
                catch (FormatException)
                {
                    directory.AddError($"Unable to parse XMP property {propName} as an double.");
                }
                break;
            }

            case FormatType.String:
            {
                directory.Set(tagType, property);
                break;
            }

            case FormatType.StringArray:
            {
                //XMP iterators are 1-based
                var count = meta.CountArrayItems(schemaNs, propName);
                var array = new string[count];
                for (var i = 1; i <= count; ++i)
                {
                    array[i - 1] = meta.GetArrayItem(schemaNs, propName, i).Value;
                }
                directory.Set(tagType, array);
                break;
            }

            default:
            {
                directory.AddError($"Unknown format code {formatCode} for tag {tagType}");
                break;
            }
            }
        }