Exemplo n.º 1
0
        private void SaveDocMDP(TPdfStream DataStream, TXRefSection XRef)
        {
            BeginDictionary(DataStream);
            SaveKey(DataStream, TPdfToken.TypeName, TPdfTokens.GetString(TPdfToken.SigRefName));
            SaveKey(DataStream, TPdfToken.TransformMethodName, TPdfTokens.GetString(TPdfToken.DocMDPName));
            SaveTransformParams(DataStream);
            SaveKey(DataStream, TPdfToken.DigestMethodName, TPdfTokens.GetString(TPdfToken.MD5Name));

            //This is a small hack. We don't have an object model of the pdf file able to compute the object hash,
            //and it is not needed anyway for Acrobat >=7 (and <6 does not allow MDP anyway).
            //But we need to write something here, or acrobat will complain the document is not PDF/SigQ complaint.
            byte[] MDPHash    = new byte[16];
            string MDPHashStr = PdfConv.ToHexString(MDPHash, true);

            Write(DataStream, TPdfTokens.GetString(TPdfToken.DigestValueName));
            long StartMDPHash = DataStream.Position;

            Write(DataStream, MDPHashStr);
            string LocationString =
                TPdfTokens.GetString(TPdfToken.OpenArray) +
                String.Format(CultureInfo.InvariantCulture, "{0} {1}", StartMDPHash, MDPHashStr.Length) +
                TPdfTokens.GetString(TPdfToken.CloseArray);

            SaveKey(DataStream, TPdfToken.DigestLocationName, LocationString);

            EndDictionary(DataStream);
        }
Exemplo n.º 2
0
        internal TPdfStream(Stream aDataStream, TPdfSignature aSignature)
        {
            EndTextData = TPdfBaseRecord.Coder.GetBytes(TPdfTokens.GetString(TPdfToken.CommandEndText) + TPdfTokens.NewLine);

            Signature = aSignature;
            if (Signature != null)
            {
                if (Signature.SignerFactory != null)
                {
                    Signer = Signature.SignerFactory.CreateSigner();
                }
                else
                {
                    PdfMessages.ThrowException(PdfErr.ErrUnassingedSignerFactory);
                }
            }

            if (Signer == null)
            {
                FDataStream = aDataStream;
            }
            else
            {
                FDataStream = new TSignedStream(aDataStream, Signer);                                                  //When signing we use a special stream that will compute the hash.
            }
            SignedStream = FDataStream as TSignedStream;
        }
Exemplo n.º 3
0
        private static void Saven2(TPdfStream DataStream, TXRefSection XRef, TPdfVisibleSignature VSig, int n2Id)
        {
            string s = String.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3} {4} {5} cm ",
                                     VSig.Rect.Width,
                                     0, 0,
                                     VSig.Rect.Height,
                                     0, 0);

            string StreamContents = "q " + s + TPdfTokens.GetString(TPdfToken.ImgPrefix) + "0 Do Q";

            XRef.SetObjectOffset(n2Id, DataStream);
            TIndirectRecord.SaveHeader(DataStream, n2Id);
            BeginDictionary(DataStream);
            WriteCommonXObject(DataStream, VSig.Rect, StreamContents);

            TPdfResources Resources = new TPdfResources(null, false, null);

            using (MemoryStream ImgStream = new MemoryStream(VSig.ImageData))
            {
                Resources.AddImage(null, ImgStream, FlxConsts.NoTransparentColor, false);
            }

            SaveProcSet(DataStream, XRef, true);
            SaveResourcesImgXObject(DataStream, XRef, Resources);

            EndDictionary(DataStream);
            WriteStream(DataStream, StreamContents);

            TIndirectRecord.SaveTrailer(DataStream);

            Resources.SaveObjects(DataStream, XRef);
        }
