Displaying Jtidy error/warning messages in a GUI JTextArea - java

I am writing a program that uses jtidy to clean up html from source code obtained from a URL. I want to display the errors and warnings in a GUI, in a JTextArea. How would I "reroute" the warnings from printing to stdout to the JTextArea? I've looked over the Jtidy API and don't see anything that does what I want. Anyone know how I can do this, or if it's even possible?
// testing jtidy options
public void test(String U) throws MalformedURLException, IOException
{
Tidy tidy = new Tidy();
InputStream URLInputStream = new URL(U).openStream();
File file = new File("test.html");
FileOutputStream fop = new FileOutputStream(file);
tidy.setShowWarnings(true);
tidy.setShowErrors(0);
tidy.setSmartIndent(true);
tidy.setMakeClean(true);
tidy.setXHTML(true);
Document doc = tidy.parseDOM(URLInputStream, fop);
}

Assuming JTidy prints errors and warnings to stdout, you can just temporarily change where System.out calls go:
PrintStream originalOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream myOutputStream = new PrintStream(baos);
System.setOut(myOutputStream);
// your JTidy code here
String capturedOutput = new String(baos.toByteArray(), StandardCharsets.UTF_8);
System.setOut(originalOut);
// Send capturedOutput to a JTextArea
myTextArea.append(capturedOutput);
There is an analogous method if you need to do this for System.err instead/as well.

Related

Removing all embedded files in iTextSharp

As I've become interested in iTextSharp I need to learn C#. Since I know a bit of AutoHotkey (simple yet powerful script programming language for Windows) it is easer for me. However, I often come across code written in Java which is said to be easily converted to C#. Unfortunetly, I have some problems with it. Let's have a look at original code written by Bruno Lowagie.
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfDictionary root = reader.getCatalog();
PdfDictionary names = root.getAsDict(PdfName.NAMES);
names.remove(PdfName.EMBEDDEDFILES);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
}
This is what I have managed to write on my own:
static void removeFiles(string sourceFilePath, string destFilePath)
{
try
{
// read src file
FileStream inputStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
Document source = new Document();
// open for reading
PdfWriter reader = PdfReader.GetInstance(inputStream);
source.Open();
// create dest file
FileStream outputStream = new FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
Document dest = new Document();
PdfWriter writer = PdfWriter.GetInstance(dest, inputStream); // open stream from src
// remove embedded files from dest
PdfDictionary root = dest.getCatalog().getPdfObject();
PdfDictionary names = root.getAsDictionary(PdfName.Names);
names.remove(PdfName.EmbeddedFiles);
// close all
source.Close();
dest.Close();
}
catch (Exception ex)
{
}
}
Unfortunately, there are many errors such as:
'Document' does not contain a definition for 'getCatalog' and no extension method 'getCatalog'
'PdfReader' does not contain a definition for 'GetInstance'
This is what I have managed to do after countless hours of coding and googling.
There are some iTextSharp examples available. See for instance: How to read a PDF Portfolio using iTextSharp
I don't know much about C#, but this is a first attempt to fix your code:
static void RemoveFiles(string sourceFilePath, string destFilePath)
{
// read src file
FileStream inputStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
// open for reading
PdfReader reader = new PdfReader(inputStream);
FileStream outputStream = new FileStream(destFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
PdfStamper stamper = new PdfStamper(reader, outputStream);
// remove embedded files
PdfDictionary root = reader.Catalog;
PdfDictionary names = root.GetAsDict(PdfName.NAMES);
names.Remove(PdfName.EMBEDDEDFILES);
// close all
stamper.Close();
reader.Close();
}
Note that I don't understand why you were using Document and PdfWriter. You should use PdfStamper instead. Also: you are only removing the document-level attachments. If there are file attachment annotations, they will still be present in the PDF.

how to read bullets from RTF file

I have a rtf file which has some text with bullets as shown in the screenshot below
I want to extract the data along with the bullets but when I print in the console, I get junk values. How do I print exactly the same from console.
The way I tried is as below
public static void main(String[] args) throws IOException, BadLocationException {
RTFEditorKit rtf = new RTFEditorKit();
Document doc = rtf.createDefaultDocument();
FileInputStream fis = new FileInputStream("C:\\Users\\Guest\\Desktop\\abc.rtf");
InputStreamReader i =new InputStreamReader(fis,"UTF-8");
rtf.read(i,doc,0);
System.out.println(doc.getText(0,doc.getLength()));
}
Console output:
I assumed junk values are due to console not supporting chareset so I tried to generate a pdf file but in pdf also I get the same junk values.
this is the pdf code
Paragraph de=new Paragraph();
Phrase pde=new Phrase();
pde.add(new Chunk(getText("C:\\Users\\Guest\\Desktop\\abc.rtf"),smallNormal_11));
de.add(pde);
de.getFont().setStyle(BaseFont.IDENTITY_H);
document.add(de);
public static String getText() throws IOException, BadLocationException {
RTFEditorKit rtf = new RTFEditorKit();
Document doc = rtf.createDefaultDocument();
FileInputStream fis = new FileInputStream("C:\\Users\\Guest\\Desktop\\abc.rtf");
InputStreamReader i =new InputStreamReader(fis,"UTF-8");
rtf.read(i,doc,0);
String output=doc.getText(0,doc.getLength());
return output;
}
Despite what you said, my guess is that it is a console encoding problem.
Anyway you can easily check it:
Just replace this line:
System.out.println(doc.getText(0,doc.getLength()));
With these 2 lines :
PrintStream ps = new PrintStream(System.out, true, "UTF-8");
ps.println(doc.getText(0,doc.getLength()));
This will force console encoding to UTF-8.
If it is still wrong, I would suspect your file is not fully rtf-compliant.
I made some tests and your code works well (the console one, I did not try the pdf) under Linux, but the console is natively in UTF-8.

Vaadin Convert and display image as PDF

Does anyone know how image file can be easily converted into PDF format. What I need is to get the image from database and display it on the screen as PDF. What am I doing wrong? I tried to use iText but with no results.
My code:
StreamResource resource = file.downloadFromDatabase();//get file from db
Document converToPdf=new Document();//Create Document Object
PdfWriter.getInstance(convertToPdf, new FileOutputStream(""));//Create PdfWriter for Document to hold physical file
convertToPdf.open();
Image convertJpg=Image.getInstance(resource); //Get the input image to Convert to PDF
convertToPdf.add(convertJpg);//Add image to Document
Embedded pdf = new Embedded("", convertToPdf);//display document
pdf.setMimeType("application/pdf");
pdf.setType(Embedded.TYPE_BROWSER);
pdf.setSizeFull();
Thanks.
You're not using iText correctly:
You never close your writer, so the addition of the image never gets written to the outputstream.
You pass an empty string to your FileOutputStream. If you want to keep the pdf in memory, use a ByteArrayOutputStream. If not, define a temporary name instead.
You pass your Document object, which is a iText-specific object to your Embedded object and treat it like a file. It is not a pdf-file or byte[]. You'll probably want to pass either your ByteArrayOutputStream or read the temp file as a ByteArrayOutputStream into memory and pass that to Embedded.
Maybe someone will use (Vaadin + iText)
Button but = new Button("FV");
StreamResource myResource = getPDFStream();
FileDownloader fileDownloader = new FileDownloader(myResource);
fileDownloader.extend(but);
hboxBottom.addComponent( but );
private StreamResource getPDFStream() {
StreamResource.StreamSource source = new StreamResource.StreamSource() {
public InputStream getStream() {
// step 1
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
// step 2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
com.itextpdf.text.pdf.PdfWriter.getInstance(document, baos);
// step 3
document.open();
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.add(new Paragraph("TEST" ));
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.newPage(); //Opened new page
//document.add(list); //In the new page we are going to add list
document.close();
//file.close();
System.out.println("Pdf created successfully..");
} catch (DocumentException ex) {
Logger.getLogger(WndOrderZwd.class.getName()).log(Level.SEVERE, null, ex);
}
ByteArrayOutputStream stream = baos;
InputStream input = new ByteArrayInputStream(stream.toByteArray());
return input;
}
};
StreamResource resource = new StreamResource ( source, "test.pdf" );
return resource;
}

