public virtual void BeginMarkerContentOperatorTest() { PdfCanvasProcessor processor = new _PdfCanvasProcessor_42(new FilteredEventListener()); IContentOperator contentOperator = processor.RegisterContentOperator("BMC", null); processor.RegisterContentOperator("BMC", contentOperator); contentOperator.Invoke(processor, null, JavaCollectionsUtil.SingletonList((PdfObject)null)); }
public void Invoke(PdfContentStreamProcessor processor, PdfLiteral oper, List <PdfObject> operands) { var proc = (PdfContentStreamEditor)processor; var isXObject = "Do" == oper.ToString(); if (isXObject) { if (proc.XObjectLevel == 0) { proc.IsWatermark = false; } proc.XObjectLevel++; } if (!isXObject && operands.Count == 2 && proc.XObjectLevel > 0 && proc.RemoveXObjects) { var op = operands[1].ToString(); if ((op == "Tj" || op == "TJ") && proc.Watermark.IsMatch(operands[0].ToString())) { proc.IsWatermark = true; proc.WatermarkCount++; } } OriginalOperator?.Invoke(processor, oper, operands); if (isXObject) { proc.XObjectLevel--; } if (isXObject && proc.IsWatermark && proc.RemoveXObjects) { return; } if (proc.XObjectLevel == 0) { ((PdfContentStreamEditor)processor).Write(processor, oper, operands); } }
public virtual void Invoke(PdfContentStreamProcessor pdfContentStreamProcessor, PdfLiteral oper, List <PdfObject> operands) { String operatorStr = oper.ToString(); PdfContentByte canvas = cleanUpStrategy.Context.Canvas; PRStream xFormStream = null; bool disableOutput = pathConstructionOperators.Contains(operatorStr) || pathPaintingOperators.Contains(operatorStr) || clippingPathOperators.Contains(operatorStr); GraphicsState gs = pdfContentStreamProcessor.Gs(); // key - number of a string in the TJ operator, value - number following the string; the first number without string (if it's presented) is stored under 0. // BE AWARE: zero-length strings are ignored!!! IDictionary <int, float> structuredTJoperands = null; if ("Do" == operatorStr) { if (operands.Count == 2 && operands[0].IsName()) { PdfDictionary xObjResources = cleanUpStrategy.Context.Resources.GetAsDict(PdfName.XOBJECT); if (xObjResources != null) { PdfStream xObj = xObjResources.GetAsStream((PdfName)operands[0]); if (xObj is PRStream && xObj.GetAsName(PdfName.SUBTYPE) != null && xObj.GetAsName(PdfName.SUBTYPE).CompareTo(PdfName.FORM) == 0) { xFormStream = (PRStream)xObj; cleanUpStrategy.RegisterNewContext(xObj.GetAsDict(PdfName.RESOURCES), null); } } } } originalContentOperator.Invoke(pdfContentStreamProcessor, oper, operands); IList <PdfCleanUpContentChunk> chunks = cleanUpStrategy.Chunks; if (xFormStream != null) { xFormStream.SetData(cleanUpStrategy.Context.Canvas.ToPdf(cleanUpStrategy.Context.Canvas.PdfWriter)); cleanUpStrategy.PopContext(); canvas = cleanUpStrategy.Context.Canvas; } if ("Do" == operatorStr) { if (chunks.Count > 0 && chunks[0] is PdfCleanUpContentChunk.Image) { PdfCleanUpContentChunk.Image chunk = (PdfCleanUpContentChunk.Image)chunks[0]; if (chunk.Visible) { PdfDictionary xObjResources = cleanUpStrategy.Context.Resources.GetAsDict(PdfName.XOBJECT); PRStream imageStream = (PRStream)xObjResources.GetAsStream((PdfName)operands[0]); UpdateImageStream(imageStream, chunk.NewImageData); } else { disableOutput = true; } } } else if (lineStyleOperators.Contains(operatorStr)) { disableOutput = true; } else if (textShowingOperators.Contains(operatorStr) && !AllChunksAreVisible(cleanUpStrategy.Chunks)) { disableOutput = true; if ("'" == operatorStr) { canvas.InternalBuffer.Append(TStar); } else if ("\"" == operatorStr) { operands[0].ToPdf(canvas.PdfWriter, canvas.InternalBuffer); canvas.InternalBuffer.Append(Tw); operands[1].ToPdf(canvas.PdfWriter, canvas.InternalBuffer); canvas.InternalBuffer.Append(TcTStar); } else if ("TJ" == operatorStr) { structuredTJoperands = StructureTJarray((PdfArray)operands[0]); } WriteTextChunks(structuredTJoperands, chunks, canvas, gs.CharacterSpacing, gs.WordSpacing, gs.FontSize, gs.HorizontalScaling); } else if (pathPaintingOperators.Contains(operatorStr)) { WritePath(operatorStr, canvas, gs.ColorSpaceStroke); } else if (strokeColorOperators.Contains(operatorStr)) { // Replace current color with the new one. cleanUpStrategy.Context.PopStrokeColor(); cleanUpStrategy.Context.PushStrokeColor(operands); } else if ("q" == operatorStr) { cleanUpStrategy.Context.PushStrokeColor(cleanUpStrategy.Context.PeekStrokeColor()); } else if ("Q" == operatorStr) { cleanUpStrategy.Context.PopStrokeColor(); } if (!disableOutput) { WriteOperands(canvas, operands); } cleanUpStrategy.ClearChunks(); }
public void Invoke(PdfContentStreamProcessor pdfContentStreamProcessor, PdfLiteral @operator, List <PdfObject> operands) { String operatorStr = @operator.ToString(); PdfContentByte canvas = cleanUpStrategy.Context.Canvas; PRStream xFormStream = null; // key - number of a string in the TJ operator, value - number following the string; the first number without string (if it's presented) is stored under 0. // BE AWARE: zero-length strings are ignored!!! IDictionary <int, float> structuredTJoperands = null; if ("Do" == operatorStr) { if (operands.Count == 2 && operands[0].IsName()) { PdfDictionary xObjResources = cleanUpStrategy.Context.Resources.GetAsDict(PdfName.XOBJECT); if (xObjResources != null) { PdfStream xObj = xObjResources.GetAsStream((PdfName)operands[0]); if (xObj is PRStream && xObj.GetAsName(PdfName.SUBTYPE) != null && xObj.GetAsName(PdfName.SUBTYPE).CompareTo(PdfName.FORM) == 0) { xFormStream = (PRStream)xObj; cleanUpStrategy.RegisterNewContext(xObj.GetAsDict(PdfName.RESOURCES), null); } } } } originalContentOperator.Invoke(pdfContentStreamProcessor, @operator, operands); IList <PdfCleanUpContentChunk> chunks = cleanUpStrategy.Chunks; bool disableOutput = false; if (xFormStream != null) { xFormStream.SetData(cleanUpStrategy.Context.Canvas.ToPdf(cleanUpStrategy.Context.Canvas.PdfWriter)); cleanUpStrategy.PopContext(); canvas = cleanUpStrategy.Context.Canvas; } if ("Do" == operatorStr) { if (chunks.Count > 0 && chunks[0].IsImage()) { PdfCleanUpContentChunk chunk = chunks[0]; if (chunk.IsVisible()) { PdfDictionary xObjResources = cleanUpStrategy.Context.Resources.GetAsDict(PdfName.XOBJECT); PRStream imageStream = (PRStream)xObjResources.GetAsStream((PdfName)operands[0]); UpdateImage(imageStream, chunk.NewImageData); } else { disableOutput = true; } } } else if ("q" == operatorStr) { cleanUpStrategy.Context.SaveGraphicsState(); } else if ("Q" == operatorStr) { cleanUpStrategy.Context.RestoreGraphicsState(); } else if ("Tf" == operatorStr) { cleanUpStrategy.Context.FontSize = ((PdfNumber)operands[1]).FloatValue; } else if ("Tc" == operatorStr) { cleanUpStrategy.Context.CharacterSpacing = ((PdfNumber)operands[0]).FloatValue; } else if ("Tw" == operatorStr) { cleanUpStrategy.Context.WordSpacing = ((PdfNumber)operands[0]).FloatValue; } else if ("Tz" == operatorStr) { cleanUpStrategy.Context.HorizontalScaling = ((PdfNumber)operands[0]).FloatValue; } else if (textShowingOperators.Contains(operatorStr) && !AllChunksAreVisible(cleanUpStrategy.Chunks)) { disableOutput = true; if ("'" == operatorStr) { canvas.InternalBuffer.Append(TStar); } else if ("\"" == operatorStr) { operands[0].ToPdf(canvas.PdfWriter, canvas.InternalBuffer); canvas.InternalBuffer.Append(Tw); operands[1].ToPdf(canvas.PdfWriter, canvas.InternalBuffer); canvas.InternalBuffer.Append(TcTStar); cleanUpStrategy.Context.CharacterSpacing = ((PdfNumber)operands[1]).FloatValue; } else if ("TJ" == operatorStr) { structuredTJoperands = StructureTJarray((PdfArray)operands[0]); } RenderChunks(structuredTJoperands, chunks, canvas); } else if ("\"" == operatorStr) { cleanUpStrategy.Context.CharacterSpacing = ((PdfNumber)operands[1]).FloatValue; } if (!disableOutput) { int index = 0; foreach (PdfObject o in operands) { ToPdf(o, canvas.PdfWriter, canvas.InternalBuffer); canvas.InternalBuffer.Append(operands.Count > ++index ? (byte)' ' : (byte)'\n'); } } cleanUpStrategy.ClearChunks(); }