Exemplo n.º 4
0
        internal string GetDestStr(TPdfDestination Dest)
        {
            int PageId = PageTree.GetPageId(Dest.PageNumber);

            string PageStr = TIndirectRecord.GetCallObj(PageId);

            switch (Dest.ZoomOptions)
            {
            case TZoomOptions.Fit:
                return
                    (TPdfTokens.GetString(TPdfToken.OpenArray) +
                     PageStr + TPdfTokens.GetString(TPdfToken.FitName) +
                     TPdfTokens.GetString(TPdfToken.CloseArray));

            case TZoomOptions.FitH:
                return
                    (TPdfTokens.GetString(TPdfToken.OpenArray) +
                     PageStr + TPdfTokens.GetString(TPdfToken.FitHName) +
                     TPdfTokens.GetString(TPdfToken.CloseArray));

            case TZoomOptions.FitV:
                return
                    (TPdfTokens.GetString(TPdfToken.OpenArray) +
                     PageStr + TPdfTokens.GetString(TPdfToken.FitVName) +
                     TPdfTokens.GetString(TPdfToken.CloseArray));
            }
            return
                (TPdfTokens.GetString(TPdfToken.OpenArray) +
                 PageStr + TPdfTokens.GetString(TPdfToken.XYZName) +
                 TPdfTokens.GetString(TPdfToken.CloseArray));
        }
Exemplo n.º 5
0
        internal string GetCallArray(TPdfStream DataStream, TXRefSection XRef, bool OnlySignatures)
        {
            StringBuilder sb     = new StringBuilder();
            int           aCount = Count;

            for (int i = 0; i < aCount; i++)
            {
                int id = Fields[i].GetId(DataStream, XRef, OnlySignatures);
                if (id < 0)
                {
                    continue;
                }
                if (sb.Length > 0)
                {
                    sb.Append(" ");
                }
                sb.Append(TIndirectRecord.GetCallObj(id));
            }
            if (sb.Length == 0)
            {
                return(String.Empty);
            }

            if (OnlySignatures)
            {
                return(sb.ToString());
            }

            return(TPdfTokens.GetString(TPdfToken.OpenArray) + sb.ToString()
                   + TPdfTokens.GetString(TPdfToken.CloseArray));
        }
Exemplo n.º 6
0
        private void SavePatterns(TPdfStream DataStream, TXRefSection XRef)
        {
			int HatchCount = HatchPatterns.Count;
			int ImageTextureCount = ImageTexturePatterns.Count;
			int GradientCount = GradientPatterns.Count;
            if (HatchCount + GradientCount + ImageTextureCount <= 0) return;
            TPdfBaseRecord.WriteLine(DataStream, TPdfTokens.GetString(TPdfToken.PatternName));
			TDictionaryRecord.BeginDictionary(DataStream);
			for (int i = 0; i < HatchCount; i++)
			{
				HatchPatterns[i].WritePattern(DataStream, XRef);
			}

			for (int i = 0; i < ImageTextureCount; i++)
			{
				ImageTexturePatterns[i].WritePattern(DataStream, XRef);
			}
            
            for (int i = 0; i < GradientCount; i++)
            {
                GradientPatterns[i].WritePattern(DataStream, XRef);
            }

            TDictionaryRecord.EndDictionary(DataStream);
            if (HatchCount <= 0) return;

            //Pattern color space
            TPdfBaseRecord.WriteLine(DataStream, TPdfTokens.GetString(TPdfToken.ColorSpaceName));
            TDictionaryRecord.BeginDictionary(DataStream);
            PatternColorSpaceId = TPdfHatch.WriteColorSpace(DataStream, XRef);
            TDictionaryRecord.EndDictionary(DataStream);
        }
Exemplo n.º 7
0
 private void SaveReferenceDict(TPdfStream DataStream, TXRefSection XRef)
 {
     Write(DataStream, TPdfTokens.GetString(TPdfToken.ReferenceName));
     Write(DataStream, TPdfTokens.GetString(TPdfToken.OpenArray));
     SaveDocMDP(DataStream, XRef);
     SaveFieldMDP(DataStream, XRef);
     Write(DataStream, TPdfTokens.GetString(TPdfToken.CloseArray));
 }
