How to add text annotation to existing PDF using pdf-annotate Python - python-3.x

I'm trying to use this Python library called pdf-annotate to add text annotation to existing PDF files. Here's the documentation of the library: https://github.com/plangrid/pdf-annotate
I got this sample code that add a square on the bottom left corner of the pdf file but couldn't figure out how to add free text instead of square. For example, I want to add a comment like "This document has been reviewed." instead of the square. This should be annotation on the existing pdf page instead of adding a new page to the pdf.
from pdf_annotate import PdfAnnotator, Appearance, Location
annotator = PdfAnnotator('sample.pdf')
annotator.add_annotation(
'square',
Location(x1=50, y1=50, x2=100, y2=100, page=0),
Appearance(fill=(1, 0, 0))
)
annotator.write('annotated.pdf')
I've been trying to review the source code in the documentation to figure out how to add text instead of square but couldn't figure it out. Does anyone know how to use this library to add text?

In my case the following worked:
annotator.add_annotation(
'text',
Location(x1=x_start, y1=y_start, x2=x_end, y2=y_end, page=0),
Appearance(content='Review...', font_size=10, fill=(0,0,0)),
)
But especially the "fill" argument is required. It gives the color of the text and must be specefied.

Related

How to preserve formatting when using getText() method in Google Docs using App Script / Javascript

The code below pulls in text from a file. I want to pull all the text from this file, and the text from this source file is formatted in bullet points.
var autoPay = DocumentApp.openById("[file ID]");
autoPay_text = autoPay.getBody().getText();
body.replaceText("{{AddedFeatures}}",autoPay_text);
How do I preserve the bullet points format when the text is placed in the destination file? Is there a method for this, or do I have to do something dreadful like creating an array?

Add SVG image to PdfSignatureAppearance with iText7

I want to add a SVG image to PdfSignatureAppearance. The method setSignatureGraphic has an ImageData parameter now in iText7. I couldn't find a way to create an imageData from SVG because ImageDataFactory is not supporting this format.
Can you please guide me on how to do that?
Note that with iText5 I was able to add svg after converting it to PDF and import it to a PDFTemplate then create an image after instantiate the PDFTemplate. setSignatureGraphic was accepting com.itextpdf.text.Image as parameter
Your question could be split into 2 more precise and simple ones:
How to process an SVG with iText?
How to create an ImageData instance out of the result of point 1?
As for question 1: one can use SvgConverter class (part of iTextCore's svg module). Unfortunately there are only PDF-related methods there: an SVG could be converted either to Image (class of layout module), or to PdfFormXObject (again PDF-related) or to a PDF file.
// to PDF
SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), signer.getDocument()); // the mentioned `signer` is the instance of PdfSigner which you use to sign the document
// to Image
SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), new File(destinationFolder + "svgAsPdf.pdf"));
As for question 2, there are several answers:
a) Suppose that you want to use this Image as the PdfSignatureAppearance's graphics data. For now the class doesn't provide a convenient setter, however, you could use some low level methods - either getLayer0 or getLayer2 to get the signature's background or foreground. They are represented by PDfFormXObject, hence you can use Canvas to add your image to them:
Image svg = SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), signer.getDocument());
Canvas canvas = new Canvas(appearance.getLayer0(), signer.getDocument());
canvas.add(svg);
canvas.close();
b) Suppose that your goal is to use the rendered bitmap as the PdfSignatureAppearance's graphics data. Then there is a specific iText product - pdfRender - which converts PDF files to images. The following code could be applied:
PdfToImageRenderer.renderPdf(new File(destinationFolder + "svgAsPdf.pdf"), new File(folderForTheResultantImage));
Now you can create an ImageData instance out of the resultant image file (by default a PDF is converted to a series of images with the format "pdfnamePAGE_NUMBER.jpg", but one could customize either the name or the output image format). In your case the PDF consist of just one page (which represents the converted SVG) and its name is "image1.jpg". The rest is obvious:
appearance.setSignatureGraphic(ImageDataFactory.create(destinationFolder + "image1.jpg"));

Getting rid of print "<IPython.core.display.Markdown object>" when using `display`

I'm trying to create nice slides using jupyter notebook and RISE. One of my objectives is to display a pandas-dataframe in a Markdown cell in order to have some styling flexibility.
I am using the following code to display my dataframe in a Markdown cell:
{{Markdown(display(df_x))}}
After running this line, I get the following result:
image of dataframe displayed
I would like to get rid of the text printed below my dataframe (<IPython.core.display.Markdown object>).
I still haven't found a way to achieve this. Could someone give me a hand?
This is the library I'm working with:
from IPython.display import display
Not familiar with Markdown class so not sure why you need that but this text printed in the output cell is coming from the fact that this Markdown class is returning and object and since you're not assigning it to any variable the default behavior for the notebook is to run something like str(your_object) which correctly returns <IPython.core.display.Markdown object>.
So the easiest workaround would be to just assign it to some variable like this:
dummy_var = Markdown(display(df_x))
# or better yet:
_ = Markdown(display(df_x))

Python3, Gtk3, Glade, set label color from code

I have a Glade file with a label with a gray default color,
which I want to override with green from code.
Therefore I added a style class in Glade:
and added this to my Python script:
However, the color remains gray.
What did I do wrong?
I made it a bit differently.
in a fileforcolors.css
I included
#resetstddata_button {
background: #00BFFF;
}
where the "resetstddata_button" is the name of the widget in the glade file (open the glade editor, click on the GtkButton you want to change the color, page "common", you see the parameter entry "Widget name", put the "resetstddata_button" in it ).
The python3 code extract for this button color change is:
style_provider = gtk.CssProvider()
css = open('/home/fileforcolors.css',
'rb') # rb needed for python 3 support
css_data = css.read()
css.close()
style_provider.load_from_data(css_data)
gtk.StyleContext.add_provider_for_screen(gdk.Screen.get_default(), style_provider,
gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
Hopefully it helps.
The problem was, that Glade initially preceded the style class name with a dot in its generated XML file (see screenshot in the OP).
You need to edit the Glade XML file and remove it:
After that, you can repeatedly change the style class name in the Glade GUI, it does not get a dot again.

How do i display the data contents of my xml file using pyqt4?

I am trying to build a tiny app to read from an xml file and display on a widget. I don't know which widget to use exactly; QTextBrowser, QTextedit and QWebView. I can't seem to find a good explanation. Please help as much as you can. Before i get, I'm so new to Python, PyQt and my programming ain't good at all.
I suggest you first interprete the xml content into a dom object, and then show whatever you want from that object into your widget. For the first part (detailed info here):
from xml.dom import minidom
dom = minidom.parse('my_xml.xml')
print(dom.toxml()) # .toxml() creates a string from the dom object
def print_some_info(node):
print('node representation: {0}'.format(node))
print('.nodeName: ' + node.nodeName)
print('.nodeValue: {0}'.format(node.nodeValue))
for child in node.childNodes:
print_some_info(child)
print_some_info(child)
(using e.g. an xml example in file 'my_xml.xml' from here)

Resources