PDFBox Overlapped Text - android

I have Implemented an algorithm to draw text on pages using PDFBox library for Android. The problem is whenever I add a new page the text is overlapped like shown on the image below. I am sure i am using the PDPageContentStream.newLine() method but the result is not as expected.
Am I missing something else ?
Here is my code snippet
PDPage page1 = new PDPage();
getInstance().getAnnexe().addPage(page1);
PDPageContentStream contentStream1 = new
PDPageContentStream(getInstance().getAnnexe(), page1, true, true);
contentStream1.beginText();
contentStream1.newLineAtOffset(100F, 650F);
contentStream1.setFont(font, fontSize);
printMultipleLines(subSet, contentStream1);
contentStream1.endText();
contentStream1.close();
And this is the printMultipleLines() Method
private void printMultipleLines(ArrayList<String> lines, PDPageContentStream contentStream) {
try {
for (String line :
lines) {
if (line.length() > 110) {
// Print line as 2 lines
contentStream.showText(line.substring(0, 90));
contentStream.newLine();
contentStream.showText(line.substring(90, line.length()));
} else
// Print line as a whole
contentStream.showText(line);
// Print Carriage Return
contentStream.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}

Thank to #TilmanHausherr the problem was with the TL operator. Each newly created page had TL equal to zero amount of user default units. I just had to set the Text Leading offset.
Here is the updated code :
PDPage page1 = new PDPage();
getInstance().getAnnexe().addPage(page1);
PDPageContentStream contentStream1 = new
PDPageContentStream(getInstance().getAnnexe(), page1, true, true);
// Set the Text Leading (TL operator) here!!!!
contentStream1.setLeading(12);
contentStream1.beginText();
contentStream1.newLineAtOffset(100F, 650F);
contentStream1.setFont(font, fontSize);
printMultipleLines(subSet, contentStream1);
contentStream1.endText();
contentStream1.close();
All thanks and credits goes to # TilmanHausherr for his fast and accurate answer.

Related

Xamarin.Forms and Android - Getting the result of a toggle button from a different view

I have been following the custom pin tutorial using Xamarin.Forms which I have linked below. I have finished implementing it and I have also moved onto adding pins to the map as well through tapping.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map/customized-pin
(side note: I am working almost exclusively with Xamarin.Forms and Android)
Currently, I am able to tap on the map and a new custom pin will be added at that location, which is great. What is not great is that I was unable to figure out how to get a tap and long hold gesture to work instead of just the normal tap. To try to combat this, and because I will eventually have to add these anyways, I am planning on adding a button that the user can press to initiate that they want to add a button to the map, and I will later add an undo button, and many others, etc.
The problem is, I have no idea how to get the result of what my toggle button state is from the custom render that I am using for the map. Where can I get the result of my toggle button and use it as a boolean towards whether to add a button or not?
Here is the toggle button code, which I took line by line from their example on this page:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/button
Here is the code where I add a custom pin just by a single tap:
private void GoogleMap_MapClick(object sender, GoogleMap.MapClickEventArgs e)
{
((CustomMap)Element).OnTap(new Position(e.Point.Latitude, e.Point.Longitude));
var addingPin = new CustomPin
{
Type = PinType.Place,
Position = new Position(e.Point.Latitude, e.Point.Longitude),
Address = " - need to possibly implement - ",
Id = "shelter",
Label = "Pin from tap",
Url = "http://www.redcross.org"
};
Map.Pins.Add(addingPin);
this.customPins.Add(addingPin);
}
I thought about making a custom button render but by my knowledge I can only apply one Android render to a content page at a time, and when I tried to add a custom button render to the map render then this method was not happy, as it was taking in some sort of Android Map View and not a button view:
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
NativeMap.InfoWindowClick -= OnInfoWindowClick;
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
customPins = formsMap.CustomPins;
Control.GetMapAsync(this);
}
}
Any help is greatly appreciated. Below I have included a pic of what my application looks like so far, along with the custom page that I am using as well.
using Hermes.Models;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace Hermes
{
public class MapPage : ContentPage
{
public MapPage()
{
CustomMap customMap = new CustomMap()
{
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand,
MapType = MapType.Street,
};
var examplePinSupplies = new CustomPin
{
Type = PinType.Place,
Position = new Position(42.02525, -93.65087),
Address = " - need to possibly implement - ",
Id = "supplies",
Label = "supplies",
Url = "https://www.redcross.org/store"
};
var examplePinMedical = new CustomPin
{
Type = PinType.Place,
Position = new Position(42.02290, -93.63912),
Address = " - need to possibly implement - ",
Id = "medical",
Label = "medical",
Url = "http://www.redcross.org"
};
var examplePinShelter = new CustomPin
{
Type = PinType.Place,
Position = new Position(42.02045, -93.60968),
Address = " - need to possibly implement - ",
Id = "shelter",
Label = "shelter",
Url = "http://www.redcross.org"
};
customMap.CustomPins = new List<CustomPin> { examplePinSupplies, examplePinMedical, examplePinShelter };
customMap.Pins.Add(examplePinSupplies);
customMap.Pins.Add(examplePinMedical);
customMap.Pins.Add(examplePinShelter);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.025250, -93.650870), Distance.FromMiles(1.0)));
var addPin = new ToggleButton { Text = "Add pin" };
var buttons = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children = {
addPin
}
};
Content = new StackLayout
{
Spacing = 0,
Children = {
customMap,
buttons
}
};
}
}
}
What I am trying to do: I am trying to tap the 'add pin' toggle button, and then the map will allow me to only add one pin on the map by tapping, and then any other consecutive taps that are not on another pin will not add another pin to the map.

