本文整理汇总了Java中com.itextpdf.text.PageSize类的典型用法代码示例。如果您正苦于以下问题:Java PageSize类的具体用法?Java PageSize怎么用?Java PageSize使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PageSize类属于com.itextpdf.text包,在下文中一共展示了PageSize类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMergeGrandizerFiles
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
* How To Remove Whitespace on Merge
* </a>
* <p>
* Testing {@link PdfDenseMergeTool} using the OP's files.
* </p>
*/
@Test
public void testMergeGrandizerFiles() throws DocumentException, IOException
{
try ( InputStream docA = getClass().getResourceAsStream("Header.pdf");
InputStream docB = getClass().getResourceAsStream("Body.pdf");
InputStream docC = getClass().getResourceAsStream("Footer.pdf"); )
{
PdfDenseMergeTool tool = new PdfDenseMergeTool(PageSize.A4, 18, 18, 5);
PdfReader readerA = new PdfReader(docA);
PdfReader readerB = new PdfReader(docB);
PdfReader readerC = new PdfReader(docC);
try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "GrandizerMerge.pdf")))
{
List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC);
tool.merge(fos, inputs);
}
finally
{
readerA.close();
readerB.close();
readerC.close();
}
}
}
示例2: createPDF
import com.itextpdf.text.PageSize; //导入依赖的package包/类
public void createPDF(String outputFile, ArrayList<Question> qlist, boolean showCorrectAnswer,
PageCounter pagecounter, int maximumPageNumber, String inputFolder) throws DocumentException, IOException
{
_inputFolder = inputFolder;
Document document = new Document(PageSize.A4, 50, 50, 70, 50);
PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
pdfwriter.setBoxSize("art", new Rectangle(36, 54, 559, 788));
pdfwriter.setPageEvent(new HeaderFooter(maximumPageNumber));
if (pagecounter != null)
{
pdfwriter.setPageEvent(pagecounter);
}
document.open();
Paragraph p = new Paragraph();
// p.setSpacingBefore(SPACING);
p.setSpacingAfter(SPACING);
p.setIndentationLeft(INDENTATION);
writeQuestions(p, document, showCorrectAnswer, qlist);
document.close();
}
示例3: writeChartToPDF
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* Save the chart as pdf.
*
* @param chart
* chart that should be saved
* @param fileName
* file name under which chart should be saved
*/
public static void writeChartToPDF(final JFreeChart chart,
final String fileName) {
PdfWriter writer = null;
com.itextpdf.text.Document document = new com.itextpdf.text.Document(
PageSize.A4);
final int width = (int) PageSize.A4.getWidth();
final int height = (int) PageSize.A4.getHeight();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName + ".pdf"));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
示例4: showBlank
import com.itextpdf.text.PageSize; //导入依赖的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();
}
示例5: testShowTextAlignedVsSimpleColumnTopAlignment
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/32162759/columntext-showtextaligned-vs-columntext-setsimplecolumn-top-alignment">
* ColumnText.ShowTextAligned vs ColumnText.SetSimpleColumn Top Alignment
* </a>
* <p>
* Indeed, the coordinates do not line up. The y coordinate of
* {@link ColumnText#showTextAligned(PdfContentByte, int, Phrase, float, float, float)}
* denotes the baseline while {@link ColumnText#setSimpleColumn(Rectangle)} surrounds
* the text to come.
* </p>
*/
@Test
public void testShowTextAlignedVsSimpleColumnTopAlignment() throws DocumentException, IOException
{
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "ColumnTextTopAligned.pdf")));
document.open();
Font fontQouteItems = new Font(BaseFont.createFont(), 12);
PdfContentByte canvas = writer.getDirectContent();
// Item Number
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("36222-0", fontQouteItems), 60, 450, 0);
// Estimated Qty
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("47", fontQouteItems), 143, 450, 0);
// Item Description
ColumnText ct = new ColumnText(canvas); // Uses a simple column box to provide proper text wrapping
ct.setSimpleColumn(new Rectangle(193, 070, 390, 450));
ct.setText(new Phrase("In-Situ : Poly Cable - 100'\nPoly vented rugged black gable 100ft\nThis is an additional description. It can wrap an extra line if it needs to so this text is long.", fontQouteItems));
ct.go();
document.close();
}
示例6: testRotateAndZoomUpperHalfPage
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/35374110/how-do-i-use-itext-to-have-a-landscaped-pdf-on-half-of-a-a4-back-to-portrait-and">
* How do i use iText to have a landscaped PDF on half of a A4 back to portrait and full size on A4
* </a>
* <p>
* This sample shows how to rotate and enlarge the upper half of an A4 page to fit into a new A4 page.
* </p>
*/
@Test
public void testRotateAndZoomUpperHalfPage() throws IOException, DocumentException
{
try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test-upperHalf.pdf")) )
{
PdfReader reader = new PdfReader(resource);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, result);
document.open();
double sqrt2 = Math.sqrt(2);
Rectangle pageSize = reader.getPageSize(1);
PdfImportedPage importedPage = writer.getImportedPage(reader, 1);
writer.getDirectContent().addTemplate(importedPage, 0, sqrt2, -sqrt2, 0, pageSize.getTop() * sqrt2, -pageSize.getLeft() * sqrt2);
document.close();
}
}
示例7: createPdf
import com.itextpdf.text.PageSize; //导入依赖的package包/类
public void createPdf(String filename) throws DocumentException, IOException {
Document document = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// TODO: force iText to respect the order in which content is added
writer.setStrictImageSequence(true);
document.open();
// step 4 - add content into document
String[] imageNames = { "35_Cal_Crutchlow.jpg", "38_Bradley_Smith.jpg", "46_Valentino_Rossi.jpg",
"99_Jorge_Lorenzo.jpg" };
for (int i = 0; i < 4; i++) {
Image image = Image.getInstance("resources/img/" + imageNames[i]);
// TODO: scale image
image.scaleToFit(500, 500); // scale size
document.add(image);
document.add(new Paragraph(imageNames[i]));
}
document.close();
}
示例8: addImage
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* adds an image to the document.
*
* @param doc
* @param iji
* @return
* @throws InterruptedException
* @throws ExecutionException
* @throws IOException
* @throws DocumentException
*/
public Document addImage(Document doc, ImageJobDescription iji)
throws InterruptedException, ExecutionException, IOException,
DocumentException {
// create image worker
ImageWorker job = new ImageWorker(dlConfig, iji);
// submit
Future<DocuImage> jobTicket = imageJobCenter.submit(job);
// wait for result
DocuImage img = jobTicket.get();
// scale the image
Image pdfimg = Image.getInstance(img.getAwtImage(), null);
float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder();
float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder();
// fit the image to the page
pdfimg.scaleToFit(docW, docH);
// add to PDF
doc.add(pdfimg);
return doc;
}
示例9: writeSimplePdf
import com.itextpdf.text.PageSize; //导入依赖的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();
}
示例10: createDocument
import com.itextpdf.text.PageSize; //导入依赖的package包/类
public Document createDocument(File pdfFile) throws DocumentException, IOException{
Document document = new Document(new Rectangle(pageWidth, pageHeight));
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
//写入页尾
this.setFooter(writer);
writer.setFullCompression();
writer.setPdfVersion(PdfWriter.VERSION_1_4);
document.open();
// //加入二维码图片
// Image image = Image.getInstance(System.getProperty(appConfig.getValue("app.root"))+"/images/logoqrcode.png");
// image.scaleAbsolute(40,40);//控制图片大小
// image.setAlignment(Image.LEFT);
// document.add(image);
return document;
}
示例11: saveGraph
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* Save the chart as pdf.
*
* @param chart
* chart that should be saved.
* @param fileName
* file name under which the chart should be saved.
* @throws IOException
* i/o exception
*/
public static void saveGraph(final Chart chart, final String fileName)
throws IOException {
final int width = (int) PageSize.A4.getWidth();
final int height = (int) PageSize.A4.getHeight();
VectorGraphics2D g = new PDFGraphics2D(0.0, 0.0, width, height);
chart.paint(g, width, height);
// Write the vector graphic output to a file
FileOutputStream file = new FileOutputStream(fileName + ".pdf");
try {
file.write(g.getBytes());
} finally {
file.close();
}
}
示例12: makePDF
import com.itextpdf.text.PageSize; //导入依赖的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();
}
}
示例13: drawSexyTriangle
import com.itextpdf.text.PageSize; //导入依赖的package包/类
private static void drawSexyTriangle(PdfWriter writer, boolean rotated)
{
PdfContentByte canvas = writer.getDirectContent();
float x = 36;
float y = 400;
float side = 70;
PdfShading axial = rotated ?
PdfShading.simpleAxial(writer, PageSize.A4.getRight() - y, x, PageSize.A4.getRight() - y, x + side, BaseColor.PINK, BaseColor.BLUE)
: PdfShading.simpleAxial(writer, x, y, x + side, y, BaseColor.PINK, BaseColor.BLUE);
PdfShadingPattern shading = new PdfShadingPattern(axial);
canvas.setShadingFill(shading);
canvas.moveTo(x,y);
canvas.lineTo(x + side, y);
canvas.lineTo(x + (side / 2), (float)(y + (side * Math.sin(Math.PI / 3))));
canvas.closePathFillStroke();
}
示例14: testSplitDocumentA6
import com.itextpdf.text.PageSize; //导入依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/46466747/how-to-split-a-pdf-page-in-java">
* How to split a PDF page in java?
* </a>
* <p>
* This test shows how to split the pages of a document into tiles of A6
* size using the {@link Abstract2DPdfPageSplittingTool}.
* </p>
*/
@Test
public void testSplitDocumentA6() throws IOException, DocumentException {
try (InputStream resource = getClass().getResourceAsStream("document.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "document-A6.pdf"))) {
Abstract2DPdfPageSplittingTool tool = new Abstract2DPdfPageSplittingTool() {
@Override
protected Iterable<Rectangle> determineSplitRectangles(PdfReader reader, int page) {
Rectangle targetSize = PageSize.A6;
List<Rectangle> rectangles = new ArrayList<>();
Rectangle pageSize = reader.getPageSize(page);
for (float y = pageSize.getTop(); y > pageSize.getBottom() + 5; y-=targetSize.getHeight()) {
for (float x = pageSize.getLeft(); x < pageSize.getRight() - 5; x+=targetSize.getWidth()) {
rectangles.add(new Rectangle(x, y - targetSize.getHeight(), x + targetSize.getWidth(), y));
}
}
return rectangles;
}
};
tool.split(result, new PdfReader(resource));
}
}
示例15: testDoubleSpace
import com.itextpdf.text.PageSize; //导入依赖的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();
}
}