I'm doing an application that will putting together a pdf with iText library and among other things the source (typography) is selected by jfontchooser (font selector).
The problem is that jfontchooser returns the name of the source. If I select Times New Roman returns exactly "Times New Roman"
But itext needs "Times- Roman" and I cannot find a way how to translate it.
I would like to translate java.awt.font to com.itextpdf.text.fontfactory
the next attempt so get saved in a txt source (of course I put static data to be understood. )
String fuenteNombre = "Times New Roman";
int fuenteSize = 14;
int fuenteEstilo = 1;
Color fuenteColor = new Color(0,0,0,255);
this.fuenteTitulo = FontFactory.getFont(fuenteNombre,
fuenteSize,
fuenteEstilo,
fuenteColor);
i solve the problem, using defaultFontMapper.. see in http://www.forosdelweb.com/f45/problema-con-font-itext-1111176/#post4647340
Related
i made a Java application whose purpose is to offer a Print Preview for PS files.
My program uses Ghostscript and Ghost4J to load the Post Script file and produces a list of Images (one for each page) using the SimpleRenderer.render method. Then using a simple JList i show only the image corresponding to the page the user selected in JList.
This worked fine until a really big PS file occurred, causing an OutOfMemoryError when executing the code
PSDocument pdocument = new PSDocument(new File(filename));
I know that is possibile to read a file a little at a time using InputStreams, the problem is that i can't think of a way to connect the bytes that i read with the actual pages of the document.
Example, i tried to read from PS file 100 MB at a time
int buffer_size = 100000000;
byte[] buffer = new byte[buffer_size];
FileInputStream partial = new FileInputStream(filename);
partial.read(buffer, 0, buffer_size);
document.load(new ByteArrayInputStream(buffer));
SimpleRenderer renderer = new SimpleRenderer();
//how many pages do i have to read?
List<Image> images = renderer.render(document, firstpage ??, lastpage ??);
Am i missing some Ghost4J functionality to read partially a file?
Or has someone other suggestions / approaches about how to solve this problem in different ways?
I am really struggling
I found out I can use Ghost4J Core API to retrieve from a Post Script file a reduced set of pages as Images.
Ghostscript gs = Ghostscript.getInstance();
String[] gsArgs = new String[9];
gsArgs[0] = "-dQUIET";
gsArgs[1] = "-dNOPAUSE";
gsArgs[2] = "-dBATCH";
gsArgs[3] = "-dSAFER";
gsArgs[4] = "-sDEVICE=display";
gsArgs[5] = "-sDisplayHandle=0";
gsArgs[6] = "-dDisplayFormat=16#804";
gsArgs[7] = "-sPageList="+firstPage+"-"+lastPage;
gsArgs[8] = "-f"+filename;
//create display callback (capture display output pages as images)
ImageWriterDisplayCallback displayCallback = new ImageWriterDisplayCallback();
//set display callback
gs.setDisplayCallback(displayCallback);
//run PostScript (also works with PDF) and exit interpreter
try {
gs.initialize(gsArgs);
gs.exit();
Ghostscript.deleteInstance();
} catch (GhostscriptException e) {
System.out.println("ERROR: " + e.getMessage());
e.printStackTrace();
}
return displayCallback.getImages(); //return List<Images>
This solve the problem of rendering page as images in the preview.
However, i could not find a way to use Ghost4J to know total number of pages of PS file (in case the file is too big for opening it with Document.load()).
So, i am still here needing some help
i'm trying to create a pdf looks like this
but when i try string padding, it looks like this in pdf file
here is the part of the c# code i tried. myExcelData is filled from excel file.
for (int i = 0; i < 15; i++)
{
Chunk cSira = new Chunk((i + 1).ToString().PadRight(10), icerikFont);
Chunk cHizmet = new Chunk(myExcelData.Tables[0].Rows[i][6].ToString().PadRight(80), icerikFont);
Chunk cAdet = new Chunk(myExcelData.Tables[0].Rows[i][1].ToString().PadRight(10), icerikFont);
Chunk cBirimFiyat = new Chunk(myExcelData.Tables[0].Rows[i][2].ToString().PadRight(20), icerikFont);
Chunk cTutar = new Chunk(myExcelData.Tables[0].Rows[i][3].ToString().PadRight(20), icerikFont);
d.Add(cSira);
d.Add(cHizmet);
d.Add(cAdet);
d.Add(cBirimFiyat);
d.Add(cTutar);
d.Add(Chunk.NEWLINE);
}
There are two ways to do this:
use a monospaced font (e.g. Courier). Currently you're using a font of which the width of the different characters is different.
download chapter 2 of my book and learn how to use TAB CHUNKS (look for that title on page 48).
Use a PdfPTable as mkl indicates in his comment (maybe you were overlooking the obvious)
If you need a code snippet for option 2, take a look at the DirectorOverview3 example. The result looks like this. If you don't understand Java, read the C# example.
I want to insert a table at a specific position with poi, the table is generated, but I find this table is not visible.
The generated table in doc is visible when previewing or editing this doc with macOS and its text tool, POI can read the table and content, too. I plan to upload 4 pictures to display the process, but I can only post 2 images, sorry for that.
#Test
public void exportDoc() throws Exception {
FileInputStream readFile = new FileInputStream(new File(readDoc));
FileOutputStream replaceFile = new FileOutputStream(new File(replaceDoc));
HWPFDocument document = new HWPFDocument(readFile);
Table table = WordUtil.insertNewTable(document,"${table}");
insertTableInDoc(table);
document.write(replaceFile);
readFile.close();
replaceFile.close();
}
private Table insertNewTable(HWPFDocument doc, String sourceValue) {
Range range = doc.getRange();
Table table = null;
for (int i = 0; i < range.numSections(); ++i) {
Section s = range.getSection(i);
for (int x = 0; x < s.numParagraphs(); x++) {
Paragraph p = s.getParagraph(x);
if (p.text().contains(sourceValue)) {
//remove target text
range.replaceText(sourceValue, "");
table = p.insertTableBefore((short) 3, 3);
return table;
}
}
}
return table;
}
private void insertTableInDoc(Table table) {
int count = 1;
for (int rowNum = 0; rowNum < table.numRows(); rowNum++) {
TableRow tableRow = table.getRow(rowNum);
for (int colNum = 0; colNum < tableRow.numCells(); colNum++) {
TableCell cell = tableRow.getCell(colNum);
Paragraph paragraph = cell.getParagraph(0);
CharacterRun characterRun = paragraph.getCharacterRun(0);
characterRun.insertBefore("number: " + count++);
}
}
}
PS:
I am sure this is not microsoft for mac 's problem, the generate table in doc at windows platform is not visible, too.
(First time to ask question, if anything wrong or my expression is not clear, please let me know and I will modify it without delay. Thanks)
With the current state of the HWPF project, you likely are out of luck when trying to insert content into a .doc file. Your best bet is to use a different format (docx).
I did not look at HWPF for the past year, so I may be wrong here regarding the current state of HWPF:
Some years ago I was developing a custom HWPF library for a client. The major goal for that custom library was the ability to modify .doc files and that Word can process the modified files correctly. Hence I know in how many levels modifying a .doc file can fail in the end. The public HWPF library is not able to handle many aspects of the .doc file format when it comes to modification (textboxes, two-byte character ranges, shape files, nested tables, ... to name a few).
To handle modification correctly, all the "features" of the specific .doc file must be supported by the library. So when there are shapes in the .doc file, HWPF must adjust the position tables of the shapes even when a simple text snippet is inserted and the shapes are not touched. If shapes are not handled, Word will crash when opening the output file.
So if you can, use docx or rtf. If it is an option, you might try one of the commercial libraries which are able to handle .doc files.
Hi i m trying to replace some text in a docx file, but i got problemes with text to be replaced that can be on multiple runs. So i tried this : but it erase everything in the document :/
private void replaceText(XWPFParagraph p, String target, String replacement) {
if (p.getRuns() != null) {
String paragraph = p.getText();
for (int i = 0; i < p.getRuns().size(); i++) {
p.removeRun(i);
}
paragraph = paragraph.replace(target, replacement);
XWPFRun r = new XWPFRun(CTR.Factory.newInstance(), p);
r.setText(paragraph, 0);
}
}
It will surely erase everything because you are removing all the runs in the paragraph. Point to understand here is that the text in the paragraph is stored inside the runs. What getText() does is it returns all the text in all the runs of the paragraph.
Removing all runs and adding just one new run will surely disrupt the font and alignment of the text
You are removing all the runs and then adding one run with the replaced text.
I believe this is not what you wish to achieve.
Just loop over the runs and replace the text inside them.
For one of my projects I chose a different route, I work on the underlying XML data and do a search/replace there which usually works quite nicely.
See https://github.com/centic9/poi-mail-merge for the details, but basically I fetch the CTBody low-level item via
CTBody body = doc.getDocument().getBody();
And then read the full XML body text
// read the current full Body text
String srcString = body.xmlText();
then do the replacements.
Finally I create a new CTBody item with the new contents via
CTBody makeBody = CTBody.Factory.parse(resultStr);
See https://github.com/centic9/poi-mail-merge/blob/master/src/main/java/org/dstadler/poi/mailmerge/MailMerge.java#L81 for the full code-details as there are a few more things that are handled to make it work nicely.
I'm currently making a code that creates an Excel using Java jxl. I've encountered no problem as long as the formula I'm using exists. The problem is, I need to input a formula which excel will not be able to find because we use a special excel to read it. I need my code to retain the invalid formula instead of writing =1 ERROR() on the cell so that when my file is opened using our special excel, it gets the value. How do I go about this?
Here is my code:
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
workbook = Workbook.createWorkbook(file, ws);
WritableSheet s = workbook.createSheet("Sheet1", 0);
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD);
DateFormat dateFormat = new DateFormat("M/d/yyyy H:mm");
NumberFormat dp = new NumberFormat("#");
NumberFormat dp2 = new NumberFormat("#.##");
ResultSetMetaData rsmd = rs.getMetaData();
int nRow = 0;
Label l = null;
Formula f = null;
for(int index = 1; index <= rsmd.getColumnCount(); index++) {
l = new Label(index-1,nRow,rsmd.getColumnName(index),new WritableCellFormat(wf));
s.addCell(l);
}
nRow++;
while(rs.next()) {
String valueDate = VerifyNull.ifNull(rs.getString(1), Constant.BLANK);
f = new Formula(0,nRow,valueDate,new WritableCellFormat(dp));
s.addCell(f);
String rateDate = VerifyNull.ifNull(rs.getString(2), Constant.BLANK);
f = new Formula(1,nRow,rateDate,new WritableCellFormat(dp));
s.addCell(f);
String market = VerifyNull.ifNull(rs.getString(3), Constant.BLANK);
l = new Label(3,nRow,market,new WritableCellFormat(wf));
s.addCell(l);
String ccy = VerifyNull.ifNull(rs.getString(4), Constant.BLANK);
l = new Label(4,nRow,ccy,new WritableCellFormat(wf));
s.addCell(l);
String label = VerifyNull.ifNull(rs.getString(5), Constant.BLANK);
l = new Label(5,nRow,label,new WritableCellFormat(wf));
s.addCell(l);
String quoteDate = VerifyNull.ifNull(rs.getString(6), Constant.BLANK);
f = new Formula(1,nRow,quoteDate,new WritableCellFormat(dp));
s.addCell(f);
String bid = "="+VerifyNull.ifNull(rs.getString(7), Constant.BLANK);
/** The value in the bid = "BDP("EURON Curncy","PX_BID","DATA")1"; */
f = new Formula(6,nRow,bid,new WritableCellFormat(dp2));
s.addCell(f);
/** The value in the offer = "BDP("EURON Curncy","PX_ASK","DATA")1"; */
String offer = "="+VerifyNull.ifNull(rs.getString(8), Constant.BLANK);
f = new Formula(7,nRow,offer,new WritableCellFormat(dp2));
s.addCell(f);
String timeStamp = VerifyNull.ifNull(rs.getString(9), Constant.BLANK);
f = new Formula(8,nRow,timeStamp,new WritableCellFormat(dateFormat));
s.addCell(f);
nRow++;
}
workbook.write();
workbook.close();
I supose that your "special excel" is that you have a plug in with visual basic functions to use whithin cell formulas
I dont know if the current version of JXL support macros or at least don't delete it when you modify an existent Workbook, but in your code your are not updating an existing Workbook, your are creating a new one
have you tried to use your code opening an existing workbook in which the visual basic function works? (it can be an empty one). If it works, you can use that file as a template, copy it to the location you want, rename it, and edit it with JXL
Sorry, I only have time for a quick reply, but have you looked at Apache POI at all? Its API might be of use to you.
However, I'm not 100% sure this is for writing Excel as I think its primary use is reading MS Office files. Still, might be worth a look though.
The problem is, I need to input a formula which excel will not be able to find because we use a special excel to read it. I need my code to retain the invalid formula instead of writing =1 ERROR() on the cell so that when my file is opened using our special excel, it gets the value.
Your problem isn't JExcel or POI. No API will be able to help you with a formula that it cannot be found.
What is this "special excel"? If it's another file, simply read it in and use it.
Your question is confusing. I'm having a hard time understanding what exactly is going on. I'm voting to close unless you can clarify.