private void DrawText(Document.DocumentLayout.Layout layout) { StringFormat drawFormat = new StringFormat(); Rectangle location = new Rectangle(layout.location.left, layout.location.top, layout.location.width, layout.location.height); Font font = new Font(layout.font.fontFamily, layout.font.fontSize); SolidBrush drawBrush = new SolidBrush(ColorTranslator.FromHtml(layout.font.fontColor)); layout.location.left += layout.shiftX; layout.location.top += layout.shiftY; drawFormat.Alignment = GetStringAlignment((int)Enum.Parse(typeof(Document.HorizontalAlignment), layout.alignmentX, true)); drawFormat.LineAlignment = GetStringAlignment((int)Enum.Parse(typeof(Document.VerticalAlignment), layout.alignmentY, true)); switch (layout.rotation) { case 90: g.RotateTransform(90); g.TranslateTransform( -(layout.location.left - layout.location.top), -(layout.location.left + layout.location.top + layout.location.width) ); location = new Rectangle(location.Left, location.Top, location.Height, location.Width); break; case 180: g.RotateTransform(180); g.TranslateTransform( -(layout.location.left * 2 + layout.location.width), -(layout.location.height + layout.location.top * 2) ); break; case 270: g.RotateTransform(270); g.TranslateTransform( -(layout.location.left + layout.location.top + layout.location.height), (layout.location.left - layout.location.top) ); location = new Rectangle(location.Left, location.Top, location.Height, location.Width); break; } g.DrawString(layout.value, font, drawBrush, location, drawFormat); }
private void DrawScaledImage(Document.DocumentLayout.Layout layout) { Document.HorizontalAlignment alignmentX = (Document.HorizontalAlignment)Enum.Parse(typeof(Document.HorizontalAlignment), layout.alignmentX, true); Document.VerticalAlignment alignmentY = (Document.VerticalAlignment)Enum.Parse(typeof(Document.VerticalAlignment), layout.alignmentY, true); Image image = Image.FromFile(layout.value); Document.Location scaledLocation = new Document.Location(); scaledLocation.left = layout.location.left; scaledLocation.top = layout.location.top; if (layout.location.width > layout.location.height) { scaledLocation.width = (int)(image.Width * ((double)layout.location.height / image.Height)); scaledLocation.height = layout.location.height; } else { scaledLocation.height = (int)(image.Height * ((double)layout.location.width / image.Width)); scaledLocation.width = layout.location.width; } if ((alignmentX == Document.HorizontalAlignment.middle) && (scaledLocation.width < layout.location.width)) { scaledLocation.left += (layout.location.width - scaledLocation.width) / 2; } else if ((alignmentX == Document.HorizontalAlignment.right) && (scaledLocation.width < layout.location.width)) { scaledLocation.left += (layout.location.width - scaledLocation.width); } if ((alignmentY == Document.VerticalAlignment.middle) && (scaledLocation.height < layout.location.height)) { scaledLocation.top += (layout.location.height - scaledLocation.height) / 2; } else if ((alignmentY == Document.VerticalAlignment.bottom) && (scaledLocation.height < layout.location.height)) { scaledLocation.top += (layout.location.height - scaledLocation.height); } RotateObject(scaledLocation, layout.rotation); g.DrawImage(image, scaledLocation.left, scaledLocation.top, scaledLocation.width, scaledLocation.height); }
public void GenerateLayout() { document.size = documentTemplate.size; document.background = documentTemplate.background; document.rotation = documentTemplate.rotation != null?GetRandomIntInRange(documentTemplate.rotation) : 0; document.shiftX = GetRandomIntInRange(documentTemplate.shiftX); document.shiftY = GetRandomIntInRange(documentTemplate.shiftY); foreach (Document.DocumentTemplate.LayoutTemplate layoutTemplate in documentTemplate.layout) { Document.DocumentLayout.Layout layout = new Document.DocumentLayout.Layout(); layout.type = layoutTemplate.type; layout.location = layoutTemplate.location; if (layout.type != Document.Type.image.ToString()) { if (layout.type == Document.Type.text.ToString()) { string format = layoutTemplate.format ?? documentTemplate.textDefault.format; string value = (layoutTemplate.value ?? GetRandomValueFromFile(layoutTemplate.valuePath)) ?? (documentTemplate.textDefault.value ?? GetRandomValueFromFile(documentTemplate.textDefault.valuePath)); layout.value = String.Format(format, value); } else { string format = layoutTemplate.format ?? documentTemplate.dateDefault.format; string value = ( layoutTemplate.value ?? GetRandomDateInRange(layoutTemplate.dateRange) ) ?? ( documentTemplate.dateDefault.value ?? GetRandomDateInRange(documentTemplate.dateDefault.dateRange) ); layout.value = String.Format(format, GetDateFromString(value)); } layout.alignmentX = GetRandomStringFromArray( layoutTemplate.alignmentX ?? (layout.type == Document.Type.text.ToString() ? documentTemplate.textDefault.alignmentX : documentTemplate.dateDefault.alignmentY) ); layout.alignmentY = GetRandomStringFromArray( layoutTemplate.alignmentY ?? (layout.type == Document.Type.text.ToString() ? documentTemplate.textDefault.alignmentY : documentTemplate.dateDefault.alignmentY) ); layout.font = GetRandomFontFromResources( layoutTemplate.font ?? (layout.type == Document.Type.text.ToString() ? documentTemplate.textDefault.font : documentTemplate.dateDefault.font) ); layout.rotation = GetRandomTextRotationInRange( layoutTemplate.rotation ?? (layout.type == Document.Type.text.ToString() ? documentTemplate.textDefault.rotation : documentTemplate.dateDefault.rotation) ); layout.shiftX = GetRandomIntInRange( layoutTemplate.shiftX ?? (layout.type == Document.Type.text.ToString() ? documentTemplate.textDefault.shiftX : documentTemplate.dateDefault.shiftX) ); layout.shiftY = GetRandomIntInRange( layoutTemplate.shiftY ?? (layout.type == Document.Type.text.ToString() ? documentTemplate.textDefault.shiftY : documentTemplate.dateDefault.shiftY) ); } else { layout.alignmentX = GetRandomStringFromArray(layoutTemplate.alignmentX ?? documentTemplate.imageDefault.alignmentX); layout.alignmentY = GetRandomStringFromArray(layoutTemplate.alignmentY ?? documentTemplate.imageDefault.alignmentY); layout.rotation = GetRandomIntInRange(layoutTemplate.rotation ?? documentTemplate.imageDefault.rotation); layout.value = (layoutTemplate.value ?? GetRandomValueFromFile(layoutTemplate.valuePath)) ?? (documentTemplate.imageDefault.value ?? GetRandomValueFromFile(documentTemplate.imageDefault.valuePath)); layout.shiftX = GetRandomIntInRange(layoutTemplate.shiftX ?? documentTemplate.imageDefault.shiftX); layout.shiftY = GetRandomIntInRange(layoutTemplate.shiftY ?? documentTemplate.imageDefault.shiftY); } document.layout.Add(layout); } }