本文整理匯總了Java中com.itextpdf.text.Document.add方法的典型用法代碼示例。如果您正苦於以下問題:Java Document.add方法的具體用法?Java Document.add怎麽用?Java Document.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.itextpdf.text.Document
的用法示例。
在下文中一共展示了Document.add方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addTitlePage
import com.itextpdf.text.Document; //導入方法依賴的package包/類
private static void addTitlePage(Document document,Resolucion res)
throws DocumentException {
Paragraph preface = new Paragraph();
// We add one empty line
addEmptyLine(preface, 1);
// Lets write a big header
preface.add(new Paragraph("Consejo Profesional de Informatica de Santiago del Estero", catFont));
addEmptyLine(preface, 1);
// Will create: Report generated by: _name, _date
preface.add(new Paragraph( "Comision Directiva del Consejo, " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
smallBold));
addEmptyLine(preface, 2);
preface.add(new Paragraph("Resolucion N°"+res.getNumero_resolucion()+"",
smallBold));
addEmptyLine(preface, 3);
preface.add(new Paragraph("Esta Resolucion de tipo "+res.getTipo()+" formulada para notificar al socio con legajo: "+res.getLegajo_socio()+" de su actual estado como socio del Consejo.\n"
+ res.getDescripcion_solicitud()+" "+res.getDescripcion_resolucion()+" en la fecha "+res.getFecha(),
cuerpo));
document.add(preface);
// Start a new page
document.newPage();
}
示例2: showBlank
import com.itextpdf.text.Document; //導入方法依賴的package包/類
/** Shows a blank document, in case of a problem in generating the PDF */
private byte[] showBlank() throws DocumentException {
final Document document = new Document(PageSize.LETTER); // FIXME - get PageSize from label definition
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("No data have been uploaded. The time is: " + new Date()));
final Barcode128 code128 = new Barcode128();
code128.setGenerateChecksum(true);
code128.setCode(new Date().toString());
document.add(code128.createImageWithBarcode(writer.getDirectContent(), null, null));
document.close();
return baos.toByteArray();
}
示例3: writeSimplePdf
import com.itextpdf.text.Document; //導入方法依賴的package包/類
public static void writeSimplePdf() throws Exception{
//1.新建document對象
//第一個參數是頁麵大小。接下來的參數分別是左、右、上和下頁邊距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//2.建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。
//創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\Documents\\ITextTest.pdf"));
//3.打開文檔
document.open();
//4.向文檔中添加內容
//通過 com.lowagie.text.Paragraph 來添加文本。可以用文本及其默認的字體、顏色、大小等等設置來創建一個默認段落
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 22, Font.BOLD, BaseColor.BLACK);
document.add(new Paragraph("sdfsdfsd全是中文顯示了沒.fsdfsfs",fontChinese));
document.add(new Paragraph("Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new BaseColor(255, 150, 200))));
Paragraph pragraph=new Paragraph("你這裏有中亠好", fontChinese);
document.add(pragraph);
//圖像支持格式 GIF, Jpeg, PNG, wmf
Image gif = Image.getInstance("F:/keyworkspace/survey/WebRoot/images/logo/snlogo.png");
gif.setBorder(5);
gif.scaleAbsolute(30,30);
gif.setAlignment(Image.RIGHT|Image.TEXTWRAP);
document.add(gif);
Paragraph pragraph11=new Paragraph("你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好你這裏有中亠好", fontChinese);
document.add(pragraph11);
Image gif15 = Image.getInstance("F:/keyworkspace/survey/WebRoot/images/logo/snlogo.png");
// gif15.setBorder(50);
gif15.setBorder(Image.BOX);
gif15.setBorderColor(BaseColor.RED);
// gif15.setBorderColorBottom(borderColorBottom)
gif15.setBorderWidth(1);
gif15.scalePercent(50);
document.add(gif15);
//5.關閉文檔
document.close();
}
示例4: getPDF
import com.itextpdf.text.Document; //導入方法依賴的package包/類
public Document getPDF() throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE + problem.getPid() + ".pdf"));
Image image = Image.getInstance(this.logo);
document.open();
document.add(image);
document.addCreationDate();
document.add(new Paragraph("Title: "+problem.getTitle()));
document.add(new Paragraph("Code: "+problem.getPid()));
document.add(new Paragraph(" "));
document.add(addParagraph("Description",problem.getDescription(), true));
document.add(addParagraph("Input",problem.getInput(), true));
document.add(addParagraph("Output",problem.getOutput(), true));
document.add(addParagraph("Input Example",problem.getInputex().replaceAll("<br/>", ""), true));
document.add(addParagraph("Output Example",problem.getOutputex(), true));
document.add(new Paragraph("Time(ms): "+problem.getTime()));
document.add(new Paragraph("Memory(kb): "+problem.getMemory()));
document.add(new Paragraph("Source(kb): "+problem.getFontsize()));
document.addTitle("Challenger Online Judge");
document.addAuthor("Chjudge");
document.close();
return document;
}
示例5: addOfferInstances
import com.itextpdf.text.Document; //導入方法依賴的package包/類
private void addOfferInstances(List<OfferInstance> offerInstances, Document document, BaseFont bf)
throws DocumentException {
if(offerInstances.isEmpty()) {
return;
}
document.add(new Paragraph("Wybrane oferty: ", new Font(bf, 12)));
PdfPTable offerInstancesTable = new PdfPTable(3);
offerInstancesTable.setWidthPercentage(100);
offerInstancesTable.setSpacingBefore(18f);
offerInstancesTable.setSpacingAfter(18f);
createofferInstancesTableHeaders(offerInstancesTable);
createofferInstancesTableContent(offerInstances, offerInstancesTable);
document.add(offerInstancesTable);
}
示例6: IMGToPDF
import com.itextpdf.text.Document; //導入方法依賴的package包/類
public void IMGToPDF(String RESOURCES, String result) throws DocumentException, FileNotFoundException, BadElementException, IOException{
ProgressBar progrsbar=new ProgressBar();
progrsbar.showProgress();
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(result));
// step 3
document.open();
// step 4
Image img;
img = Image.getInstance(RESOURCES);
Image.getInstance(img);
document.add(img);
progrsbar.updatePercent(100);
document.close();
}
示例7: testChangingMargins
import com.itextpdf.text.Document; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/38057241/itextpdf-different-margin-on-specific-page">
* itextpdf different margin on specific page
* </a>
* <p>
* This test shows how to set different margins to separate pages.
* </p>
*/
@Test
public void testChangingMargins() throws IOException, DocumentException
{
StringBuilder builder = new StringBuilder("test");
for (int i = 0; i < 100; i++)
builder.append(" test");
String test = builder.toString();
try ( OutputStream pdfStream = new FileOutputStream(new File(RESULT_FOLDER, "ChangingMargins.pdf")))
{
Document pdfDocument = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
PdfWriter.getInstance(pdfDocument, pdfStream);
pdfDocument.open();
for (int m = 0; m < pdfDocument.getPageSize().getWidth() / 2 && m < pdfDocument.getPageSize().getHeight() / 2; m += 100)
{
// pdfDocument.setMargins(m, m, 100, 100);
pdfDocument.setMargins(m, m, m, m);
pdfDocument.newPage();
pdfDocument.add(new Paragraph(test));
}
pdfDocument.close();
}
}
示例8: makePDF
import com.itextpdf.text.Document; //導入方法依賴的package包/類
private static void makePDF(Bitmap bmp, File file) {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
document.addAuthor(FullscreenActivity.mAuthor.toString());
document.addTitle(FullscreenActivity.mTitle.toString());
document.addCreator("OpenSongApp");
if (bmp!=null && bmp.getWidth()>bmp.getHeight()) {
document.setPageSize(PageSize.A4.rotate());
} else {
document.setPageSize(PageSize.A4);
}
document.addTitle(FullscreenActivity.mTitle.toString());
document.open();//document.add(new Header("Song title",FullscreenActivity.mTitle.toString()));
BaseFont urName = BaseFont.createFont("assets/fonts/Lato-Reg.ttf", "UTF-8",BaseFont.EMBEDDED);
Font TitleFontName = new Font(urName, 14);
Font AuthorFontName = new Font(urName, 10);
document.add(new Paragraph(FullscreenActivity.mTitle.toString(),TitleFontName));
document.add(new Paragraph(FullscreenActivity.mAuthor.toString(),AuthorFontName));
addImage(document,bmp);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
示例9: writeTree
import com.itextpdf.text.Document; //導入方法依賴的package包/類
private void writeTree(Document document) throws DocumentException, IOException {
if(mTreeReport == null) {
return;
}
Paragraph title = createTitleParagraph();
document.add(title);
addSpace(document);
PdfPTable titleTable = new PdfPTable(new float[]{1f, 3f});
titleTable.setWidthPercentage(100f);
getParagraph(titleTable, MobileRangerApplication.getAppContext().getString(R.string.form_date_title), mTreeReport.getDate(), 0);
getParagraph(titleTable, MobileRangerApplication.getAppContext().getString(R.string.form_reporting_employee_title), mTreeReport.getReportingEmployee(), 0);
document.add(titleTable);
addSpace(document);
if(mTreeReport.getLocations() != null && mTreeReport.getLocations().size() > 0) {
for (TreeLocation location : mTreeReport.getLocations()) {
PdfPTable table = createTableForLocation(location);
document.add(table);
document.newPage();
}
}
}
示例10: createSimpleTextPdf
import com.itextpdf.text.Document; //導入方法依賴的package包/類
/**
* This method creates a PDF with a single styled paragraph.
*/
static byte[] createSimpleTextPdf() throws DocumentException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase("Beware: ", new Font(FontFamily.HELVETICA, 12, Font.BOLDITALIC)));
paragraph.add(new Phrase("The implementation of ", new Font(FontFamily.HELVETICA, 12, Font.ITALIC)));
paragraph.add(new Phrase("MarginFinder", new Font(FontFamily.COURIER, 12, Font.ITALIC)));
paragraph.add(new Phrase(" is far from optimal. It is not even correct as it includes all curve control points which is too much. Furthermore it ignores stuff like line width or wedge types. It actually merely is a proof-of-concept.", new Font(FontFamily.HELVETICA, 12, Font.ITALIC)));
document.add(paragraph);
document.close();
return baos.toByteArray();
}
示例11: main
import com.itextpdf.text.Document; //導入方法依賴的package包/類
/**
* Generates a document with a header containing Page x of y and with a Watermark on every page.
* @param args no arguments needed
*/
public static void main(String args[]) {
try {
// step 1: creating the document
Document doc = new Document(PageSize.A4, 50, 50, 100, 72);
// step 2: creating the writer
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("pageNumbersWatermark.pdf"));
// step 3: initialisations + opening the document
writer.setPageEvent(new PageNumbersWatermark());
doc.open();
// step 4: adding content
String text = "some padding text ";
for (int k = 0; k < 10; ++k)
text += text;
Paragraph p = new Paragraph(text);
p.setAlignment(Element.ALIGN_JUSTIFIED);
doc.add(p);
// step 5: closing the document
doc.close();
}
catch ( Exception e ) {
e.printStackTrace();
}
}
示例12: testDoubleSpace
import com.itextpdf.text.Document; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/35699167/double-space-not-being-preserved-in-pdf">
* Double space not being preserved in PDF
* </a>
* <p>
* Indeed, the double space collapses into a single one when copying&pasting from the
* generated PDF displayed in Adobe Reader. On the other hand the gap for the double
* space is twice as wide as for the single space. So this essentially is a quirk of
* copy&paste of Adobe Reader (and some other PDF viewers, too).
* </p>
*/
@Test
public void testDoubleSpace() throws DocumentException, IOException
{
try ( OutputStream pdfStream = new FileOutputStream(new File(RESULT_FOLDER, "DoubleSpace.pdf")))
{
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorderWidth(0.5f);
table.getDefaultCell().setBorderColor(BaseColor.LIGHT_GRAY);
table.addCell(new Phrase("SINGLE SPACED", new Font(BaseFont.createFont(), 36)));
table.addCell(new Phrase("DOUBLE SPACED", new Font(BaseFont.createFont(), 36)));
table.addCell(new Phrase("TRIPLE SPACED", new Font(BaseFont.createFont(), 36)));
Document pdfDocument = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
PdfWriter.getInstance(pdfDocument, pdfStream);
pdfDocument.open();
pdfDocument.add(table);
pdfDocument.close();
}
}
示例13: testAppendPDFs
import com.itextpdf.text.Document; //導入方法依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/29001852/how-to-create-a-pdf-and-you-then-merge-another-pdf-to-the-same-document-using-it">
* how to create a PDF and you then merge another pdf to the same document using itext
* </a>
* <p>
* Testing the OP's method with <code>paginate</code> set to <code>false</code>
* </p>
*/
@Test
public void testAppendPDFs() throws IOException, DocumentException
{
try (
InputStream testA4Stream = getClass().getResourceAsStream("testA4.pdf");
InputStream fromStream = getClass().getResourceAsStream("from.pdf");
InputStream prefaceStream = getClass().getResourceAsStream("preface.pdf");
InputStream type3Stream = getClass().getResourceAsStream("Test_Type3_Problem.pdf");
FileOutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "appendPdfs.pdf"));
)
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, output);
document.open();
document.add(new Paragraph("Some content to start with"));
appendPDFs(Arrays.asList(testA4Stream, fromStream, prefaceStream, type3Stream), writer, document, null, false);
document.close();
}
}
示例14: writeQuestions
import com.itextpdf.text.Document; //導入方法依賴的package包/類
private void writeQuestions(Paragraph paragraph, Document document, boolean showCorrectAnswer,
ArrayList<Question> qlist) throws DocumentException, IOException
{
for (int i = 0; i < qlist.size(); i++)
{
Question question = qlist.get(i);
paragraph.clear();
// addQuestionNumber(paragraph, i, qlist.size());
addQuestionText(paragraph, question, i);
addAnswerTexts(paragraph, showCorrectAnswer, question);
fixFonts(paragraph);
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
table.setKeepTogether(true);
PdfPCell cell = new PdfPCell();
cell.addElement(paragraph);
cell.setBorderColor(BaseColor.WHITE);
cell.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell);
document.add(table);
}
}
示例15: main
import com.itextpdf.text.Document; //導入方法依賴的package包/類
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document d = new Document(PageSize.A4);
FileOutputStream fos = new FileOutputStream("teste.pdf");
PdfWriter.getInstance(d, fos);
d.open();
PdfPTable pTable = new PdfPTable(3);
PdfPCell cell1 = new PdfPCell(new Phrase("111111111"));
PdfPCell cell2 = new PdfPCell(new Phrase("222222222"));
cell2.setRowspan(5);
PdfPCell cell3 = new PdfPCell(new Phrase("333333333"));
pTable.addCell(cell1);
pTable.addCell(cell2);
pTable.addCell(cell3);
pTable.addCell(cell1);
pTable.addCell(cell3);
pTable.addCell(cell1);
pTable.addCell(cell3);
pTable.addCell(cell1);
pTable.addCell(cell3);
pTable.addCell(cell1);
pTable.addCell(cell3);
pTable.addCell(cell1);
pTable.addCell(cell3);
pTable.addCell(cell1);
pTable.addCell(cell3);
d.add(pTable);
d.close();
}