How to insert multiple drawn line with itext7 - itext7

drawing a line a positioning int
I have a document where I want the following
From b = to x
Text1-->Image -->line--> Text2
Where line is between image1 and before Text2
Next b
It would look like "SomeText1" image Line "-------" "SomeText2"
Where ------ is a solid line, and every line has these 4 elements all on the same Y
I'm using
Y = Doc.GetRenderer().GetCurrentArea().GetBBox().GetTop()
To get and hold the Y position as I build up the b position as I drop in the text and image.
Using
new line = new SolidLine(1f)
dim ls = new LineSeparator(line)
It's the placing of the line I'm having trouble with.
setMarginTop doesn't work for obvious reasons and SetFixedPosition doesn't seem to work
To summarize: The text and image are fine and placed at the correct Y spot, but I'm unable to place the line on the same Y part of the page as the text and image. I've looked at using Canvas, but the samples seem to suggest I need to add a new page.
Any help much appreciated

Related

Moviepy - Place textclips at relative position

How do I place a textclip, right below the other? I require to place an image at the left of the video and two lines of text to the right of image, that are placed one below the other.
Something like this:
I tried to use set_position, but doesn't scale well for different videos (of different resolutions). By adjusting the arguments of set_position, I am able to place the textclips one below the other without gap in one resolution, but when I go to a higher resolution video, it shows a gap (I understand why the gap comes, but not sure how to prevent it)
txt_clip1 = TextClip("This is line 1 of text", fontsize = 12, color = 'white', bg_color='black')
txt_clip1 = txt_clip1.set_duration(7).set_start(0).set_end(7)
txt_clip1 = txt_clip1.set_position((0.1,0.90), relative=True).set_opacity(0.6)
txt_clip2 = TextClip("This is line 2 of the text, smaller font", fontsize = 8, color = 'white', bg_color='black')
txt_clip2 = txt_clip2.set_duration(7).set_start(0).set_end(7)
txt_clip2 = txt_clip2.set_position((0.1,0.93), relative=True).set_opacity(0.6)
I tried to insert a new line character in the text, but that doesn't suit me because the second line of text has different font properties.
Hopefully this will help you
txt_clip1 = TextClip(
"Cool effect 2nd line", color="black", bg_color="red", font="Amiri-Bold", kerning=5, fontsize=20
)
cvc = CompositeVideoClip([txt_clip1.set_position("East")], size=screensize)
txt_clip1 = txt_clip1.set_position((5,35))
.set_position mother take to arguments 1st is the position from the left screen and second is the position from the top
so your code is like something
txt_clip1 = txt_clip1.set_position((5,500))
txt_clip2 = txt_clip2.set_position((5,535))
It will place the text in the position you want.
Hopefully, this will help.
Happy coding

Find cursor's coordinates in NSTextView

