public AffineTransform(AffineTransform t) { this.type = t.type; this.m00 = t.m00; this.m10 = t.m10; this.m01 = t.m01; this.m11 = t.m11; this.m02 = t.m02; this.m12 = t.m12; }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { // step 1 Rectangle rect = new Rectangle(-595, -842, 595, 842); using (MemoryStream ms = new MemoryStream()) { using (Document document = new Document(rect)) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, ms); // step 3 document.Open(); // step 4 PdfContentByte canvas = writer.DirectContent; // draw coordinate system canvas.MoveTo(-595, 0); canvas.LineTo(595, 0); canvas.MoveTo(0, -842); canvas.LineTo(0, 842); canvas.Stroke(); // read the PDF with the logo PdfReader reader = new PdfReader(RESOURCE); PdfTemplate template = writer.GetImportedPage(reader, 1); // add it canvas.SaveState(); canvas.AddTemplate(template, 0, 0); AffineTransform af = new AffineTransform(); af.Translate(-595, 0); af.Scale(0.5f, 0.5f); canvas.Transform(af); canvas.AddTemplate(template, 0, 0); canvas.ConcatCTM(AffineTransform.GetTranslateInstance(595, 595)); canvas.AddTemplate(template, 0, 0); canvas.RestoreState(); canvas.SaveState(); af = new AffineTransform(1f, 0f, 0.4f, 1f, -750f, -650f); canvas.AddTemplate(template, af); canvas.RestoreState(); canvas.SaveState(); af = new AffineTransform(0, -1, -1, 0, 650, 0); canvas.AddTemplate(template, af); af = new AffineTransform(0, -0.2f, -0.5f, 0, 350, 0); canvas.AddTemplate(template, af); canvas.RestoreState(); } return ms.ToArray(); } }
public void Rotate(double angle) { Concatenate(AffineTransform.GetRotateInstance(angle)); }
/** * adds a template with the given matrix. * @param template template to add * @param transform transform to apply to the template prior to adding it. * @param tagContent <code>true</code> - template content will be tagged(all that will be added after), <code>false</code> - only a Do operator will be tagged. * taken into account only if <code>isTagged()</code> - <code>true</code>. */ public void AddTemplate(PdfTemplate template, AffineTransform transform, bool tagContent) { double[] matrix = new double[6]; transform.GetMatrix(matrix); AddTemplate(template, (float) matrix[0], (float) matrix[1], (float) matrix[2], (float) matrix[3], (float) matrix[4], (float) matrix[5], tagContent); }
/** * Concatenate a matrix to the current transformation matrix. * @param transform added to the Current Transformation Matrix */ public void ConcatCTM(AffineTransform transform) { double[] matrix = new double[6]; transform.GetMatrix(matrix); ConcatCTM((float) matrix[0], (float) matrix[1], (float) matrix[2], (float) matrix[3], (float) matrix[4], (float) matrix[5]); }
/** * adds an image with the given matrix. * @param image image to add * @param transform transform to apply to the template prior to adding it. */ public void AddImage(Image image, AffineTransform transform) { double[] matrix = new double[6]; transform.GetMatrix(matrix); AddImage(image, (float)matrix[0], (float)matrix[1], (float)matrix[2], (float)matrix[3], (float)matrix[4], (float) matrix[5], false); }
private byte[] CreatePdfWithRotatedXObject(String xobjectText) { MemoryStream baos = new MemoryStream(); Document doc = new Document(); PdfWriter writer = PdfWriter.GetInstance(doc, baos); writer.CompressionLevel = 0; doc.Open(); doc.Add(new Paragraph("A")); doc.Add(new Paragraph("B")); bool rotate = true; PdfTemplate template = writer.DirectContent.CreateTemplate(20, 100); template.SetColorStroke(BaseColor.GREEN); template.Rectangle(0, 0, template.Width, template.Height); template.Stroke(); AffineTransform tx = new AffineTransform(); if(rotate) { tx.Translate(0, template.Height); tx.Rotate(-90 / 180f * Math.PI); } template.Transform(tx); template.BeginText(); template.SetFontAndSize(BaseFont.CreateFont(), 12); if (rotate) template.MoveText(0, template.Width - 12); else template.MoveText(0, template.Height - 12); template.ShowText(xobjectText); template.EndText(); Image xobjectImage = Image.GetInstance(template); if (rotate) xobjectImage.RotationDegrees = 90; doc.Add(xobjectImage); doc.Add(new Paragraph("C")); doc.Close(); return baos.ToArray(); }
private Point2D[] TransformPoints(Matrix transormationMatrix, bool inverse, params Point2D[] points) { AffineTransform t = new AffineTransform(transormationMatrix[Matrix.I11], transormationMatrix[Matrix.I12], transormationMatrix[Matrix.I21], transormationMatrix[Matrix.I22], transormationMatrix[Matrix.I31], transormationMatrix[Matrix.I32]); Point2D[] transformed = new Point2D[points.Length]; if (inverse) { t = t.CreateInverse(); } t.Transform(points, 0, transformed, 0, points.Length); return transformed; }
public void ApplyCTM(AffineTransform ctm) { PdfArray origRect = GetAsArray(PdfName.RECT); if(origRect != null) { PdfRectangle rect; if(origRect.Size == 4) { rect = new PdfRectangle(origRect.GetAsNumber(0).FloatValue, origRect.GetAsNumber(1).FloatValue, origRect.GetAsNumber(2).FloatValue, origRect.GetAsNumber(3).FloatValue); } else { rect = new PdfRectangle(origRect.GetAsNumber(0).FloatValue, origRect.GetAsNumber(1).FloatValue); } Put(PdfName.RECT, rect.Transform(ctm)); } }
virtual public void SetTransform(AffineTransform t) { type = t.type; SetTransform(t.m00, t.m10, t.m01, t.m11, t.m02, t.m12); }
virtual public void preConcatenate(AffineTransform t) { SetTransform(Multiply(this, t)); }
virtual public void Concatenate(AffineTransform t) { SetTransform(Multiply(t, this)); }
virtual public void Rotate(double angle, double px, double py) { Concatenate(AffineTransform.GetRotateInstance(angle, px, py)); }
virtual public void Shear(double shx, double shy) { Concatenate(AffineTransform.GetShearInstance(shx, shy)); }
virtual public void Scale(double scx, double scy) { Concatenate(AffineTransform.GetScaleInstance(scx, scy)); }
virtual public void Translate(double mx, double my) { Concatenate(AffineTransform.GetTranslateInstance(mx, my)); }
virtual public PdfRectangle Transform(AffineTransform transform) { float[] pts = {llx, lly, urx, ury}; transform.Transform(pts, 0, pts, 0, 2); float[] dstPts = {pts[0], pts[1], pts[2], pts[3]}; if (pts[0] > pts[2]) { dstPts[0] = pts[2]; dstPts[2] = pts[0]; } if (pts[1] > pts[3]) { dstPts[1] = pts[3]; dstPts[3] = pts[1]; } return new PdfRectangle(dstPts[0], dstPts[1], dstPts[2], dstPts[3]); }
/** * Adds an <CODE>Image</CODE> to the page. The positioning of the <CODE>Image</CODE> * is done with the transformation matrix. To position an <CODE>image</CODE> at (x,y) * use AddImage(image, image_width, 0, 0, image_height, x, y). The image can be placed inline. * @param image the <CODE>Image</CODE> object * @param a an element of the transformation matrix * @param b an element of the transformation matrix * @param c an element of the transformation matrix * @param d an element of the transformation matrix * @param e an element of the transformation matrix * @param f an element of the transformation matrix * @param inlineImage <CODE>true</CODE> to place this image inline, <CODE>false</CODE> otherwise * @throws DocumentException on error */ public virtual void AddImage(Image image, float a, float b, float c, float d, float e, float f, bool inlineImage) { if (image.Layer != null) BeginLayer(image.Layer); if (IsTagged()) { if (inText) EndText(); AffineTransform transform = new AffineTransform(a, b, c, d, e, f); Point2D[] src = new Point2D.Float[] { new Point2D.Float(0, 0), new Point2D.Float(1, 0), new Point2D.Float(1, 1), new Point2D.Float(0, 1) }; Point2D[] dst = new Point2D.Float[4]; transform.Transform(src, 0, dst, 0, 4); float left = float.MaxValue; float right = float.MinValue; float bottom = float.MaxValue; float top = float.MinValue; for (int i = 0; i < 4; i++) { if (dst[i].GetX() < left) left = (float) dst[i].GetX(); if (dst[i].GetX() > right) right = (float) dst[i].GetX(); if (dst[i].GetY() < bottom) bottom = (float) dst[i].GetY(); if (dst[i].GetY() > top) top = (float) dst[i].GetY(); } image.SetAccessibleAttribute(PdfName.BBOX, new PdfArray(new float[] {left, bottom, right, top})); } if (writer != null && image.IsImgTemplate()) { writer.AddDirectImageSimple(image); PdfTemplate template = image.TemplateData; if (image.GetAccessibleAttributes() != null) { foreach (PdfName key in image.GetAccessibleAttributes().Keys) { template.SetAccessibleAttribute(key, image.GetAccessibleAttribute(key)); } } float w = template.Width; float h = template.Height; AddTemplate(template, a / w, b / w, c / h, d / h, e, f); } else { content.Append("q "); content.Append(a).Append(' '); content.Append(b).Append(' '); content.Append(c).Append(' '); content.Append(d).Append(' '); content.Append(e).Append(' '); content.Append(f).Append(" cm"); if (inlineImage) { content.Append("\nBI\n"); PdfImage pimage = new PdfImage(image, "", null); if (image is ImgJBIG2) { byte[] globals = ((ImgJBIG2)image).GlobalBytes; if (globals != null) { PdfDictionary decodeparms = new PdfDictionary(); decodeparms.Put(PdfName.JBIG2GLOBALS, writer.GetReferenceJBIG2Globals(globals)); pimage.Put(PdfName.DECODEPARMS, decodeparms); } } PdfWriter.CheckPdfIsoConformance(writer, PdfIsoKeys.PDFISOKEY_INLINE_IMAGE, pimage); foreach (PdfName key in pimage.Keys) { if (!abrev.ContainsKey(key)) continue; PdfObject value = pimage.Get(key); String s = abrev[key]; content.Append(s); bool check = true; if (key.Equals(PdfName.COLORSPACE) && value.IsArray()) { PdfArray ar = (PdfArray)value; if (ar.Size == 4 && PdfName.INDEXED.Equals(ar.GetAsName(0)) && ar[1].IsName() && ar[2].IsNumber() && ar[3].IsString() ) { check = false; } } if (check && key.Equals(PdfName.COLORSPACE) && !value.IsName()) { PdfName cs = writer.GetColorspaceName(); PageResources prs = PageResources; prs.AddColor(cs, writer.AddToBody(value).IndirectReference); value = cs; } value.ToPdf(null, content); content.Append('\n'); } content.Append("ID\n"); pimage.WriteContent(content); content.Append("\nEI\nQ").Append_i(separator); } else { PdfName name; PageResources prs = PageResources; Image maskImage = image.ImageMask; if (maskImage != null) { name = writer.AddDirectImageSimple(maskImage); prs.AddXObject(name, writer.GetImageReference(name)); } name = writer.AddDirectImageSimple(image); name = prs.AddXObject(name, writer.GetImageReference(name)); content.Append(' ').Append(name.GetBytes()).Append(" Do Q").Append_i(separator); } } if (image.HasBorders()) { SaveState(); float w = image.Width; float h = image.Height; ConcatCTM(a / w, b / w, c / h, d / h, e, f); Rectangle(image); RestoreState(); } if (image.Layer != null) EndLayer(); Annotation annot = image.Annotation; if (annot == null) return; float[] r = new float[unitRect.Length]; for (int k = 0; k < unitRect.Length; k += 2) { r[k] = a * unitRect[k] + c * unitRect[k + 1] + e; r[k + 1] = b * unitRect[k] + d * unitRect[k + 1] + f; } float llx = r[0]; float lly = r[1]; float urx = llx; float ury = lly; for (int k = 2; k < r.Length; k += 2) { llx = Math.Min(llx, r[k]); lly = Math.Min(lly, r[k + 1]); urx = Math.Max(urx, r[k]); ury = Math.Max(ury, r[k + 1]); } annot = new Annotation(annot); annot.SetDimensions(llx, lly, urx, ury); PdfAnnotation an = PdfAnnotationsImp.ConvertAnnotation(writer, annot, new Rectangle(llx, lly, urx, ury)); if (an == null) return; AddAnnotation(an); }
public static AffineTransform GetTranslateInstance(double mx, double my) { AffineTransform t = new AffineTransform(); t.SetToTranslation(mx, my); return t; }
internal void CopyParameters(GraphicState cp) { fontDetails = cp.fontDetails; colorDetails = cp.colorDetails; size = cp.size; xTLM = cp.xTLM; yTLM = cp.yTLM; aTLM = cp.aTLM; bTLM = cp.bTLM; cTLM = cp.cTLM; dTLM = cp.dTLM; tx = cp.tx; leading = cp.leading; scale = cp.scale; charSpace = cp.charSpace; wordSpace = cp.wordSpace; textColorFill = cp.textColorFill; colorFill = cp.colorFill; textColorStroke = cp.textColorStroke; colorStroke = cp.colorStroke; CTM = (AffineTransform)cp.CTM.Clone(); textRenderMode = cp.textRenderMode; extGState = cp.extGState; }
public static AffineTransform GetScaleInstance(double scx, double scY) { AffineTransform t = new AffineTransform(); t.SetToScale(scx, scY); return t; }
/** * Changes the text matrix. * <P> * @param transform overwrite the current text matrix with this one */ public void SetTextMatrix(AffineTransform transform) { double[] matrix = new double[6]; transform.GetMatrix(matrix); SetTextMatrix((float)matrix[0], (float)matrix[1], (float)matrix[2], (float)matrix[3], (float)matrix[4], (float)matrix[5]); }
public static AffineTransform GetShearInstance(double shx, double shy) { AffineTransform m = new AffineTransform(); m.SetToShear(shx, shy); return m; }
/** * adds a template with the given matrix. * @param template template to add * @param transform transform to apply to the template prior to adding it. */ public void AddTemplate(PdfTemplate template, AffineTransform transform) { AddTemplate(template, transform, false); }
public static AffineTransform GetRotateInstance(double angle, double x, double y) { AffineTransform t = new AffineTransform(); t.SetToRotation(angle, x, y); return t; }
/** Concatenates a transformation to the current transformation * matrix. * @param af the transformation */ public void Transform(AffineTransform af) { if (inText && IsTagged()) { EndText(); } double[] matrix = new double[6]; af.GetMatrix(matrix); state.CTM.Concatenate(af); content.Append(matrix[0]).Append(' ').Append(matrix[1]).Append(' ').Append(matrix[2]).Append(' '); content.Append(matrix[3]).Append(' ').Append(matrix[4]).Append(' ').Append(matrix[5]).Append(" cm").Append_i(separator); }
/** * Multiply matrix of two AffineTransform objects * @param t1 - the AffineTransform object is a multiplicand * @param t2 - the AffineTransform object is a multiplier * @return an AffineTransform object that is a result of t1 multiplied by matrix t2. */ AffineTransform Multiply(AffineTransform t1, AffineTransform t2) { return new AffineTransform( t1.m00 * t2.m00 + t1.m10 * t2.m01, // m00 t1.m00 * t2.m10 + t1.m10 * t2.m11, // m01 t1.m01 * t2.m00 + t1.m11 * t2.m01, // m10 t1.m01 * t2.m10 + t1.m11 * t2.m11, // m11 t1.m02 * t2.m00 + t1.m12 * t2.m01 + t2.m02, // m02 t1.m02 * t2.m10 + t1.m12 * t2.m11 + t2.m12);// m12 }