The page margin and the start of the paragraph are not match in ITextsharp - itext

I am using Itextsharp.
doc.SetMargins(36, 36, 36, 36);
doc.NewPage();
writer.PageEvent = new PDFPageEvent();
int horizontalMarginForColumn = 54;
int bottomMarginForColumn = 72;
int topMarginForColumn = 112;
int interval = 8;
Rectangle pageNumberLeftLocation = new Rectangle(horizontalMarginForColumn, doc.PageSize.Height - 36, horizontalMarginForColumn + 36, doc.PageSize.Height);
paragraph = new Paragraph();
paragraph.SpacingBefore = 0;
paragraph.Alignment = Element.ALIGN_LEFT;
line = new LineSeparator(1f, 100f, BaseColor.BLACK, 0, 0f);
line.Alignment = Element.ALIGN_CENTER;
paragraph.Add(new Chunk(line));
paragraph.Add(Chunk.NEWLINE);
chunk = new Chunk(" Index", FontFactory.GetFont("Arial", 15, Font.BOLD));
chunk.setLineHeight(23);
paragraph.Add(chunk);
paragraph.Add(Chunk.NEWLINE);
paragraph.Add(new Chunk(line));
doc.Add(paragraph);
writer.DirectContent.SetColorStroke(BaseColor.BLUE);
writer.DirectContent.Rectangle(pageNumberLeftLocation.Left, pageNumberLeftLocation.Bottom, pageNumberLeftLocation.Width, pageNumberLeftLocation.Height);
writer.DirectContent.Stroke();
The top margin of the page is 36, and the height of the rectangle is 36. So I assume that the bottom line of the rectangle and the beginning of the paragraph which is a black line, sholud be in the same vertical position. But you can see, there is some space between the two. Why is this? Thank you.

Related