I have wrapping NSTextView instances stacked vertically, for example:
The quick brown fox
jumps over the lazy dog
Jackdaws love my big
sphinx of quartz
I need to move between them with up/down arrows. For example, when the cursor is positioned after the l in "lazy" and the user presses the down arrow, the cursor should jump right after the y in "my" – like it would do if these sentences were in the same text view.
By default, when the down arrow is pressed while the cursor is at the last wrapped line, a text view moves it to the end of that line. While I can use textView(_:doCommandBy:) in NSTextViewDelegate to detect the "arrow down" selector and override the default behavior, there are two problems:
I can determine if the cursor is at the last line by getting its position via the selectedRanges property and then checking for the newline character after this position, but it is not possible to know if it is at the last wrapped line, i.e. near the border of the current text view.
I need to know the X coordinate of the cursor to place it at approximately the same X coordinate in another text view (the fonts won't necessarily be fixed-width, so I can't rely on the character count).
I suppose both of them can be resolved via NSLayoutManager, but I can't wrap my head around all of its available methods.
It turned out to be relatively easy, here's what I've done (the examples are in C#). First, boundingRect(forGlyphRange:in:) gets the cursor's location in the current view:
var cursorLocation = new NSRange(CurrentTextView.SelectedRange.Location, 0);
var cursorCoordinates = CurrentTextView.LayoutManager.BoundingRectForGlyphRange(cursorLocation, CurrentTextView.TextContainer).Location;
Then if the second text view is below, the insertion point will be at 0 on the Y axis:
var insertionPoint = new CGPoint(cursorCoordinates.X, 0);
And if it is above, then another view's height should be used (reduced by 1, otherwise the resulting character index will be incorrect and the cursor will be placed at the end of the line):
var insertionPoint = new CGPoint(cursorCoordinates.X, AnotherTextView.Bounds.Size.Height - 1);
After getting the insertion point, another view needs to become the first responder and then characterIndexForInsertion(at:) does the job:
Window.MakeFirstResponder(AnotherTextView);
var index = AnotherTextView.CharacterIndex(insertionPoint);
AnotherTextView.SelectedRange = new NSRange(index, 0);

PIL: Imageobject.save() after drawing completely corrupts images and smurfs the ouput

I have these two functions in my program:
def depict_ph_increase(x,y,color, imobject):
program_print(color)
draw = PIL.ImageDraw.Draw(imobject)
draw.text((x, y),color,(255,255,255))
imobject.save('tmp-out.gif')
im_temp = PIL.Image.open("tmp-out.gif")#.convert2byte()
im_temp = im_temp.resize((930, 340), PIL.Image.ANTIALIAS)
MAP_temp = ImageTk.PhotoImage(im_temp)
map_display_temp = Label(main, image=MAP_temp)
map_display_temp.image = MAP_temp # keep a reference!
map_display_temp.grid(row=4,column=2, columnspan=3)
def read_temp_pixels(temperature_file, rngup, rngdown):
temp_image_object = PIL.Image.open(temperature_file)
(length, width) = get_image_size(temp_image_object)
(rngxleft, rngxright) = rngup
(rngyup,rngydown) = rngdown
print 'the length and width is'
print length, width
hotspots = 5;
for hotspot in range(0,hotspots):
color = "#ffffff"
while color == "#ffffff" or color == "#000000" or color == "#505050" or color == "#969696":
yc = random.randint(rngxleft, rngxright)
xc = random.randint(rngyup,rngydown)
color = convert_RGB_HEX(get_pixel_color(temp_image_object, xc, yc))
depict_ph_increase(xc,yc,color, temp_image_object)
The bottom one calls the top one. Their job is to read in this image:
It then randomly selects a few pixels, grabs their colors, and writes the hex values of the colors on top. But, when it redisplays the image, it gives me this garbage:
Those white numbers up near the upper right corner are the hex values its drawing. Its somehow reading the values from the corrupted image, despite the fact that I don't collect the values until AFTER I actually call the ImageDraw() method. Can someone explain to me why it is corrupting the image?
Some background--the get_pixel_color() function is used several other times in the program and is highly accurate, its just reading the pixel data from the newly corrupted image somehow. Furthermore, I do similar image reading (but not writing) at other points in my code.
If there is anything I can clarify, or any other part of my code you want to see, please let me know. You can also view the program in its entirety at my github here: https://github.com/jrfarah/coral/blob/master/src/realtime.py It should be commit #29.
Other SO questions I have examined, to no avail: Corrupted image is being saved with PIL
Any help would be greatly appreciated!
I fixed the problem by editing this line:
temp_image_object = PIL.Image.open(temperature_file)
to be
temp_image_object = PIL.Image.open(temperature_file).convert('RGB')

Print image to pdf without margin using Matlab

I'm trying to use the answers I found in these questions:
How to save a plot into a PDF file without a large margin around
Get rid of the white space around matlab figure's pdf output
External source
to print a matlab plot to pdf without having the white margins included.
However using this code:
function saveTightFigure( h, outfilename, orientation )
% SAVETIGHTFIGURE(H,OUTFILENAME) Saves figure H in file OUTFILENAME without
% the white space around it.
%
% by ``a grad student"
% http://tipstrickshowtos.blogspot.com/2010/08/how-to-get-rid-of-white-margin-in.html
% get the current axes
ax = get(h, 'CurrentAxes');
% make it tight
ti = get(ax,'TightInset');
set(ax,'Position',[ti(1) ti(2) 1-ti(3)-ti(1) 1-ti(4)-ti(2)]);
% adjust the papersize
set(ax,'units','centimeters');
pos = get(ax,'Position');
ti = get(ax,'TightInset');
set(h, 'PaperUnits','centimeters');
set(h, 'PaperSize', [pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
set(h, 'PaperPositionMode', 'manual');
set(h, 'PaperPosition',[0 0 pos(3)+ti(1)+ti(3) pos(4)+ti(2)+ti(4)]);
% save it
%saveas(h,outfilename);
if( orientation == 1)
orient portrait
else
orient landscape
end
print( '-dpdf', outfilename );
end
Results in this output:
As you can see the 'PaperSize' seems to be set not properly. Any idea of possible fixes?
NOTE
If I change the orientation between landscape and portrait the result is the same, simply the image is chopped in a different way.
However if I save the image with the saveas(h,outfilename); instruction the correct output is produced.
Why is this? And what is the difference between the two saving instructions?
Alltogether the answers you mentioned offer a lot of approaches, but most of them didn't worked for me neither. Most of them screw up your papersize when you want to get the tight inset, the only which worked for me was:
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
I finally wrote a function, where I specify the exact height and width of the output figure on paper, and the margin I want (or just set it to zero). Be aware that you also need to pass the axis handle. Maybe this functions works for you also.
function saveFigure( fig_handle, axes_handle, name , height , width , margin)
set(axes_handle,'LooseInset',get(axes_handle,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')
% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
'PaperPosition',[0 0 width+margin height+margin],...
'PaperSize',[width+margin height+margin],...
'PaperPositionMode','auto',...
'InvertHardcopy', 'on',...
'Renderer','painters'... %recommended if there are no alphamaps
);
saveas(fig_handle,name,'pdf')
end
Edit: if you use painters as renderer saveas and print should produce similar results. For jpegs print is preferable as you can specify the resolution.

How can I add an image to a PDF at specific x-y coordinates using IText?

I have existing PDFs to which I need to dynamically add an image/images. The image comes from a file upload. Once I have the file uploaded, how can specify where to place the image on the PDF. One code snippet I found does not work correctly. This needs to work for PDFs with any number of pages. From what I understand, absolute positioning is set from the bottom-left corner of the last page of the PDF. If I need an image to be displayed 30 pixels from the top and 50 pixels from the left of page 1, how can I accomplish this? Or, if I need to display an image 50px from the top/100 px from the left on page 2?
I've tried using the code found at http://rip747.wordpress.com/2009/03/26/add-an-image-dynamically-to-a-pdf-with-cf-and-itext/. I've modified it for my needs below:
<cfscript>
myLeft = 30;
myTop = 50;
myPageNum = 1;
// output buffer to write PDF
fileIO = createObject("java","java.io.FileOutputStream").init(myOutputPath);
// reader to read our PDF
reader = createObject("java","com.lowagie.text.pdf.PdfReader").init(mySourcePath);
// stamper so we can modify our existing PDF
stamper = createObject("java","com.lowagie.text.pdf.PdfStamper").init(reader, fileIO);
// get the content of our existing PDF
content = stamper.getOverContent(reader.getNumberOfPages());
// create an image object so we can add our dynamic image to our PDF
image = createobject("java", "com.lowagie.text.Image");
// initalize our image
img = image.getInstance(imgPath);
x = (reader.getPageSize(1).width() - img.scaledWidth()) - myLeft;
y = (reader.getPageSize(1).height() - img.scaledHeight()) - myTop;
// now we assign the position to our image
img.setAbsolutePosition(javacast("float", x), javacast("float", y));
// add our image to the existing PDF
content.addImage(img);
// flattern our form so our values show
stamper.setFormFlattening(true);
// close the stamper and output our new PDF
stamper.close();
// close the reader
reader.close();
</cfscript>
The above code places my image at the top-right corner of page 2 - 50px form the top/30px from the left.
I know I'm close...just need a little help getting this nailed down for my needs.
I've updated my code. This gets the image to the top left corner of page 2 - correct positioning, but I want it on page 1:
x = myLeft;
y = (reader.getPageSize(1).height()) - img.scaledHeight() - myTop;
I thought maybe I needed to add the height of page 1 to get the image up to page 1, but the image completely disappears when I try either of the options below:
// I figure I'll need something like this to handle multi-page docs
y = (reader.getPageSize(1).height() * reader.getNumberOfPages()) - img.scaledHeight() - myTop;
y = reader.getPageSize(1).height() + reader.getPageSize(1).height() - img.scaledHeight() - myTop;
You're getting your "OverContent" from stamper.getOverContent(reader.getNumberOfPages());. The parameter for getOverContent() is the page number. So your code is getting a PdfContentByte for the last page, not the first.
I found my answer:
The page number has to be set in com.lowagie.text.pdf.PdfStamper.getOverContent():
content = stamper.getOverContent(myPageNum);
Knew it was easy.
are you using CF8+? You can use
<cfpdf action="addWatermark" source="myPDF.pdf" image="myImage.jpg"
position="0,0" rotation="0" showOnPrint="true" opacity="10">

Resources