{
Document document = new Document(PageSize.A3, 32, 32, 32, 32);
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "", "");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from test3");
List arrlist = new ArrayList();
while(rs.next()){
String xa =rs.getString("display");
if(xa.equals("1")){
arrlist.add(rs.getString("question_text"));
}
}
Collections.shuffle(arrlist);
for(int i=0;i<5;i++){
String str = (String) arrlist.get(i);
htmlWorker.parse(new StringReader(str));
htmlWorker.parse(new StringReader("<br>"));
}
document.close();
}
Above is a code snippet which gets data from the database and displays on pdf.How do I add a title, logo,date and page no.to this?Please if anyone could help.I am using itext.
Please check out some of the page event examples. Currently, you're creating an unnamed PdfWriter instance. Change this into a named writer instance and declare a page event before opening the document instance:
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
writer.setPageEvent(new MyPageEvents());
The MyPageEvents class is to be written by you. It needs to implement the PdfPageEvent interface, or, better yet, extend the PdfPageEventHelper class.
Adding a title, logo, date,... in the header is done by implementing the onEndPage() method. Do NOT use the onStartPage() method without reading the documentation!!!
Use these onEndPage() examples for inspiration.
To add an Image, add a member variable img to your page event, and add this to your constructor:
Image img = Image.getInstance(pathToImage);
img.setAbsolutePosition(36, 806);
Then in the onEndPage() method, do this:
writer.getDirectContent().addImage(img);
This will add the image at position x = 36, y = 806 (near the left top of the page). Note that it's important that you don't create a new Image instance for every new page, because that would result in a bloated PDF file. By creating the Image in the constructor of your page event and reusing the object, the image bytes will be present only once in the PDF file.
Related
I have some html content which I'm inserting into a pptx slide and persisting the formatting.
I'm using apache POI for this. A base64 gets generated which I later convert to pptx. Up until now I haven't faced any issues. But now hyperlinks are causing problems.
When I just create the hyperlink with method createHyperlink(), it works fine and the PPT gets generated but the link is not set so it doesn't do any action when clicked.
When I set the URL either through setAddress() or linkToUrl() methods, the PPT generated seems corrupted and windows asks me if it can "repair" it. After the repair option, when the PPT opens, the link does not work.
Below is the code I wrote:
Approach 1:
textRun.createHyperlink();
textRun.getHyperlink().linkToUrl("http://poi.apache.org");
Approach 2:
textRun.createHyperlink();
textRun.getHyperlink().setAddress("http://poi.apache.org");
Approach 3:
XSLFHyperlink link = textRun.createHyperlink();
link.setAddress("http://poi.apache.org");
Approach 4:
XSLFHyperlink link = textRun.createHyperlink();
link.linkToUrl("http://poi.apache.org");
None of these approaches work.
Does anyone have any idea regarding this and provide me the right solution?
Not clear where your problem is. For me XSLFHyperlink.setAddress as well as XSLFHyperlink.linkToUrl are working and create usable hyperlinks using current Apache POI version 5.2.3.
Complete example:
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
import java.awt.Rectangle;
public class CreatePPTXTextShapeWithLinkedText {
static XSLFTextParagraph drawRectWithText(XSLFSlide slide, int x, int y, int width, int height, String text) {
XSLFAutoShape shape = slide.createAutoShape();
Rectangle rect = new Rectangle(x, y, width, height);
shape.setAnchor(rect.getBounds2D());
shape.setShapeType(ShapeType.RECT);
shape.setText(text);
shape.setTextDirection(TextShape.TextDirection.HORIZONTAL);
shape.setVerticalAlignment(VerticalAlignment.MIDDLE);
XSLFTextParagraph xslfTextParagraph = shape.getTextParagraphs().get(0);
xslfTextParagraph.setTextAlign(TextParagraph.TextAlign.CENTER);
return xslfTextParagraph;
}
public static void main(String[] args) throws Exception {
SlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = (XSLFSlide)slideShow.createSlide();
XSLFTextParagraph xslfTextParagraph = drawRectWithText(slide, 100, 100, 500, 50, "This is a test text. ");
XSLFTextRun xslfTextRun = xslfTextParagraph.addNewTextRun();
xslfTextRun.setText("This text is linked to Apache POI.");
XSLFHyperlink xslfHyperlink = xslfTextRun.createHyperlink();
xslfHyperlink.setAddress("http://poi.apache.org");
xslfTextRun = xslfTextParagraph.addNewTextRun();
xslfTextRun.setText(" This is a test text again. ");
xslfTextRun = xslfTextParagraph.addNewTextRun();
xslfTextRun.setText("This text is linked to Stackoverflow.");
xslfHyperlink = xslfTextRun.createHyperlink();
xslfHyperlink.linkToUrl("https://stackoverflow.com");
xslfTextRun = xslfTextParagraph.addNewTextRun();
xslfTextRun.setText(" This is a test text again. ");
FileOutputStream out = new FileOutputStream("./CreatePPTXTextShapeWithLinkedText.pptx");
slideShow.write(out);
out.close();
slideShow.close();
}
}
It creates:
Note: To open a hyperlink while editing a presentation, right-click the hyperlink and select Open Hyperlink on the shortcut menu. Only during a slide show hyperlinks will work as you might think they should. See Open a hyperlink. But that's by design in PowerPoint.
I want to import a text file (not user import), for example:
Coding
There are several languages...
When the file is read, the first line should be in larger font and bolded, and the other lines can be kept in the text file format. Not sure how I can use JSP and link it to HTML
You've left out a lot of important details (Q: How is the JSP getting invoked? Q: Are you passing a filename in the URL? Etc. etc.)
But here's a simple example that might get you started in the right direction:
<%
BufferedReader reader = new BufferedReader(new FileReader("myile.txt"));
boolean firstLine = true;
String line;
while((line = reader.readLine())!= null){
if (firstLine) {
line = "<b>" + line + "</b>";
firstLine = false;
}
out.println(line);
}
reader.close();
%>
I am going to parse some web pages using Java program. For this purpose I wrote a small code for parsing page content by using xpath as selector. For parsing different sites you need to find the appropriate xpath per each site. The problem is for doing that you need an operator to find the write xpath for you. (for example using firepath firefox addon) Suppose you dont know what page you should parse or the number of sites get really big for operator to find right xpath. In this case you need a way for parsing pages without using any selector. (same scenario exist for CSS selector) Or there should be a way to find xpath automatically! I was wondering what is the method of parsing web pages in this way?
Here is the small code which I wrote for this purpose, please feel free to extend that in presenting your solutions.
public downloadHTML(String url) throws IOException{
CleanerProperties props = new CleanerProperties();
// set some properties to non-default values
props.setTranslateSpecialEntities(true);
props.setTransResCharsToNCR(true);
props.setOmitComments(true);
// do parsing
TagNode tagNode = new HtmlCleaner(props).clean(
new URL(url)
);
// serialize to xml file
new PrettyXmlSerializer(props).writeToFile(
tagNode, "c:\\TEMP\\clean.xml", "utf-8"
);
}
public static void testJavaxXpath(String pattern)
throws ParserConfigurationException, SAXException, IOException,
FileNotFoundException, XPathExpressionException {
DocumentBuilder b = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
org.w3c.dom.Document doc = b.parse(new FileInputStream(
"c:\\TEMP\\clean.xml"));
// Evaluate XPath against Document itself
javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList) xPath.evaluate(pattern,
doc.getDocumentElement(), XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); ++i) {
Element e = (Element) nodes.item(i);
System.out.println(e.getFirstChild().getTextContent());
}
}
I have this situation:
The user has an editor on his page and he enters text(with colors, formating, hyperlinks and he can also add pictures). When he clicks Submit the data from the editor(with the proper formating) must be sent to a specific placeholder in a Microsoft Office Word document.
I am using OpenXml SDK to write in the document and I tried HtmlToOpenXml so I can read the html.
I use HtmlToOpenXml and from the html string(from the user) I det a couple of paragraphs and now I have to insert them in the content control. Do you know how can I find the control and append them in it(if possible)
I managed to fix this and here is the code I used
//name of the file which will be saved
const string filename = "test.docx";
//html string to be inserted and rendered in the word document
string html = #"<b>Test</b>";
//the Html2OpenXML dll supports all the common html tags
//open the template document with the content controls in it(in my case I used Richtext Field Content Control)
byte[] byteArray = File.ReadAllBytes("..."); // template path
using (MemoryStream generatedDocument = new MemoryStream())
{
generatedDocument.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(generatedDocument, true))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
//just in case
if (mainPart == null)
{
mainPart = doc.AddMainDocumentPart();
new Document(new Body()).Save(mainPart);
}
HtmlConverter converter = new HtmlConverter(mainPart);
Body body = mainPart.Document.Body;
//sdtElement is the Content Control we need.
//Html is the name of the placeholder we are looking for
SdtElement sdtElement = doc.MainDocumentPart.Document.Descendants<SdtElement>()
.Where(
element =>
element.SdtProperties.GetFirstChild<SdtAlias>() != null &&
element.SdtProperties.GetFirstChild<SdtAlias>().Val == "Html").FirstOrDefault();
//the HtmlConverter returns a set of paragraphs.
//in them we have the data which we want to insert in the document with it's formating
//After that we just need to append all paragraphs to the Content Control and save the document
var paragraphs = converter.Parse(html);
for (int i = 0; i < paragraphs.Count; i++)
{
sdtElement.Append(paragraphs[i]);
}
mainPart.Document.Save();
}
File.WriteAllBytes(filename, generatedDocument.ToArray());
}
I'm trying to create a header with an image; the header is added, but the image is missing in header. My application is deployed on an Oracle Weblogic server (using Java EE and Hibernate).
I'm trying to create the image like this. getImage(seasonalFilter.getPictureFileId()).getAbsolutePath(). The image path is something like this: /tmp/6461346546165461313_65464.jpg.
Note that I want to add text under the image in the header (for every page).
public File convertHtmlToPdf(String JSONString, ExportQueryTypeDTO queryType, String htmlText, ExportTypeDTO type) throws VedStatException {
try {
File retFile = null;
FilterDTO filter = null;
HashMap<Object, Object> properties = new HashMap<Object, Object>(queryType.getHashMap());
filter = JSONCoder.decodeSeasonalFilterDTO(JSONString);
DateFormat formatter = new SimpleDateFormat("yyyy_MM_dd__HH_mm");
//logger.debug("<<<<<< HTML TEXT: " + htmlText + " >>>>>>>>>>>>>>>>");
StringBuilder tmpFileName = new StringBuilder();
tmpFileName.append(formatter.format(new Date()));
retFile = File.createTempFile(tmpFileName.toString(), type.getSuffix());
OutputStream out = new FileOutputStream(retFile);
com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.LETTER);
com.lowagie.text.pdf.PdfWriter pdfWriter = com.lowagie.text.pdf.PdfWriter.getInstance(document, out);
document.open();
com.lowagie.text.html.simpleparser.HTMLWorker htmlWorker = new com.lowagie.text.html.simpleparser.HTMLWorker(document);
String str = htmlText.replaceAll("ű", "û").replaceAll("ő", "õ").replaceAll("Ő", "Õ").replaceAll("Ű", "Û");
htmlWorker.parse(new StringReader(str));
if (filter instanceof SeasonalFilterDTO) {
SeasonalFilterDTO seasonalFilter = (SeasonalFilterDTO) filter;
if (seasonalFilter.getPictureFileId() != null) {
logger.debug("Image absolutePath: " + getImage(seasonalFilter.getPictureFileId()).getAbsolutePath());
Image logo = Image.getInstance(getImage(seasonalFilter.getPictureFileId()).getAbsolutePath());
logo.setAlignment(Image.MIDDLE);
logo.setAbsolutePosition(0, 0);
logo.scalePercent(100);
Chunk chunk = new Chunk(logo, 0, 0);
HeaderFooter header = new HeaderFooter(new Phrase(chunk), true);
header.setBorder(Rectangle.NO_BORDER);
document.setHeader(header);
}
}
document.close();
return retFile;
} catch (Exception e) {
throw new VedStatException(e);
}
}
I really dislike your false allegation that "Every tutorial is based on C:\imagelocation\dsadsa.jpg"
I'm the author of two books and many tutorials about iText and I know for a fact that what you say doesn't make any sense. Take a look at my name: "Bruno Lowagie." You are using my name in your code, so please believe me when I say you're doing it completely wrong.
Instead of HTMLWorker, you should use XML Worker. HTMLWorker is no longer supported and will probably be removed from iText in the near future.
I see that you're also using the HeaderFooter class. This class has been removed several years ago. Please take a look at the newer examples: http://www.itextpdf.com/themes/keyword.php?id=221
These examples are written in Java; if you need the C# version, please look for the corresponding C# examples in the SVN repository.
Regarding images, you may want to read chapter 10 of my book.
Finally: please read http://lowagie.com/itext2