Create a New Page During Text to PDF conversion Using Itext

I am converting a text file to PDF using iText. The conversion works fine but I need that during conversion if the BufferedReader encounters a certain text, a new PDF Page is Started. This is what I have tried But A new Page is not started when that Text is encountered. My Sample code is as Below(Just the relevant part).
Document output = new Document(PageSize.B3);
FileInputStream fs = new FileInputStream("C:/ABC Statements final/File.TXT");
FileOutputStream file = new FileOutputStream(new File("C:/Pdf Statements/File.PDF"));
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
PdfWriter writer = PdfWriter.getInstance(output, file);
output.open();
writer.open();
.............................
String pageend = "Page Total";
String trimmedend = br.readLine().trim();
if (trimmedend.startsWith(pageend)) {
output.newPage();
}
Maybe you need to change your if-statement to something like this:
String pageend = "page total";
...
if (trimmedend.toLowerCase().contains(pageend)) {
...
}
This way, you avoid case-sensitivity and you avoid the problem of having characters that aren't considered being white space before "page total". Of course: this is just an educated guess. I don't know what your original data stream looks like.

How to Convert from String into PDF in Java

Currently I am using this code but its throwing PrintJobFlavorException. This is my code help me out fixing this one:
public class PJUtil {
public static void main(String[] args) throws Exception {
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
Writer output = null;
String text = "printing in pdfPrinting in Java ";
File file = new File("C:\\CMPSup_AL_.PDF");
output = new BufferedWriter(new FileWriter(file));
output.write(text);
output.close();
InputStream is = new BufferedInputStream(new FileInputStream(file));
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
}
}
and exception is
Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
your printer may not support text based representation. Have a look at this article java printing, specially page 5.
As other have pointed out, you can't just create a file called PDF and print it. If you need to generate PDF then you might take a look at itext.
Just to give you another option for creating PDF files. Try using Apache's PDFBox and take a look at the cookbook. The HelloWorld example shows you how to create a simple PDF document like the one you were trying to create in your sample code.
Also take a look on Jasper Reports http://community.jaspersoft.com/project/jasperreports-library
Change DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF to *DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE*.
E Pavan Varma

Categories

Resources