Search in sources :

Example 61 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7js-highlevel by itext.

the class JekyllHydeV5 method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    LineSeparator separator = new LineSeparator(new DottedLine(2f, 5f));
    separator.setMarginLeft(10);
    separator.setMarginRight(10);
    boolean chapter = false;
    Div div = new Div();
    String line;
    while ((line = br.readLine()) != null) {
        div = new Div().setFont(font).setFontSize(11).setMarginBottom(18);
        div.add(new Paragraph(line).setFont(bold).setFontSize(12).setMarginBottom(0));
        while ((line = br.readLine()) != null) {
            div.add(new Paragraph(line).setMarginBottom(0).setFirstLineIndent(36));
            if (line.isEmpty()) {
                if (chapter) {
                    div.add(separator);
                }
                document.add(div);
                div = new Div();
                chapter = true;
                break;
            }
        }
    }
    document.add(div);
    // Close document
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) LineSeparator(com.itextpdf.layout.element.LineSeparator) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) Div(com.itextpdf.layout.element.Div) DottedLine(com.itextpdf.kernel.pdf.canvas.draw.DottedLine) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 62 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7js-highlevel by itext.

the class ListSeparatorProperties method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    Style style = new Style();
    style.setBackgroundColor(ColorConstants.YELLOW);
    document.add(createNewSeparator().addStyle(style).setDestination("Top"));
    document.add(new Paragraph("test"));
    document.add(createNewSeparator().setWidth(300).setHorizontalAlignment(HorizontalAlignment.CENTER));
    document.add(createNewSeparator().setMargin(10).setVerticalAlignment(VerticalAlignment.BOTTOM).setBorder(new SolidBorder(0.5f)));
    document.add(createNewSeparator().setMargin(10).setWidth(300));
    document.add(createNewSeparator().setMargin(10).setRelativePosition(10, 10, 50, 10));
    document.add(createNewSeparator().setMargin(10).setWidth(UnitValue.createPercentValue(50)));
    document.add(createNewSeparator().setMargin(10).setWidth(50).setAction(PdfAction.createGoTo("Top")));
    document.add(createNewSeparator().setFixedPosition(100, 200, 350));
    document.add(new AreaBreak());
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.YELLOW).setMarginBottom(10));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.LIGHT_GRAY).setPaddingLeft(20).setPaddingRight(50));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.YELLOW));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.LIGHT_GRAY).setMarginBottom(50));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.YELLOW));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.LIGHT_GRAY).setMargin(50).setPadding(30));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.YELLOW));
    document.add(createNewSeparator().setBackgroundColor(ColorConstants.LIGHT_GRAY));
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Style(com.itextpdf.layout.Style) AreaBreak(com.itextpdf.layout.element.AreaBreak) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) SolidBorder(com.itextpdf.layout.borders.SolidBorder)

Example 63 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project jExifToolGUI by hvdwolf.

the class ExportToPDF method WriteToPDF.

/**
 * This method writes the pdf and is called from the CompareImagesWindow for the there displayed info
 * @param allMetadata
 */
public static void WriteToPDF(List<String[]> allMetadata) {
    List<String[]> imageMetadata = new ArrayList<String[]>();
    File[] files = MyVariables.getLoadedFiles();
    int[] selectedIndices = MyVariables.getSelectedFilenamesIndices();
    File tmpfile;
    String filename;
    String pdfnamepath = "";
    Document doc = null;
    String producedDocs = "";
    boolean isWindows = Utils.isOsFromMicrosoft();
    for (int index : selectedIndices) {
        // First get the data belonging to this file (index)
        for (String[] row : allMetadata) {
            if (Integer.valueOf(row[1]) == index) {
                imageMetadata.add(row);
                logger.trace("index {} rowdata {}", index, Arrays.toString(row));
            }
        }
        filename = files[index].getName();
        tmpfile = files[index];
        pdfnamepath = tmpfile.getParent() + File.separator + Utils.getFileNameWithoutExtension(filename) + ".pdf";
        logger.debug("pdfnamepath {}", pdfnamepath);
        try {
            PdfWriter writer = new PdfWriter(pdfnamepath);
            PdfDocument pdfDoc = new PdfDocument(writer);
            doc = new Document(pdfDoc);
            // Creating the top table
            doc.add(topTable(tmpfile));
            Paragraph paragraph1 = new Paragraph("\n\n" + ResourceBundle.getBundle("translations/program_strings").getString("exppdf.metadata") + " " + filename);
            doc.add(paragraph1);
            // Now writing the metadata table
            doc.add(fillMetadataTable(imageMetadata));
            doc.close();
            producedDocs += pdfnamepath + "<br>";
        } catch (FileNotFoundException e) {
            logger.error("pdf file not found error {}", e);
            e.printStackTrace();
            doc.close();
        }
        MyVariables.setpdfDocs(producedDocs);
        logger.debug("producedDocs {}", producedDocs);
    }
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) File(java.io.File)

Example 64 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project axelor-open-suite by axelor.

the class PrintServiceImpl method generatePDF.

