But where do you change the font size of the method
HtmlConverter.convertToElements?
Why do I get this result with this code result code after execute ?
because the text between the tags does not change size ?
I also tried these solutions using the various methods (but the size does not change) :
https://stackoverflow.com/a/59044415/18323778
itext7 set font and size of HtmlConverter elements (In this question it is explained how to use the css too I am interested in changing only 2 properties of the element)
Run this code:
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.layout.properties.Property;
import com.itextpdf.layout.properties.UnitValue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
/**
*
* #author UC9001309
*/
public class TestConvertHtml2pdf {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
PdfWriter pdfWriter = new PdfWriter("C:\\Temp\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()) + ".pdf");
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
FontProvider provider = new DefaultFontProvider(true, false, false);
ConverterProperties cvProp = new ConverterProperties();
cvProp.setFontProvider(provider);
cvProp.setImmediateFlush(true);
List<IElement> lst = HtmlConverter.convertToElements("Testo <b><u><i>prova</i></u></b> con tag",cvProp);
lst.get(0).setProperty(Property.FONT_SIZE,UnitValue.createPointValue(Float.parseFloat("20")));
Paragraph p = (Paragraph) lst.get(0);
p.setProperty(Property.FONT_SIZE,UnitValue.createPointValue(Float.parseFloat("20")));
document.add(p);
document.close();
}
}
i tried various methods but still don't get results, does anyone know how to fix ?
Thanks for your help .
The goal is to create a method in Itext 7 to pass as parameters a string with html tag and font and font size, get it translated by the html parser to apply bold, italics, uderline without setting them with Itex commands as paragraph. selBold ec ..
Because my colleagues get tired of programming.
After several attempts I solved this (I create a main to try):
package testconverthtml2pdf;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.layout.properties.UnitValue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
/**
*
* #author UC9001309
*/
public class TestConvertHtml2pdf {
/**
* #param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// Future parameters of the method change it to test !!!
float vardim = 10.5f;
String tipofont =StandardFonts.COURIER.toString().toLowerCase();
String strInput = "Testo <b><u><i>prova</i></u></b> con tag";
// -------------------------------------------------------------
PdfWriter pdfWriter = new PdfWriter("C:\\Temp\\" + new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()) + ".pdf");
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
FontProvider provider = new DefaultFontProvider(true, false, false);
ConverterProperties cvProp = new ConverterProperties();
cvProp.setFontProvider(provider);
cvProp.setImmediateFlush(true);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<p style='font-family:"+tipofont +";font-size:"+UnitValue.createPointValue(vardim)+"'>") ;
stringBuilder.append(strInput) ;
stringBuilder.append("</p>") ;
System.out.println(stringBuilder);
List<IElement> lst = HtmlConverter.convertToElements(stringBuilder.toString(),cvProp);
Paragraph p = (Paragraph) lst.get(0);
document.add(p);
document.close();
}
}
To set Font and Font-Size to the input string I add a with a Style attribute inside for Font and Font-Size taken from parameter.
stringBuilder.append("<p style='font-family:"+tipofont +";font-size:"+UnitValue.createPointValue(vardim)+"'>") ;
tipofont -----> Font parameter
vardim -----> Font size parameter
I have to thank André Lemos who in his comment (lost because instead of modifying the question I deleted and recreated) gave me a solution because the bold was not created because the font type that had bold as its attribute was missing. I loaded Currier but not Currier-Bold.
If this solution can help anyone, it is available to the community.
Related
I just copied a program from website (I am sure it was written by Bruno) but does not work for me. I just have a very plain pdf fie and interested to insert buttons in it using this program exactly.
But it does not insert the button on the file. My original file is in "/WEB-INF/design100.pdf".
package admin_pack;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PushbuttonField;
import com.itextpdf.text.pdf.TextField;
import javax.servlet.annotation.WebServlet;
/**
*
* #author Mushtaq
*/
#WebServlet(name = "FormServlet", urlPatterns = {"/FormServlet"})
public class FormServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/pdf");
try {
// We get a resource from our web app
InputStream is
= getServletContext().getResourceAsStream("/WEB-INF/design100.pdf");
// We create a reader with the InputStream
PdfReader reader = new PdfReader(is, null);
// We create an OutputStream for the new PDF
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Now we create the PDF
PdfStamper stamper = new PdfStamper(reader, baos);
// We add a submit button to the existing form
PushbuttonField button = new PushbuttonField(
stamper.getWriter(), new Rectangle(90, 660, 140, 690), "submit");
button.setText("POST");
button.setBackgroundColor(new GrayColor(0.7f));
button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
PdfFormField submit = button.getField();
// submit.setAction(PdfAction.createSubmitForm(
// "/book/form", null, PdfAction.SUBMIT_HTML_FORMAT));
stamper.addAnnotation(submit, 1);
stamper.close();
reader.close();
// We write the PDF bytes to the OutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
} catch (DocumentException e) {
throw new IOException(e.getMessage());
}
}
}
I use Jung to draw a graph and the inputs are in .net format (Building it with txt2pajek). I want to see weights on edges and in output.
Please help me out how to do that?
Thanks
The code and .net file format are:
package pGraph;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.commons.collections15.FactoryUtils;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.MapTransformer;
import com.sun.xml.internal.ws.api.server.Container;
import pGraph.JungExample2.MyRenderer;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.io.PajekNetReader;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
/**
* A class that shows the minimal work necessary to load and visualize a graph.
*/
public class D1
{
public static void main(String[] args) throws IOException
{
JFrame jf = new JFrame();
PajekNetReader pnr = new PajekNetReader(FactoryUtils.instantiateFactory(Object.class));
Graph g = new DirectedSparseGraph();
VisualizationViewer vv = new VisualizationViewer(new FRLayout(g));
vv.getRenderContext().setVertexLabelTransformer(pnr.getVertexLabeller());
vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());
pnr.load("F:\\1c\\qq.net", g);
DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
vv.setGraphMouse(gm);
System.out.println(g.toString());
final DefaultModalGraphMouse<String, Number> graphMouse = new DefaultModalGraphMouse<String, Number>();
graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
vv.setGraphMouse(graphMouse);
final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
jf.getContentPane().add(vv) ;
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
}
*Vertices 5
1 "t"
2 "b"
3 "c"
4 "d"
5 "a"
*Arcs
1 2 0.2
3 4 0.9
5 4 0.86
PajekNetReader.getEdgeWeightTransformer() will give you the edge weights from the Pajek file.
You'll then need to provide a simple Transformer that can turn your Transformer<E, Number> into a Transformer<E, String>, and pass that to vv.getRenderContext().setEdgeLabelTransformer(). (Using toStringLabeller doesn't make much sense because the edge objects are not, themselves, the weights that you want.)
http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/io/PajekNetReader.html
I trying to use the dart.js call googlemap but receive "Breaking on exception: ReferenceError: ReceivePortSync is not defined". Anyone know about this exception?
import 'dart:html';
import 'dart:js';
import 'package:js/js.dart' as js;
import 'package:google_maps/google_maps.dart';
void main() {
bindMap();
}
void bindMap(){
js.scoped((){
final mapOptions = new MapOptions()
..zoom = 8
..center = new LatLng(-34.397, 150.644)
..mapTypeId = MapTypeId.ROADMAP
;
final map = new GMap(query("#map_canvas"), mapOptions);
});
}
You have to add <script src="packages/browser/interop.js"></script> in your html file and add browser package in dependencies.
On your code, you can :
avoid js.scoped that is not needed since few versions of js.
remove import 'dart:js'; that is not needed.
Finally you can have a look at the simple map example to have an up-to-date example.
I'm building a pure actionscript project and would like to create a pdf with an map chart of the world to display my data.
It's possible to create a map and save as image and then embed in pdf with alivepdf?
package
{
import MyButton;
import flash.display.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.external.*;
import flash.net.*;
import flash.net.FileReference;
import flash.system.Security;
import flash.utils.ByteArray;
import org.alivepdf.display.*;
import org.alivepdf.fonts.*;
import org.alivepdf.layout.*;
import org.alivepdf.pdf.*;
import org.alivepdf.saving.*;
public class WorldTest extends Sprite
{
private var saveButton:MyButton = new MyButton();
// declared of logo or imagen on top of the PDF's document.
[Embed(source="FCMap_World.swf", mimeType="application/octet-stream" )]
protected var jpgBytes:Class;
protected var pdf:PDF;
public function WorldTest() {
saveButton.y = 0;
addChild(saveButton);
this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
}
protected function onMouseClickEvent(event:Event):void{
pdf = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.LETTER);
pdf.setDisplayMode(Display.FULL_WIDTH);
pdf.addPage();
pdf.addImage(new jpgBytes() as DisplayObject, null, 0, 0, 0);
var bytes:ByteArray = pdf.save(Method.LOCAL);
var file:FileReference = new FileReference();
file.save(bytes, "myPDF.pdf");
}
}
}
It seems like you are trying to add swf file as image.
You need to make an image from it first.
var map : MovieClip = new jpgBytes();
var bitmapData : BitmapData = new BitmapData(map.width, map.height);
bitmapData.draw(map);
var bitmap : Bitmap = new Bitmap(bitmapData);
You can try to use another library called PDFcase for creating pdf in ActionScript.
This example shows how to add images to pdf.
I am new to the social network analysis and twitter API.I wanted to collect tweets on certain topic .So i have written the following code
package com;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
public class TwitterSearchAdvance {
public static void main(String[] vishal) throws TwitterException,
IOException {
// List<Tweet> Data = new ArrayList<Tweet>();
StringBuffer stringbuffer = new StringBuffer();
Twitter twitter = new TwitterFactory().getInstance();
for (int page = 0; page <= 4; page++) {
Query query = new Query("Airtel");
// query.setRpp(100); // 100 results per page
QueryResult qr = twitter.search(query);
List<Status> qrTweets = qr.getTweets();
System.out.println("-------------------" + qrTweets.size());
// break out if there are no more tweets
for (Status t : qrTweets) {
System.out.println(t.getCreatedAt() + ": " + t.getText());
stringbuffer.append(t);
stringbuffer.append("\n");
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
"/home/vishal/FirstDocu.txt"), true));
bw.write(stringbuffer.toString());
bw.newLine();
// bw.write(t.getCreatedAt() + ": " + t.getText());
bw.close();
}
}
}
}
But when i run the following program the following error starts coming
Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/configuration.html for the detail.
at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:200)
at twitter4j.TwitterImpl.get(TwitterImpl.java:1833)
at twitter4j.TwitterImpl.search(TwitterImpl.java:282)
at com.TwitterSearchAdvance.main(TwitterSearchAdvance.java:28)
Where do i need to provide credentials in my program
Thanks
Have a look at the options here http://twitter4j.org/en/configuration.html
There are a number of ways to provide credentials to your program:
Properties File
ConfigurationBuilder class
System properties
Environment Variables
All details and instructions can be found in the link