Getting exception upon clicking Ionic based icon "Exception in thread "main" org.openqa.selenium.WebDriverException" [duplicate]

I used explicit waits and I have the warning:
org.openqa.selenium.WebDriverException:
Element is not clickable at point (36, 72). Other element would receive
the click: ...
Command duration or timeout: 393 milliseconds
If I use Thread.sleep(2000) I don't receive any warnings.
#Test(dataProvider = "menuData")
public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.findElement(By.id("navigationPageButton")).click();
try {
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
} catch (Exception e) {
System.out.println("Oh");
}
driver.findElement(By.cssSelector(btnMenu)).click();
Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
}
WebDriverException: Element is not clickable at point (x, y)
This is a typical org.openqa.selenium.WebDriverException which extends java.lang.RuntimeException.
The fields of this exception are :
BASE_SUPPORT_URL : protected static final java.lang.String BASE_SUPPORT_URL
DRIVER_INFO : public static final java.lang.String DRIVER_INFO
SESSION_ID : public static final java.lang.String SESSION_ID
About your individual usecase, the error tells it all :
WebDriverException: Element is not clickable at point (x, y). Other element would receive the click
It is clear from your code block that you have defined the wait as WebDriverWait wait = new WebDriverWait(driver, 10); but you are calling the click() method on the element before the ExplicitWait comes into play as in until(ExpectedConditions.elementToBeClickable).
Solution
The error Element is not clickable at point (x, y) can arise from different factors. You can address them by either of the following procedures:
1. Element not getting clicked due to JavaScript or AJAX calls present
Try to use Actions Class:
WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
2. Element not getting clicked as it is not within Viewport
Try to use JavascriptExecutor to bring the element within the Viewport:
WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);
3. The page is getting refreshed before the element gets clickable.
In this case induce ExplicitWait i.e WebDriverWait as mentioned in point 4.
4. Element is present in the DOM but not clickable.
In this case induce ExplicitWait with ExpectedConditions set to elementToBeClickable for the element to be clickable:
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));
5. Element is present but having temporary Overlay.
In this case, induce ExplicitWait with ExpectedConditions set to invisibilityOfElementLocated for the Overlay to be invisible.
WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
6. Element is present but having permanent Overlay.
Use JavascriptExecutor to send the click directly on the element.
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
In case you need to use it with Javascript
We can use arguments[0].click() to simulate click operation.
var element = element(by.linkText('webdriverjs'));
browser.executeScript("arguments[0].click()",element);
I ran into this error while trying to click some element (or its overlay, I didn't care), and the other answers didn't work for me. I fixed it by using the elementFromPoint DOM API to find the element that Selenium wanted me to click on instead:
element_i_care_about = something()
loc = element_i_care_about.location
element_to_click = driver.execute_script(
"return document.elementFromPoint(arguments[0], arguments[1]);",
loc['x'],
loc['y'])
element_to_click.click()
I've also had situations where an element was moving, for example because an element above it on the page was doing an animated expand or collapse. In that case, this Expected Condition class helped. You give it the elements that are animated, not the ones you want to click. This version only works for jQuery animations.
class elements_not_to_be_animated(object):
def __init__(self, locator):
self.locator = locator
def __call__(self, driver):
try:
elements = EC._find_elements(driver, self.locator)
# :animated is an artificial jQuery selector for things that are
# currently animated by jQuery.
return driver.execute_script(
'return !jQuery(arguments[0]).filter(":animated").length;',
elements)
except StaleElementReferenceException:
return False
You can try
WebElement navigationPageButton = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("navigationPageButton")));
navigationPageButton.click();
Scrolling the page to the near by point mentioned in the exception did the trick for me. Below is code snippet:
$wd_host = 'http://localhost:4444/wd/hub';
$capabilities =
[
\WebDriverCapabilityType::BROWSER_NAME => 'chrome',
\WebDriverCapabilityType::PROXY => [
'proxyType' => 'manual',
'httpProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
'sslProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
'noProxy' => PROXY_EXCEPTION // to run locally
],
];
$webDriver = \RemoteWebDriver::create($wd_host, $capabilities, 250000, 250000);
...........
...........
// Wait for 3 seconds
$webDriver->wait(3);
// Scrolls the page vertically by 70 pixels
$webDriver->executeScript("window.scrollTo(0, 70);");
NOTE: I use Facebook php webdriver
If element is not clickable and overlay issue is ocuring we use arguments[0].click().
WebElement ele = driver.findElement(By.xpath("//div[#class='input-group-btn']/input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
The best solution is to override the click functionality:
public void _click(WebElement element){
boolean flag = false;
while(true) {
try{
element.click();
flag=true;
}
catch (Exception e){
flag = false;
}
if(flag)
{
try{
element.click();
}
catch (Exception e){
System.out.printf("Element: " +element+ " has beed clicked, Selenium exception triggered: " + e.getMessage());
}
break;
}
}
}
In C#, I had problem with checking RadioButton,
and this worked for me:
driver.ExecuteJavaScript("arguments[0].checked=true", radio);
Can try with below code
WebDriverWait wait = new WebDriverWait(driver, 30);
Pass other element would receive the click:<a class="navbar-brand" href="#"></a>
boolean invisiable = wait.until(ExpectedConditions
.invisibilityOfElementLocated(By.xpath("//div[#class='navbar-brand']")));
Pass clickable button id as shown below
if (invisiable) {
WebElement ele = driver.findElement(By.xpath("//div[#id='button']");
ele.click();
}

Multiple page print using iText pdf in landscape orientation

I have tried itextPdf_page_orientation but the problem is - only first page is proper rest all pages are printed in half area.
Here is an image which will help you understand the issue.
I have tried both setting the page size PdfPage.A4.rotate() and setting the event to PdfWritter.
Here is the code snipet.
#Override
protected String doInBackground(Void... params) {
final String PARENT_PATH =
Environment.getExternalStorageDirectory().getPath() + "/.GSTInvoice";
Document document = null;
try {
File file = new File(PARENT_PATH);
if (!file.exists()) {
file.mkdirs();
}
File pdfFile = new File(file, "last_sales_summary");
document = new Document();
document.setPageSize(PageSize.A4.rotate());
event = new RotateEvent();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
writer.setPageEvent(event);
document.open();
event.setOrientation(PdfPage.LANDSCAPE);
taxList = new ArrayList<>();
PdfContentByte cb = writer.getDirectContent();
printPage(document, writer, cb);
if (document != null && document.isOpen()) {
document.close();
}
return pdfFile.getPath();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null && document.isOpen()) {
document.close();
}
}
return null;
}
private void printPage(Document document, PdfWriter pdfWriter, PdfContentByte pdfContentByte) throws Exception{
int noOfPages = getNoOfPages();
BaseFont latoLight = BaseFont.createFont("assets/Lato-Light.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font light = new Font(latoLight, 8);
for (int i=1;i<=noOfPages;i++) {
if (i != 1) {
document.newPage();
pdfWriter.setPageSize(PageSize.A4.rotate());
event.setOrientation(PdfPage.LANDSCAPE);
document.setPageSize(PageSize.A4.rotate());
}
addTopPart(document);
addMiddleTable(document, i);
if (noOfPages>1) {
Paragraph paragraph = new Paragraph(new Phrase("Page " + i + "/" + noOfPages, light));
paragraph.setAlignment(Element.ALIGN_CENTER);
paragraph.setSpacingBefore(8f);
ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_CENTER,
paragraph,
(document.right() - document.left()) / 2 + document.leftMargin(),
document.bottom() - 10, 0);
}
event.setOrientation(PdfPage.LANDSCAPE);
}
}
Any input will be appreciated
The only peculiarity of the PDF is that the first page has a page rotation of 0 while the second one has a page rotation of 90.
This is due to what I hinted at in my first comment: By doing event.setOrientation(PdfPage.LANDSCAPE) after document.open() the first page is not rotated by the event listener, only all following ones. Other than that all pages have a mediabox of a portrait A4 page with rotated, page-filling content.
As you indicate that changing the order of event.setOrientation and document.open does not change the behavior, the print manager in question seems to be buggy.
You might try to remove all event.setOrientation(PdfPage.LANDSCAPE) calls; this should result in all pages to become like the first one.
Or you might want to remove all that code setting and changing rotation and instead instantiate the Document document using new Document(new RectangleReadOnly(842,595)), i.e. without any rotation at all; as the print manager appears not to handle rotation properly, this might also result in a desired behavior.
As the OP reported in a comment,
your suggestion to use RectangleReadOnly(842,595) worked.

ListView reusing old images

I created a plugin using Picasso and it uses the android.widget.ImageView to load the cached image into.
The plugin works fine if using a Repeater but whenever i try using it with a ListView after scrolling past about the 7th item the ListView begins to reuse old images even if the image source is different
The reason why is because list views reuse the entire fragment; so what happens is that your img being reused gets the old image shown unless you clear it.
I actually use Picasso myself; and this is my current picasso library.
So if you look in my code below, when I set the new .url, I clear the existing image. (I made a comment on the specific line) -- This way the image now show blank, and then picasso loads it from either memory, disk or a remote url (in my case a remote url) and it will assign the proper image.
"use strict";
var Img = require('ui/image').Image;
var application = require("application");
var PT = com.squareup.picasso.Target.extend("Target",{
_owner: null,
_url: null,
onBitmapLoaded: function(bitmap, from) {
// Since the actual image / target is cached; it is possible that the
// target will not match so we don't replace the image already seen
if (this._url !== this._owner._url) {
return;
}
this._owner.src = bitmap;
},
onBitmapFailed: function(ed) {
console.log("Failed File", this._url);
},
onPrepareLoad: function(ed) {
}
});
Object.defineProperty(Img.prototype, "url", {
get: function () {
return this._url;
},
set: function(src) {
if (src == null || src === "") {
this._url = "";
this.src = null;
return;
}
var dest = src;
this._url = dest;
this.src = null; // -- THIS IS THE LINE TO CLEAR THE IMAGE
try {
var target = new PT();
target._owner = this;
target._url = dest;
var x = com.squareup.picasso.Picasso.with(application.android.context).load(dest).into(target);
} catch (e) {
console.log("Exception",e);
}
},
enumerable: true,
configurable: true
});
Please note you only need to require this class once, then it attaches itself to the <Image> component and adds the new .url property; this allows me to use this in the Declarative XML in all the rest of the screens and when I need picasso, I just use the .url property to have picasso take over the loading of that image.

Make a TextView scroll its Content in a "bouncing" sort of fashion

Right now i am implementing a custom Renderer for my Xamarin.Forms Label with which i can make use of the marquee scrolling features of the TextView.
What i want to do now is change the scrolling mode of the text from just going to the left endlessly to "bouncing" from side to side. (Kind of like this question i found regarding this subject: How to make a label scroll a word back and forth?) I have not found any resources online that talk about this. I would be very happy if you could give me a general idea of where to look or what to do.
Thank you for your time
Here is my marquee Custom-Renderer:
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
MarqueeLabel marqLabel = (MarqueeLabel)this.Element;
base.OnElementChanged(e);
if (marqLabel.shouldScroll)
{
Control.SetSingleLine(true);
Control.SetHorizontallyScrolling(true);
Control.Selected = true;
Control.Ellipsize = Android.Text.TextUtils.TruncateAt.Marquee;
Control.SetMarqueeRepeatLimit(-1);
}
}
If you can do the animations on the Xamarin.Forms level then you can save yourself quite a bit of work rather than implementing on each platform-specific level.
Try the following example that demonstrates some bouncing of a Label to and from the edges of the screen, as it may give you some ideas? Note it doesn't do it continuously in the example below.
StackLayout objStackLayout = new StackLayout()
{
Orientation = StackOrientation.Vertical,
};
Label objLabel = new Label();
objLabel.Text="Hello";
objLabel.HorizontalOptions = LayoutOptions.Start;
objStackLayout.Children.Add(objLabel);
Button objButton1 = new Button()
{
Text = "Animate"
};
objButton1.Clicked += (async (o2, e2) =>
{
double dblWidth = objStackLayout.Width - objLabel.Width;
//
await objLabel.TranslateTo(dblWidth, 0,1000, Easing.BounceIn);
await objLabel.TranslateTo(0,0,1000, Easing.BounceIn);
});
objStackLayout.Children.Add(objButton1);
This is not the good way to do that but I am sure this will solve your problem.
StackLayout objStackLayout = new StackLayout()
{
Orientation = StackOrientation.Vertical,
};
Label lblMarquee = new Label();
lblMarquee.Text = "This is marquee animation!!!";
lblMarquee.HorizontalOptions = LayoutOptions.Start;
objStackLayout.Children.Add(lblMarquee);
Button btnAnimate = new Button()
{
Text = "Stare Marquee Animation"
};
btnAnimate.Clicked += (async (o2, e2) =>
{
double dblWidth = objStackLayout.Width + lblMarquee.Width;
// Infinite loop
while (true)
{
lblMarquee.IsVisible = false;
await lblMarquee.TranslateTo(dblWidth / 2, 0, 500, Easing.SinIn);
lblMarquee.IsVisible = true;
await lblMarquee.TranslateTo(-(dblWidth / 2), 0, 10000, Easing.SinIn);
}
});
objStackLayout.Children.Add(btnAnimate);
Content = objStackLayout;
}
NOTE: You can remove the infinite loop and try the same with TPL that will be good way to handle never ending task instead of using infinite loop.

Categories

Resources