Exemplo n.º 8
0
 private static void SaveResourcesFirstXObject(TPdfStream DataStream, TXRefSection XRef, int FRMId)
 {
     Write(DataStream, TPdfTokens.GetString(TPdfToken.XObjectName));
     BeginDictionary(DataStream);
     SaveKey(DataStream, TPdfToken.FRMName, TIndirectRecord.GetCallObj(FRMId));
     EndDictionary(DataStream);
     EndDictionary(DataStream);
 }
Exemplo n.º 9
0
 private static void SaveResourcesSecondXObject(TPdfStream DataStream, int n0Id, int n2Id)
 {
     Write(DataStream, TPdfTokens.GetString(TPdfToken.XObjectName));
     BeginDictionary(DataStream);
     SaveKey(DataStream, TPdfToken.n0Name, TIndirectRecord.GetCallObj(n0Id));
     SaveKey(DataStream, TPdfToken.n2Name, TIndirectRecord.GetCallObj(n2Id));
     EndDictionary(DataStream);
     EndDictionary(DataStream);
 }
Exemplo n.º 10
0
 private static void WriteCommonXObject(TPdfStream DataStream, RectangleF Rect, string StreamContents)
 {
     SaveKey(DataStream, TPdfToken.LengthName, StreamContents.Length);
     SaveKey(DataStream, TPdfToken.TypeName, TPdfTokens.GetString(TPdfToken.XObjectName));
     SaveKey(DataStream, TPdfToken.SubtypeName, TPdfTokens.GetString(TPdfToken.FormName));
     SaveKey(DataStream, TPdfToken.FormTypeName, 1);
     SaveKey(DataStream, TPdfToken.MatrixName, "[1.0 0.0 0.0 1.0 0.0 0.0]"); //Standard drawing matrix
     SaveKey(DataStream, TPdfToken.BBoxName, PdfConv.ToRectangleWH(new RectangleF(0, 0, Rect.Width, Rect.Height), true));
 }
Exemplo n.º 11
0
 public static void WriteColorSpaceObject(TPdfStream DataStream, TXRefSection XRef, int ColorSpaceId)
 {
     XRef.SetObjectOffset(ColorSpaceId, DataStream);
     TIndirectRecord.SaveHeader(DataStream, ColorSpaceId);
     TPdfBaseRecord.Write(DataStream, TPdfTokens.GetString(TPdfToken.OpenArray));
     TDictionaryRecord.SaveKey(DataStream, TPdfToken.PatternName, TPdfTokens.GetString(TPdfToken.DeviceRGBName));
     TPdfBaseRecord.Write(DataStream, TPdfTokens.GetString(TPdfToken.CloseArray));
     TIndirectRecord.SaveTrailer(DataStream);
 }
Exemplo n.º 12
0
 private void SaveKids(TPdfStream DataStream, TXRefSection XRef)
 {
     WriteLine(DataStream, TPdfTokens.GetString(TPdfToken.KidsName) + " " + TPdfTokens.GetString(TPdfToken.OpenArray));
     for (int i = 0; i < FList.Count; i++)
     {
         TIndirectRecord.CallObj(DataStream, FList[i].Id);
     }
     WriteLine(DataStream, TPdfTokens.GetString(TPdfToken.CloseArray));
 }
Exemplo n.º 13
0
 private void SaveTransformParams(TPdfStream DataStream)
 {
     Write(DataStream, TPdfTokens.GetString(TPdfToken.TransformParamsName));
     BeginDictionary(DataStream);
     SaveKey(DataStream, TPdfToken.TypeName, TPdfTokens.GetString(TPdfToken.TransformParamsName));
     SaveKey(DataStream, TPdfToken.PName, Signature.AllowedChangesValue);
     SaveKey(DataStream, TPdfToken.VName, TPdfTokens.GetString(TPdfToken.V1_2Name));
     EndDictionary(DataStream);
 }