How can i make fair value gap boxes and hide when filled automatically? calculation would be low[0] > high{-[2]

Fair value gap coding in pinescript?
I have tried to write code but unable to add more functions on it.
//#version=5
indicator('Fair Value Gap Akash Mehto', overlay=true)
boxLength = input.int(title='boxLength', defval=6, group='General', inline='General')
boxTransparency = input.int(title='boxTransparency', defval=85, group='General', inline='General')
bullishFvg = low[0] > high[2]
bearishFvg = high[0] < low[2]
if bullishFvg
box.new(left=bar_index - 2, top=low[0], right=bar_index + boxLength, bottom=high[2], bgcolor=color.new(color.rgb(28, 202, 121), boxTransparency), text="FVG", text_size = "tiny", text_halign = text.align_right, text_color = color.green, border_color=color.new(color.green, boxTransparency))
if bearishFvg
box.new(left=bar_index - 2, top=low[2], right=bar_index + boxLength, bottom=high[0], bgcolor=color.new(color.rgb(240, 46, 46), boxTransparency), text="FVG", text_size = "tiny", text_halign = text.align_right, text_color = color.red, border_color=color.new(color.green, boxTransparency))

Gmap.Net save image around selected marker

I have an application with GMap.Net showing various markers. I know how to take a screen shot of the current map and markers:
Dim sImageName As String = DateTime.Now.ToString(Format("yyyyMMdd-HHmmss")) & ".png"
Dim ThisMap As New Bitmap(Form2.myMap.Width, Form2.myMap.Height)
Form2.myMap.DrawToBitmap(ThisMap, New Rectangle(0, 0, Form2.myMap.Width, Form2.myMap.Height))
ThisMap.Save(sImagesFolder & sImageName)
What I would like to do is create an image for a selected marker. Instead of the image being the entire map shown on screen, it would center on the marker and show 100 pixels in each direction.
Does anyone know how to do that?
This is what I tried, but it gives me a blank image-- nothing shows up. I feel like this should be working...
Private Sub MyMap_OnMarkerClick(item As GMapMarker, e As Windows.Forms.MouseEventArgs) Handles myMap.OnMarkerClick
SelMarkerX = e.X
SelMarkerY = e.Y
Dim sImageName As String = DateTime.Now.ToString(Format("yyyyMMdd-HHmmss")) & ".png"
Dim ThisMap As New Bitmap(140,100)
myMap.DrawToBitmap(ThisMap, New Rectangle(SelMarkerX - 70, SelMarkerY - 50, 140, 100))
ThisMap.Save(sImagesFolder & sImageName)
End Sub
I just don't get it. If I write:
myMap.DrawToBitmap(ThisMap, New Rectangle(0, 0, 140, 100)
then I get what you might expect. I get the upper left corner of the existing map from 0 to 140 horizontally and 0 to 100 vertically. If I change it to this:
myMap.DrawToBitmap(ThisMap, New Rectangle(10, 0, 140, 100)
then I get 0 to 130 horizontally and not 10 to 140.
Well, I couldn't figure out how to do it with Gmap, so I wondered if I could crop it outside of Gmap and apparently that is common. Here is the code I used.
Dim ThisMap As New Bitmap(Form2.myMap.Width, Form2.myMap.Height)
Form2.myMap.DrawToBitmap(ThisMap, New Rectangle(0, 0, Form2.myMap.Width, Form2.myMap.Height))
ThisMap.Save(sImagesFolder & sImageName)
Dim LocX = SelMarkerX - 160 'x cord. of where crop starts
Dim LocY = SelMarkerY - 120 'y cord. of where crop starts
Dim CropW = 320 'Crop width
Dim CropH = 240 'Crop height
Dim CropRect As New Rectangle(LocX, LocY, CropW, CropH)
Dim OriginalImage = ThisMap
Dim CropImage = New Bitmap(CropRect.Width, CropRect.Height)
Using grp = Graphics.FromImage(CropImage)
grp.DrawImage(OriginalImage, New Rectangle(0, 0, CropRect.Width, CropRect.Height), CropRect, GraphicsUnit.Pixel)
CropImage.Save(sImagesFolder & sImageName)
End Using

ItextSharp - Add text watermark on separate lines

I am trying to add a watermak on a pdf file with itextsharp 5.1.2.0 .
I want the watermark to be in the center of the page, with a 45° angle, with a border around these lines.
Here is my code :
for (int i = 1; i <= reader.NumberOfPages; i++)
{
iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents;
pdfPageContents = pdfStamper.GetOverContent(i);
pdfPageContents.BeginText();
PdfGState gstate = new PdfGState();
gstate.FillOpacity = 0.4f;
gstate.StrokeOpacity = 0.4f;
pdfPageContents.SaveState();
pdfPageContents.SetGState(gstate);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
pdfPageContents.SetRGBColorFill(255, 0, 0);
double radians = Math.Atan2(pageSize.Height, pageSize.Width);
float textAngle = radians * (180 / Math.PI);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Phrase 1 This is Phrase 2 and must be centered below phrase 1" , pageSize.Width / 2, pageSize.Height / 2, textAngle);
//pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Phrase 1", pageSize.Width / 2, pageSize.Height / 2, textAngle);
//pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "This is Phrase 2 and must be centered below phrase 1", pageSize.Width / 2 + 20 , pageSize.Height / 2 - 20, textAngle);
}
This adds the watermark on one line.
I am able to have two lines with the commented code.
What i dont like are the hardcoded values for the position for the second line.
I am sure there must be a better way of achieving this.
Regarding the border i did not manage to add it around the lines.
If someone can help me with this case .
Thanks
basically what I do for my project is as under... here font_size and style are the variables... also llx, lly, urx,ury are variable for the the corrdinates position and size.. in alignment variable, you can set the alignment easily....
for opacity your code will work fine(you can add state options)
Dim Font = New Font(arial, FONT_SIZE, STYLE, iTextSharp.text.Color.BLACK )
dim text = GetRowValue(row, "CONTROL_MAP")
Dim ct As ColumnText = New ColumnText(pdf_cb)
ct.SetSimpleColumn(LLX, LLY, URX, URY, FONT_SIZE, ALIGNMENT)
ct.SetText(New Paragraph(0, text, Font))
ct.Go()

Pyglet: why does the text in my example become fuzzy when I resize the window?

In the following toy example, I have a text layout in a resize-able window. When the window is re-sized, the text becomes fuzzy for some resolutions (give it a try). Why is that?
import pyglet
width, height = 500, 500
window = pyglet.window.Window(width, height, resizable=True)
batch = pyglet.graphics.Batch()
document = pyglet.text.document.FormattedDocument()
document.insert_text(0, 'Hello world!', attributes=dict(font_name='Arial', font_size=12, color=(255, 255, 255, 255)))
layout = pyglet.text.layout.IncrementalTextLayout(document, width, height, multiline=True, batch=batch)
# for drawing graphics
pyglet.gl.glLineWidth(3)
outline = batch.add(4, pyglet.gl.GL_LINE_LOOP, None, ('v2f', (0, 0, width, 0, width, height, 0, height)), ('c4B', (255, 0, 0, 0)*4))
#window.event
def on_resize(width, height):
document.delete_text(0, len(document.text))
document.insert_text(0, "Layout has been resized to {}x{}".format(0.9*width, 0.9*height))
layout.width = 0.9*width
layout.height = 0.9*height
layout.x = (width - layout.width)*0.5
layout.y = (height - layout.height)*0.5
outline.vertices = (layout.x, layout.y, layout.width + layout.x, layout.y, layout.width + layout.x, layout.height + layout.y, layout.x, layout.height + layout.y)
#window.event
def on_draw():
window.clear()
batch.draw()
pyglet.app.run()
Things start to become wonky because the layout is being given decimal width and height. Fixing that removes the fuzzy text issues:
import pyglet
width, height = 500, 500
window = pyglet.window.Window(width, height, resizable=True)
batch = pyglet.graphics.Batch()
document = pyglet.text.document.FormattedDocument()
document.insert_text(0, 'Hello world!', attributes=dict(font_name='Arial', font_size=12, color=(255, 255, 255, 255)))
layout = pyglet.text.layout.IncrementalTextLayout(document, width, height, multiline=True, batch=batch)
# for drawing graphics
pyglet.gl.glLineWidth(3)
outline = batch.add(4, pyglet.gl.GL_LINE_LOOP, None, ('v2f', (0, 0, width, 0, width, height, 0, height)), ('c4B', (255, 0, 0, 0)*4))
#window.event
def on_resize(width, height):
width, height = int(width), int(height)
window.set_size(width, height)
layout_width, layout_height = int(0.9*width), int(0.9*height)
document.delete_text(0, len(document.text))
document.insert_text(0, "Layout has been resized to {}x{}".format(layout_width, layout_height))
layout.width, layout.height = layout_width, layout_height
layout.x = int((width - layout_width)*0.5)
layout.y = int((height - layout_height)*0.5)
outline.vertices = (layout.x, layout.y, layout.width + layout.x, layout.y, layout.width + layout.x, layout.height + layout.y, layout.x, layout.height + layout.y)
#window.event
def on_draw():
window.clear()
batch.draw()
pyglet.app.run()
I had the same problem and I made it work like this:
You will need a wrapper class which will have the on_resize event handler. Also it needs the parent window.
class Label(pyglet.text.Label):
def __init__(self, window,**kwargs):
self.window = window
self.window.push_handlers(self)
self.init_settings = dict()
for k,v in kwargs.items():
if callable(v):
kwargs[k] = v(self.window)
self.init_settings[k] = v
super().__init__(**kwargs)
def on_resize(self, width, height):
for k,v in self.init_settings.items():
setattr(self,k,v(self.window))
Usage:
Each property you need to update on resizing the window will be a lambda function. Here you should write the initial values of the label with the initial window sizes.
class WindowSubclass(pyglet.window.Window):
def __init__(self,*args,**kwargs):
self.initial_size = self.width,self.height
self.label = Label(window=self,
text='PAUSED',
multiline=False,
font_name='Times New Roman',
bold=True,
font_size=lambda win:24*win.height/win.initial_height,
width=lambda win:win.width/4,
x=lambda win:win.width/2,
y=lambda win:win.height/2,
color=(255, 255, 255, 255),
anchor_x='center', anchor_y='center')

iTextSharp watermark multiline text

I have seen answers to question of how to add text watermark to an existing PDF document using iTextSharp. My question is how can we do multiline text. Is there a way to do this without having multiple PdfContentByte defined. I have tried to insert a newline character with no luck.
Here is the code from the internet. I just added
pdfData.ShowTextAligned(Element.ALIGN_CENTER, editDate, (pageRectangle.Width / 2) + 100, (pageRectangle.Height / 2) - 100, 45);
as the second line to get the second line of the watermark, it works but uses same parameters (color, size, etc.) as the first.
iTextSharp.text.Rectangle pageRectangle = PDFreader.GetPageSizeWithRotation(1);
//pdfcontentbyte object contains graphics and text content of page returned by pdfstamper
PdfContentByte pdfData = stamper.GetOverContent(1);
//create fontsize for watermark
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 120);
//create new graphics state and assign opacity
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.2F;
//set graphics state to pdfcontentbyte
pdfData.SetGState(graphicsState);
//set color of watermark
pdfData.SetColorFill(iTextSharp.text.Color.BLUE);
//indicates start of writing of text
pdfData.BeginText();
//show text as per position and rotation
pdfData.ShowTextAligned(Element.ALIGN_CENTER, "E D I T E D" , (pageRectangle.Width / 2), (pageRectangle.Height / 2), 45);
pdfData.ShowTextAligned(Element.ALIGN_CENTER, editDate, (pageRectangle.Width / 2) + 100, (pageRectangle.Height / 2) - 100, 45);
//call endText to invalid font set
pdfData.EndText();