I created a PDF file in Java using OpenPDF and inserted a paragraph. The problem is that I want to place it in the middle, not on the left. How can this be done?
Second question: How can I place a word with specific formatting in the test?
For example: "Hello and welcome" (welcome should be bold)
This is my code:
Document document = new Document();
String PDFPath = "output.pdf";
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDFPath));
Font font_bold = new Font(Font.TIMES_ROMAN, 16, Font.BOLD, Color.BLACK);
Font font_normal = new Font(Font.TIMES_ROMAN, 15, Font.NORMAL, Color.BLACK);
Paragraph p1 = new Paragraph("Ordonnance", font_bold);
document.open();
document.add(p1);
document.close();
You need to define the alignment if you want to have the paragraph centred:
p1.setAlignment(Element.ALIGN_CENTER);
To apply formatting to some parts of the text, you can divide it into several chunks. A Chunk in OpenPDF is a string with a certain Font. Instead of adding a String (unformatted) to the paragraph, you add a Chunk-object like new Chunk("Hello and ", fontNormal) which defines the text and the font to be used.
Here is the code for what you want to do according to the question:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
Font fontNormal = new Font(Font.TIMES_ROMAN, 15, Font.NORMAL);
Font fontBold = new Font(Font.TIMES_ROMAN, 16, Font.BOLD);
Paragraph p1 = new Paragraph();
p1.setAlignment(Element.ALIGN_CENTER);
p1.add(new Chunk("Hello and ", fontNormal));
p1.add(new Chunk("welcome", fontBold));
document.add(p1);
document.close();
The result looks like this:
Your first line had some weird declaration, but this should work:
Document document = new Document();
String PDFPath = "D:\\test.pdf";
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDFPath));
Font font_bold = new Font(Font.TIMES_ROMAN, 16, Font.BOLD, Color.BLACK);
Font font_normal = new Font(Font.TIMES_ROMAN, 15, Font.NORMAL, Color.BLACK);
Paragraph p1 = new Paragraph("Ordonnance", font_bold);
p1.setAlignment(Element.ALIGN_CENTER);
document.open();
document.add(p1);
document.close();
You just have to set the alignment of the paragraph
Related
I am using iText 5.5.10 to create a PDF. When I use English words it works fine, but when I use Arabic words it shows empty lines.
What is the problem, and how can I solve it? Thanks in advance.
public void createPDF1(View view) throws IOException, DocumentException {
// BaseFont urName = BaseFont.createFont("assets/subFolder/fontName.TTF", "UTF-8",BaseFont.EMBEDDED);
// Font urFontName = new Font(urName, 12);
Font font = FontFactory.getFont("Simplified Arabic", BaseFont.IDENTITY_H, true, 22, Font.BOLD);
EditText et=(EditText)findViewById(R.id.txt_input);
BaseFont ArialBase = BaseFont.createFont("assets/tahoma.ttf", BaseFont.IDENTITY_H, true);
Font ArialFont = new Font(ArialBase, 20);
// Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font chapterFont = FontFactory.getFont("assets/arialuni.ttf", BaseFont.IDENTITY_H, 16, Font.BOLDITALIC);
Font f=new Font(Font.FontFamily.TIMES_ROMAN,50.0f,Font.UNDERLINE,BaseColor.RED);
Document document = new Document();
String outpath= Environment.getExternalStorageDirectory()+"/mypdf1.pdf";
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outpath));
document.open();
Phrase p = new Phrase("This is incorrect:احمد",ArialFont);
p.add(new Chunk("احمد",ArialFont));
p.add(new Chunk(": 50.00 USD"));
document.add(p);
p = new Phrase("This is correct: ");
p.add(new Chunk("#"+et.getText().toString(),chapterFont));
p.add(new Phrase(": 50.00"));
ColumnText canvas = new ColumnText(writer.getDirectContent());
canvas.setSimpleColumn(36, 750, 559, 780);
canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
canvas.addElement(p);
canvas.go();
document.close();
}
I have tried the suggestions in this post, but it doesn't work.
I am converting html data into pdf,but I got white space where some large data will insert, I'd like to trim off the whitespace for following data
note: header is coming from jasper report
'Document document = new Document(PageSize.A4, 40, 72, getMargins(1),20);`
document.setMarginMirroringTopBottom(true);`
String path = dao.gettomcatserverPath1() + "webapps/wordimages/disch" +patid+ ".pdf";`
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
`String html = "";
List list1 = dao.executeQuery("select wordole from ip.dischargetemplate where patid='"+patid+"'");
html = ((Map)list1.get(0)).get("WORDOLE").toString();
InputStream _ishtml = new ByteArrayInputStream(html.getBytes());
HeaderFooter event = new HeaderFooter();`
writer.setPageEvent(event);`
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));`
document.open();`
Font font = FontFactory.getFont("Times-Roman");
XMLWorkerHelper.getInstance().parseXHtml(writer, document, _ishtml);`
document.close();`
Below code is for writing pdf file;
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(bf));
String fileName = filePath3 + "//DEVIATION_REPORT.pdf";
OutputStream file = new FileOutputStream(new File(fileName));
Font smallFont = new Font(Font.FontFamily.COURIER, 6, Font.NORMAL);
Font headerFont = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);
Font tabFont = new Font(Font.FontFamily.HELVETICA, 5, Font.NORMAL);
Font rusFont = new Font(bf, 5);
Font blueFont = new Font(bf, 5);
blueFont.setColor(BaseColor.BLUE);
Font redFont = new Font(bf, 5);
redFont.setColor(BaseColor.RED);
String comType;
if (compType == 2)
comType = "Character";
else
comType = "Word";
Document doc = new Document();
PdfWriter.getInstance(doc, file);
doc.open();
Image image1 = Image.getInstance(cmdpath + "ScRp.jpg");
image1.setAlignment(Element.ALIGN_LEFT);
Paragraph prg = new Paragraph("Compare", smallFont);
prg.setAlignment(Element.ALIGN_RIGHT);
doc.add(image1);
doc.add(prg);
doc.add(new Paragraph("__________________________________________________"));
doc.add(new Paragraph("Passed Report"));
doc.add(new Paragraph(" "));
PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("No", headerFont));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setPaddingBottom(5);
PdfPCell cell2 = new PdfPCell(new Paragraph("Details", headerFont));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingBottom(5);
table.addCell(cell1);
table.addCell(cell2);
doc.add(table);
doc.newPage();
doc.close();
file.close();
How do I do if i want to make the pdf is non editable. I've tried to use
PdfEncryptor.encrypt(
new PdfReader(fileName),
new FileOutputStream("HelloWorldProtected.pdf"),
null,
"StrongPassword".getBytes(),
PdfWriter.AllowPrinting,
PdfWriter.STRENGTH128BITS);
but compiler stops here. I waited more than 15 minutes but nothing happen.
I even make file.setreadOnly(), but it doesn't user to save it after edit. It still allow user to edit. User can Save AS the document and replace with the non editable one.
Is there any other way we can make the file as non editable. Please advice.
You might want to use iText for such kind of work if its fine with your project. I cant see any other option as if now. I have personally used iText and it is quite robust. let me know if this helps.
Edit:
Encryption settings
http://viralpatel.net/blogs/password-protect-pdf-itext-java/
http://www.jarfinder.com/index.php/java/info/com.lowagie.text.pdf.interfaces.PdfEncryptionSettings
Try searching more on google and SO
EDIT: The long waiting of 15 min. problem might be of file write permissions on your file path.
I am using itext to generate pdf file. I want to align my title in the middle of the page. Presently i am using like this
Paragraph preface = new Paragraph();
for (int i = 0; i < 10; i++) {
preface.add(new Paragraph(" "));
}
Is it correct or is there any another best way to do this.
Use Paragraph#setAlignment(int) :
Paragraph preface = new Paragraph();
preface.setAlignment(Element.ALIGN_CENTER);
See the ALIGN_* constants in the Element interface for more possible values.
Not sure if this is an old version, but for PdfWriter these methods weren't there. Instead I used:
Paragraph p = new Paragraph("This too shall pass");
p.Alignment = Element.ALIGN_CENTER;
public static final String DEST = "results/tables/centered_text.pdf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new CenteredTextInCell().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Paragraph para = new Paragraph("Test", font);
para.setLeading(0, 1);
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell();
cell.setMinimumHeight(50);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.addElement(para);
table.addCell(cell);
document.add(table);
document.close();
}
If you are looking for a solution to Itext7 then you can use the method setTextAlignment(...).
Example:
Paragraph preface = new Paragraph();
// add text
preface.setTextAlignment(TextAlignment.CENTER);
If any one is looking for .NET/C# version, below is how I achieved the CENTER alignment.
I am using iText7 library for .NET/C#, and I achieved this using :
Paragraph preface = new Paragraph();
preface.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
This is what worked for me (itext 5.0.5 ):
Paragraph p3= new Paragraph("Hello" );
p3.setAlignment(Element.ALIGN_CENTER);
I have searching solution for this to align PdfPCell text into right and also center. After modify and change the code sequence it is work.
This code is not work to align text to center.
PdfPCell cell = new PdfPCell();
cell.addElement(new Phrase("Testing Page");
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
After modifying code with this , it is working now.
Paragraph p = new Paragraph("Testing Page");
//Pass Paragraph object into PdfPCell
PdfPCell cell = new PdfPCell(p);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
Is there is a way to tell an iText paragraph contents to write from right to left?
I know it can be done with ColumnText or a table, but I need it in a paragraph (multiple pages can be written).
Here is just an example.
Document document = new Document(PageSize.A1.rotate(), 20, 20, 20, 20);
document.addTitle("YourReport");
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c:\\ITextTest.pdf"));
document.open();
Paragraph TitleHeading = new Paragraph(targetFileName);
TitleHeading.setAlignment(writer.RUN_DIRECTION_RTL);
document.add(TitleHeading);