Exemplo n.º 14
0
        public static int WriteColorSpace(TPdfStream DataStream, TXRefSection XRef)
        {
            int Result = XRef.GetNewObject(DataStream);

            TPdfBaseRecord.Write(DataStream, TPdfTokens.GetString(TPdfToken.PatternColorSpacePrefix) + " ");
            TIndirectRecord.CallObj(DataStream, Result);

            return(Result);
        }
Exemplo n.º 15
0
 public void WriteImage(TPdfStream DataStream, TXRefSection XRef)
 {
     TPdfBaseRecord.Write(DataStream, TPdfTokens.GetString(TPdfToken.ImgPrefix) + Id.ToString(CultureInfo.InvariantCulture) + " ");
     ImgObjId = XRef.GetNewObject(DataStream);
     TIndirectRecord.CallObj(DataStream, ImgObjId);
     if (FSMask != null)
     {
         SMaskId = XRef.GetNewObject(DataStream);
     }
 }
Exemplo n.º 16
0
        internal TLinkAnnot(real ax1, real ay1, real aWidth, real aHeight, string URL) :
            base(ax1, ay1, aWidth, aHeight)
        {
            //only 7-bits allowed.

            byte[] bt = TPdfStringRecord.EscapeString(Encoding.ASCII.GetBytes(URL));
            FURL = TPdfTokens.GetString(TPdfToken.OpenString) +
                   Encoding.ASCII.GetString(bt, 0, bt.Length) +
                   TPdfTokens.GetString(TPdfToken.CloseString);
        }
Exemplo n.º 17
0
        internal override void SaveToStream(TPdfStream DataStream, TXRefSection XRef)
        {
            BeginSaveToStream(DataStream, XRef);
            SaveKey(DataStream, TPdfToken.SubtypeName, TPdfTokens.GetString(TPdfToken.LinkName));
            SaveKey(DataStream, TPdfToken.AName, TIndirectRecord.GetCallObj(ActionId));
            SaveKey(DataStream, TPdfToken.BorderName, TPdfTokens.GetString(TPdfToken.Border0));
            EndSaveToStream(DataStream, XRef);

            SaveAction(DataStream, XRef);
        }
Exemplo n.º 18
0
 private static bool IsStandardFont(String aFontName)
 {
     aFontName = aFontName.Replace(" ", String.Empty);
     return
         (String.Equals(aFontName, TPdfTokens.GetString(TPdfToken.StArial), StringComparison.CurrentCultureIgnoreCase) ||
          String.Equals(aFontName, TPdfTokens.GetString(TPdfToken.StCourier), StringComparison.CurrentCultureIgnoreCase) ||
          String.Equals(aFontName, TPdfTokens.GetString(TPdfToken.StCourierNew), StringComparison.CurrentCultureIgnoreCase) ||
          String.Equals(aFontName, TPdfTokens.GetString(TPdfToken.StMicrosoftSansSerif), StringComparison.CurrentCultureIgnoreCase) ||
          String.Equals(aFontName, TPdfTokens.GetString(TPdfToken.StMicrosoftSerif), StringComparison.CurrentCultureIgnoreCase) ||
          String.Equals(aFontName, TPdfTokens.GetString(TPdfToken.StTimesNewRoman), StringComparison.CurrentCultureIgnoreCase));
 }
Exemplo n.º 19
0
        private static int SaveAPRef(TPdfStream DataStream, TXRefSection XRef)
        {
            //AP call
            int APId = XRef.GetNewObject(DataStream);

            Write(DataStream, TPdfTokens.GetString(TPdfToken.APName));
            BeginDictionary(DataStream);
            SaveKey(DataStream, TPdfToken.NName, TIndirectRecord.GetCallObj(APId));
            EndDictionary(DataStream);
            return(APId);
        }