@Override
@Transactional
public Map<String, Object> generatePDF(Print print) throws AxelorException {
    try {
        print = printRepo.find(print.getId());
        String html = generateHtml(print);
        ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
        try (PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfOutputStream))) {
            pdfDoc.setDefaultPageSize(print.getDisplayTypeSelect() == PrintRepository.DISPLAY_TYPE_LANDSCAPE ? PageSize.A4.rotate() : PageSize.A4);
            if (print.getPrintPdfFooter() != null && !print.getHidePrintSettings()) {
                com.itextpdf.layout.Document doc = new com.itextpdf.layout.Document(pdfDoc);
                pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, new TableFooterEventHandler(doc, print));
            }
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setBaseUri(attachmentPath);
            HtmlConverter.convertToPdf(html, pdfDoc, converterProperties);
        }
        String documentName = (StringUtils.notEmpty(print.getDocumentName()) ? print.getDocumentName() : "") + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + FILE_EXTENSION_PDF;
        InputStream pdfInputStream = new ByteArrayInputStream(pdfOutputStream.toByteArray());
        MetaFile metaFile = metaFiles.upload(pdfInputStream, documentName);
        File file = MetaFiles.getPath(metaFile).toFile();
        String fileLink = PdfTool.getFileLinkFromPdfFile(PdfTool.printCopiesToFile(file, 1), metaFile.getFileName());
        if (ObjectUtils.notEmpty(file) && file.exists() && (print.getAttach() || StringUtils.notEmpty(print.getMetaFileField()))) {
            Class<? extends Model> modelClass = (Class<? extends Model>) Class.forName(print.getMetaModel().getFullName());
            Model objectModel = JPA.find(modelClass, print.getObjectId());
            if (ObjectUtils.notEmpty(objectModel)) {
                if (print.getAttach()) {
                    metaFiles.attach(metaFile, documentName, objectModel);
                }
                if (StringUtils.notEmpty(print.getMetaFileField())) {
                    saveMetaFileInModel(modelClass, objectModel, metaFile, print.getMetaFileField());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(print.getPrintSet())) {
            for (Print subPrint : print.getPrintSet()) {
                generatePDF(subPrint);
            }
        }
        return ActionView.define(documentName).add("html", fileLink).map();
    } catch (IOException | ClassNotFoundException e) {
        throw new AxelorException(e, TraceBackRepository.CATEGORY_INCONSISTENCY);
    }
}
Also used : ConverterProperties(com.itextpdf.html2pdf.ConverterProperties) AxelorException(com.axelor.exception.AxelorException) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Print(com.axelor.apps.base.db.Print) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(com.axelor.db.Model) MetaFile(com.axelor.meta.db.MetaFile) File(java.io.File) MetaFile(com.axelor.meta.db.MetaFile) Transactional(com.google.inject.persist.Transactional)

Example 65 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7j-pdfsweep by itext.

the class CleanupLicenseEventsTest method cleanUpRedactAnnotationsWithAdditionalLocationSendsCoreAndCleanUpEventTest.

@Test
public void cleanUpRedactAnnotationsWithAdditionalLocationSendsCoreAndCleanUpEventTest() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfDocument document = new PdfDocument(new PdfReader(INPUT_PATH + "absentICentry.pdf"), new PdfWriter(baos));
    String oldProducer = document.getDocumentInfo().getProducer();
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
    PdfCleanUpLocation lineLoc = new PdfCleanUpLocation(1, new Rectangle(100, 560, 200, 30), ColorConstants.GREEN);
    cleanUpLocations.add(lineLoc);
    PdfCleaner.cleanUpRedactAnnotations(document, cleanUpLocations);
    document.close();
    List<ConfirmEvent> events = handler.getEvents();
    Assert.assertEquals(2, events.size());
    Assert.assertEquals(ITextCoreProductEvent.PROCESS_PDF, events.get(0).getEvent().getEventType());
    Assert.assertEquals(PdfSweepProductEvent.CLEANUP_PDF, events.get(1).getEvent().getEventType());
    try (PdfDocument pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())))) {
        String expectedProdLine = createExpectedProducerLine(new ConfirmedEventWrapper[] { getCoreEvent(), getCleanUpEvent() }, oldProducer);
        Assert.assertEquals(expectedProdLine, pdfDocument.getDocumentInfo().getProducer());
    }
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle) ConfirmEvent(com.itextpdf.commons.actions.confirmations.ConfirmEvent) ByteArrayOutputStream(com.itextpdf.io.source.ByteArrayOutputStream) PdfReader(com.itextpdf.kernel.pdf.PdfReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Aggregations

PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)205 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)194 Document (com.itextpdf.layout.Document)131 Paragraph (com.itextpdf.layout.element.Paragraph)110 PdfReader (com.itextpdf.kernel.pdf.PdfReader)64 PdfFont (com.itextpdf.kernel.font.PdfFont)45 ExtendedITextTest (com.itextpdf.test.ExtendedITextTest)45 IntegrationTest (com.itextpdf.test.annotations.type.IntegrationTest)45 Test (org.junit.Test)45 Rectangle (com.itextpdf.kernel.geom.Rectangle)41 HyphenationConfig (com.itextpdf.layout.hyphenation.HyphenationConfig)32 BufferedReader (java.io.BufferedReader)26 FileReader (java.io.FileReader)26 ArrayList (java.util.ArrayList)26 AreaBreak (com.itextpdf.layout.element.AreaBreak)25 Image (com.itextpdf.layout.element.Image)25 Table (com.itextpdf.layout.element.Table)25 CompositeCleanupStrategy (com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy)22 List (java.util.List)22 Cell (com.itextpdf.layout.element.Cell)21