Exemplo n.º 20
0
        private void WriteToUnicodeData(TPdfStream DataStream)
        {
            string s = TPdfTokens.GetString(TPdfToken.ToUnicodeData).Replace("\r", "");

            TPdfBaseRecord.WriteLine(DataStream, s);

            TPdfBaseRecord.WriteLine(DataStream, ToUnicodeData.GetData());

            s = TPdfTokens.GetString(TPdfToken.ToUnicodeData2).Replace("\r", "");
            TPdfBaseRecord.WriteLine(DataStream, s);
        }
Exemplo n.º 21
0
 private static void WriteSimpleString(TPdfStream DataStream, string Text, TPdfFont aFont, string EndNewText)
 {
     if (Text == null || Text.Length == 0)
     {
         return;
     }
     Write(DataStream, TPdfTokens.GetString(TPdfToken.OpenString));
     UnicodeWrite(DataStream, Text, aFont);
     Write(DataStream, TPdfTokens.GetString(TPdfToken.CloseString));
     TPdfBaseRecord.WriteLine(DataStream, EndNewText);
 }
Exemplo n.º 22
0
        internal byte[] GetByteRange(TPdfStream DataStream)
        {
            long cut    = DataStream.SignPosition;
            long endcut = cut + PKCSSize;

            string s = TPdfTokens.GetString(TPdfToken.ByteRangeName) +
                       TPdfTokens.GetString(TPdfToken.OpenArray) +
                       String.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3}", 0, cut, endcut, DataStream.Length - endcut) +
                       TPdfTokens.GetString(TPdfToken.CloseArray);

            return(Coder.GetBytes(s));
        }
Exemplo n.º 23
0
        private string GetCoords()
        {
            StringBuilder Result = new StringBuilder();

            Result.Append(TPdfTokens.GetString(TPdfToken.OpenArray));
            Result.Append(PdfConv.CoordsToString(RotatedCoords.Left)); Result.Append(" ");
            Result.Append(PdfConv.CoordsToString(RotatedCoords.Top)); Result.Append(" ");
            Result.Append(PdfConv.CoordsToString(RotatedCoords.Right)); Result.Append(" ");
            Result.Append(PdfConv.CoordsToString(RotatedCoords.Bottom));
            Result.Append(TPdfTokens.GetString(TPdfToken.CloseArray));
            return(Result.ToString());
        }
Exemplo n.º 24
0
 private void SaveGStates(TPdfStream DataStream, TXRefSection XRef)
 {
     int aCount = GStates.Count;
     if (aCount <= 0) return;
     TPdfBaseRecord.WriteLine(DataStream, TPdfTokens.GetString(TPdfToken.ExtGStateName));
     TDictionaryRecord.BeginDictionary(DataStream);
     for (int i = 0; i < aCount; i++)
     {
         GStates[i].WriteGState(DataStream, XRef);
     }
     TDictionaryRecord.EndDictionary(DataStream);
 }
Exemplo n.º 25
0
        private void SaveAction(TPdfStream DataStream, TXRefSection XRef)
        {
            XRef.SetObjectOffset(ActionId, DataStream);
            TIndirectRecord.SaveHeader(DataStream, ActionId);
            BeginDictionary(DataStream);

            TPdfBaseRecord.Write(DataStream,
                                 String.Format(CultureInfo.InvariantCulture, TPdfTokens.GetString(TPdfToken.LinkData), FURL));

            EndDictionary(DataStream);
            TIndirectRecord.SaveTrailer(DataStream);
        }
Exemplo n.º 26
0
        internal static void SaveToStream(TPdfStream DataStream)
        {
            if (DataStream.HasSignature)              //Our sign algorithm requires Reader 7 or newer.
            {
                TPdfHeaderRecord.SaveToStream(DataStream, TPdfTokens.GetString(TPdfToken.Header16));
            }
            else
            {
                TPdfHeaderRecord.SaveToStream(DataStream, TPdfTokens.GetString(TPdfToken.Header14));
            }

            TPdfBaseRecord.WriteLine(DataStream, TPdfTokens.GetString(TPdfToken.HeaderComment));
        }
Exemplo n.º 27
0
        private byte[] GetPatternDef()
        {
            string s = String.Format(CultureInfo.InvariantCulture, "{0} {1} {2} {3} {4} {5} cm",
                                     ImgWidth,
                                     0, 0,
                                     ImgHeight,
                                     0,
                                     0);

            s += " " + TPdfTokens.GetString(TPdfToken.ImgPrefix) + "0 " + TPdfTokens.GetString(TPdfToken.CommandDo);

            return(Encoding.UTF8.GetBytes(s));
        }
Exemplo n.º 28
0
        public override void WriteFontObject(TPdfStream DataStream, TXRefSection XRef)
        {
            XRef.SetObjectOffset(FontObjId, DataStream);
            TIndirectRecord.SaveHeader(DataStream, FontObjId);
            TDictionaryRecord.BeginDictionary(DataStream);
            TDictionaryRecord.SaveKey(DataStream, TPdfToken.TypeName, TPdfTokens.GetString(TPdfToken.FontName));
            TDictionaryRecord.SaveKey(DataStream, TPdfToken.SubtypeName, TPdfTokens.GetString(TPdfToken.Type1Name));
            TDictionaryRecord.SaveKey(DataStream, TPdfToken.BaseFontName, GetFontName());
            TDictionaryRecord.SaveKey(DataStream, TPdfToken.EncodingName, EncodingType());

            TDictionaryRecord.EndDictionary(DataStream);
            TIndirectRecord.SaveTrailer(DataStream);
        }
Exemplo n.º 29
0
        public void Select(TPdfStream DataStream, real FontSize, ref string LastFont)
        {
            UsedInDoc = true;
            string NewFont = TPdfTokens.GetString(TPdfToken.FontPrefix) + Id.ToString(CultureInfo.InvariantCulture) + " " +
                             PdfConv.CoordsToString(FontSize) + " " + TPdfTokens.GetString(TPdfToken.CommandFont);

            if (LastFont == NewFont)
            {
                return;
            }
            LastFont = NewFont;
            TPdfBaseRecord.WriteLine(DataStream, NewFont);
        }
Exemplo n.º 30
0
        private string GetFontName()
        {
            string Result = "/" + FFontName;

            //Times new roman fonts are differently named. We have italic instead of oblique and -Roman for normal.
            if (String.Compare(Result, 1, TPdfTokens.GetString(TPdfToken.PsTimes), 0, Result.Length, StringComparison.CurrentCulture) == 0)
            {
                if ((FFontStyle & FontStyle.Italic) != 0)
                {
                    if ((FFontStyle & FontStyle.Bold) != 0)
                    {
                        Result += TPdfTokens.GetString(TPdfToken.PsBoldItalic);
                    }
                    else
                    {
                        Result += TPdfTokens.GetString(TPdfToken.PsItalic);
                    }
                }
                else
                if ((FFontStyle & FontStyle.Bold) != 0)
                {
                    Result += TPdfTokens.GetString(TPdfToken.PsBold);
                }

                else
                {
                    Result += TPdfTokens.GetString(TPdfToken.PsRoman);
                }
            }

            else              //Not times.
            {
                if ((FFontStyle & FontStyle.Italic) != 0)
                {
                    if ((FFontStyle & FontStyle.Bold) != 0)
                    {
                        Result += TPdfTokens.GetString(TPdfToken.PsBoldOblique);
                    }
                    else
                    {
                        Result += TPdfTokens.GetString(TPdfToken.PsOblique);
                    }
                }
                else
                if ((FFontStyle & FontStyle.Bold) != 0)
                {
                    Result += TPdfTokens.GetString(TPdfToken.PsBold);
                }
            }
            return(Result);
        }