Search in sources :

Example 16 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project trainning by fernandotomasio.

the class TCATrainningDocumentODF method createAnexo3.

private void createAnexo3(String file) {
    try {
        document.newPage();
        PdfReader reader = new PdfReader(file);
        int n = reader.getNumberOfPages();
        PdfImportedPage page;
        for (int i = 1; i <= n; i++) {
            page = writer.getImportedPage(reader, i);
            // Image imagePage = Image.getInstance(page);
            // imagePage.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(page, 0, 0);
            document.newPage();
        // document.add(imagePage);
        }
    } catch (IOException ex) {
        Logger.getLogger(TCATrainningDocumentODF.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : PdfImportedPage(com.itextpdf.text.pdf.PdfImportedPage) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) PdfReader(com.itextpdf.text.pdf.PdfReader) IOException(java.io.IOException)

Example 17 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addUniformText.

private void addUniformText(PcGtsType pc, int cutoffLeft, int cutoffTop, ExportCache cache) throws DocumentException, IOException {
    PdfContentByte cb = writer.getDirectContentUnder();
    cb.setColorFill(BaseColor.BLACK);
    cb.setColorStroke(BaseColor.BLACK);
    /**
     * The path to the font.
     */
    // FontFactory.register("c:/windows/fonts/arialbd.ttf");
    // BaseFont bf = BaseFont.createFont("/fonts/arialbd.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    cb.beginLayer(ocrLayer);
    // FontFactory.register("arialbd.ttf", "my_bold_font");
    // Font fontTest = FontFactory.getFont("arialbd.ttf", Font.BOLDITALIC);
    cb.setFontAndSize(bfArial, 10);
    List<TrpRegionType> regions = pc.getPage().getTextRegionOrImageRegionOrLineDrawingRegion();
    /*
		 * use reading order comparator for sorting since at this time reading order is more trustable
		 * other sorting is not transitive and seldomly produces "Comparison violates its general contract" exception
		 */
    Collections.sort(regions, new TrpElementReadingOrderComparator<RegionType>(true));
    // Collections.sort(regions, new TrpElementCoordinatesComparator<RegionType>());
    float textBlockXStart = 0;
    int i = 0;
    for (TrpRegionType r : regions) {
        // TODO add paths for tables etc.
        if (r instanceof TrpTableRegionType) {
            exportTable(r, cb, cutoffLeft, cutoffTop, true, cache);
        } else if (r instanceof TrpTextRegionType) {
            TrpTextRegionType tr = (TrpTextRegionType) r;
            // compute average text region start
            // textBlockXStart = (float) (PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX());
            // double minX = PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX();
            // this should result in the the same value as the method in the line above which is deprecated
            double minX = tr.getBoundingBox().getMinX();
            double maxX = tr.getBoundingBox().getMaxX();
            double trWidth = tr.getBoundingBox().getWidth();
            // if (hasSmallerColumn(regions, tr)){
            if (isOnlyRegionInThisRow(regions, tr)) {
                // if (regions.size() == 1){
                logger.debug("only one region in this row!!");
                // indent start of text block under certain preconditions
                if (minX < twelfthPoints[1][0] && (twelfthPoints[1][0] < maxX && trWidth > twelfthPoints[2][0])) {
                    textBlockXStart = twelfthPoints[1][0];
                } else // if textregion contains only one line this is probably a headline
                if (tr.getTextLine().size() == 1) {
                    // logger.debug("tr.getTextLine().size() == 1 ");
                    textBlockXStart = getPrintregionStartX((float) (minX), tr.getBoundingBox().getMaxX());
                } else if (twelfthPoints[2][0] < maxX && trWidth > twelfthPoints[3][0]) {
                    // logger.debug("twelfthPoints[2][0] < tr.getBoundingBox().getMaxX() ");
                    textBlockXStart = twelfthPoints[2][0];
                } else {
                    textBlockXStart = (float) minX;
                }
            } else {
                logger.debug("several columns found, minX of text region is : " + minX);
                // float startWithThisX = (float) (minX < smallerRegionMaxX ? smallerRegionMaxX : minX);
                // textBlockXStart = getPrintregionStartX((float) (startWithThisX));
                /*
					 * this is then used for all lines of a region as start point
					 */
                textBlockXStart = getAverageBeginningOfBaselines(tr);
                textBlockXStart += 40;
            }
            // logger.debug("textBlockXStart " + textBlockXStart);
            addUniformTextFromTextRegion(tr, cb, cutoffLeft, cutoffTop, bfArial, textBlockXStart, cache);
        }
    }
    cb.endLayer();
// addTocLinks(doc, page,cutoffTop);
}
Also used : TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) UnknownRegionType(eu.transkribus.core.model.beans.pagecontent.UnknownRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) TrpTableRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType) RegionType(eu.transkribus.core.model.beans.pagecontent.RegionType) TextRegionType(eu.transkribus.core.model.beans.pagecontent.TextRegionType) TrpTableRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableRegionType) TrpRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpRegionType) TrpTextRegionType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTextRegionType) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Point(java.awt.Point)

Example 18 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addTags.

public void addTags(TrpDoc doc, Set<Integer> pageIndices, boolean useWordLevel2, ExportCache cache) throws DocumentException, IOException {
    PdfContentByte cb = writer.getDirectContentUnder();
    document.newPage();
    int l = 0;
    float posY;
    // BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", BaseFont.NOT_EMBEDDED, true, null, null);
    Set<String> wantedTags = cache.getOnlySelectedTagnames(CustomTagFactory.getRegisteredTagNames());
    // logger.debug("selectedTags Size " + selectedTags.size());
    for (String currTagname : wantedTags) {
        double lineHeight = 12 / scaleFactorY;
        double lineGap = 4 / scaleFactorY;
        // logger.debug("currTagname " + currTagname);
        // get all custom tags with currTagname and text
        HashMap<CustomTag, String> allTagsOfThisTagname = cache.getTags(currTagname);
        // logger.debug("all Tags Of This Tagname " + currTagname);
        if (allTagsOfThisTagname.size() > 0) {
            posY = (float) (twelfthPoints[1][1] + (lineHeight + lineGap) * l);
            if (posY > twelfthPoints[10][1]) {
                document.newPage();
                posY = twelfthPoints[1][1];
                l = 0;
            }
            l++;
            String color = CustomTagFactory.getTagColor(currTagname);
            addUniformTagList(lineHeight, twelfthPoints[1][0], posY, "", currTagname + " Tags:", "", cb, 0, 0, bfArial, twelfthPoints[1][0], false, color, 0, false);
            // addUniformStringTest(lineMeanHeight, twelfthPoints[1][0], posY, currTagname + " Tags:", cb, 0, 0, bfArial, twelfthPoints[1][0], false, color, 0);
            Collection<String> valueSet = allTagsOfThisTagname.values();
            Collection<CustomTag> keySet = allTagsOfThisTagname.keySet();
            HashSet<String> uniqueValues = new HashSet<String>();
            Iterator<CustomTag> it = keySet.iterator();
            while (it.hasNext()) {
                CustomTag currEntry = it.next();
                String currValue = allTagsOfThisTagname.get(currEntry);
                // case for gap tag
                if (currValue == null) {
                    currValue = "";
                }
                String expansion = "";
                // handles continued tags over several lines
                while (currEntry.isContinued() && it.hasNext()) {
                    currEntry = it.next();
                    if (currEntry.isContinued()) {
                        String continued = allTagsOfThisTagname.get(currEntry);
                        currValue = currValue.concat(continued);
                        // soft hyphen
                        currValue = currValue.replaceAll("\u00AD", "");
                        // minus
                        currValue = currValue.replaceAll("\u002D", "");
                        // not sign
                        currValue = currValue.replaceAll("\u00AC", "");
                    // char c = 0xFFFA; String.valueOf(c).replaceAll("\\p{C}", "?");
                    }
                }
                boolean rtl = false;
                if (!currValue.isEmpty() && textIsRTL(currValue)) {
                    rtl = true;
                    // logger.debug("rtl tag found " + currValue);
                    currValue = reverseString(currValue);
                }
                String searchText = currValue;
                if (currTagname.equals(CommentTag.TAG_NAME)) {
                    CommentTag ct = (CommentTag) currEntry;
                    if (ct.getComment() != "") {
                        if (!rtl)
                            expansion = ": " + ct.getComment();
                        else
                            expansion = ct.getComment() + " :";
                    }
                // currValue = currValue.concat(": " + ct.getComment());
                // logger.debug("comment " + currValue);
                } else if (currTagname.equals(AbbrevTag.TAG_NAME)) {
                    AbbrevTag at = (AbbrevTag) currEntry;
                    if (at.getExpansion() != "")
                        if (!rtl)
                            expansion = ": " + at.getExpansion();
                        else
                            expansion = at.getExpansion() + " :";
                } else if (currTagname.equals(GapTag.TAG_NAME)) {
                    GapTag at = (GapTag) currEntry;
                    currValue = currEntry.getTextOfShape();
                    searchText = currValue;
                    int offset = Math.max(at.getOffset(), currValue.length() - 1);
                    String sub1 = currValue.substring(0, offset);
                    String sub2 = currValue.substring(offset);
                    String exp = (String) at.getAttributeValue("supplied");
                    if (exp != null && exp != "") {
                        currValue = sub1.concat("[" + exp + "]").concat(sub2);
                    // expansion = "[" + (String) at.getAttributeValue("supplied") + "]";
                    } else // no supplied attribute - gap must not be in the tag list
                    {
                        continue;
                    }
                } else if (currTagname.equals(SuppliedTag.TAG_NAME)) {
                }
                // make sure that similar tags are only exported once
                if (!uniqueValues.contains(currValue)) {
                    uniqueValues.add(currValue);
                    posY = (float) (twelfthPoints[1][1] + (lineHeight + lineGap) * l);
                    if (posY > twelfthPoints[11][1]) {
                        document.newPage();
                        posY = twelfthPoints[1][1];
                        l = 1;
                    }
                    addUniformTagList(lineHeight, twelfthPoints[1][0], posY, searchText, currValue, expansion, cb, 0, 0, bfArial, twelfthPoints[1][0], true, null, 0, rtl);
                    // logger.debug("tag value is " + currValue);
                    l++;
                }
            }
            l++;
        }
    }
}
Also used : CustomTag(eu.transkribus.core.model.beans.customtags.CustomTag) Point(java.awt.Point) CommentTag(eu.transkribus.core.model.beans.customtags.CommentTag) GapTag(eu.transkribus.core.model.beans.customtags.GapTag) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) AbbrevTag(eu.transkribus.core.model.beans.customtags.AbbrevTag) HashSet(java.util.HashSet)

Example 19 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project TranskribusCore by Transkribus.

the class TrpPdfDocument method addTitlePage.

public void addTitlePage(TrpDoc doc) {
    document.newPage();
    PdfContentByte cb = writer.getDirectContentUnder();
    float lineHeight = twelfthPoints[1][0] / 3;
    float posY = twelfthPoints[1][1];
    addTitleString("Title Page", posY, 0, (float) (lineHeight * 1.5), cb, bfArialBoldItalic);
    posY += lineHeight * 2;
    TrpDocMetadata docMd = doc.getMd();
    if (writeDocMd("Title: ", docMd.getTitle(), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.5;
    }
    if (writeDocMd("Author: ", docMd.getAuthor(), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.5;
    }
    lineHeight = twelfthPoints[1][0] / 6;
    if (writeDocMd("Description: ", docMd.getDesc(), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.2;
    }
    if (writeDocMd("Genre: ", docMd.getGenre(), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.2;
    }
    if (writeDocMd("Writer: ", docMd.getWriter(), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.2;
    }
    if (docMd.getScriptType() != null) {
        if (writeDocMd("Scripttype: ", docMd.getScriptType().toString(), posY, 0, lineHeight, cb, bfArialItalic)) {
            posY += lineHeight * 1.2;
        }
    }
    if (writeDocMd("Language: ", docMd.getLanguage(), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.2;
    }
    if (writeDocMd("Number of Pages in whole Document: ", String.valueOf(docMd.getNrOfPages()), posY, 0, lineHeight, cb, bfArialItalic)) {
        posY += lineHeight * 1.2;
    }
    if (docMd.getCreatedFromDate() != null) {
        if (writeDocMd("Created From: ", docMd.getCreatedFromDate().toString(), posY, 0, lineHeight, cb, bfArialItalic)) {
            posY += lineHeight * 1.2;
        }
    }
    if (docMd.getCreatedToDate() != null) {
        if (writeDocMd("Created To: ", docMd.getCreatedToDate().toString(), posY, 0, lineHeight, cb, bfArialItalic)) {
            posY += lineHeight * 1.5;
        }
    }
    // --- Export settings section
    lineHeight = twelfthPoints[1][0] / 3;
    addTitleString("Export Settings: ", posY, twelfthPoints[1][0], lineHeight, cb, bfArialBoldItalic);
    String imageSetting = (imgOnly ? "Images without text layer" : "Images with text layer");
    String extraTextSetting = (extraTextPage ? "Extra pages for transcribed text are added" : "");
    String blackeningSetting = (doBlackening ? "Sensible data is invisible" : "Sensible data is shown if existent");
    String tagSetting = (highlightTags ? "Tags are highlighted (colored lines) and added at the end" : "No tags shown in export");
    lineHeight = twelfthPoints[1][0] / 6;
    posY += lineHeight * 1.5;
    addTitleString(imageSetting + " / " + extraTextSetting + " / " + blackeningSetting + " / " + tagSetting, posY, twelfthPoints[1][0], lineHeight, cb, bfArialBoldItalic);
    // --- Export settings section end
    // --- Editorial declaration section
    lineHeight = twelfthPoints[1][0] / 3;
    posY += lineHeight * 1.5;
    List<EdFeature> efl = doc.getEdDeclList();
    if (efl.size() >= 0) {
        addTitleString("Editorial Declaration: ", posY, twelfthPoints[1][0], lineHeight, cb, bfArialBoldItalic);
        posY += lineHeight * 1.5;
        lineHeight = twelfthPoints[1][0] / 6;
    }
    for (EdFeature edfeat : efl) {
        addTitleString(edfeat.getTitle() + ": " + edfeat.getDescription() + "\n" + edfeat.getSelectedOption().toString(), posY, twelfthPoints[1][0], lineHeight, cb, bfArial);
        // posY += lineHeight;
        // addTitleString(edfeat.getSelectedOption().toString(), posY, twelfthPoints[1][0], lineHeight, cb, bfArial);
        posY += lineHeight * 1.5;
    }
// --- Editorial declaration section	end
}
Also used : EdFeature(eu.transkribus.core.model.beans.EdFeature) TrpDocMetadata(eu.transkribus.core.model.beans.TrpDocMetadata) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 20 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project mica2 by obiba.

the class PdfUtils method addImage.

public static void addImage(byte[] input, OutputStream output, Image image, String placeholder) throws IOException, DocumentException {
    try (PdfReaderAutoclosable pdfReader = new PdfReaderAutoclosable(input);
        PdfStamperAutoclosable pdfStamper = new PdfStamperAutoclosable(pdfReader, output)) {
        AcroFields form = pdfStamper.getAcroFields();
        List<AcroFields.FieldPosition> positions = form.getFieldPositions(placeholder);
        positions.forEach(p -> {
            image.scaleToFit(p.position.getWidth(), p.position.getHeight());
            image.setAbsolutePosition(p.position.getLeft() + (p.position.getWidth() - image.getScaledWidth()) / 2, p.position.getBottom() + (p.position.getHeight() - image.getScaledHeight()) / 2);
            PdfContentByte cb = pdfStamper.getOverContent(p.page);
            try {
                cb.addImage(image);
            } catch (DocumentException e) {
                throw Throwables.propagate(e);
            }
        });
    }
}
Also used : DocumentException(com.itextpdf.text.DocumentException) AcroFields(com.itextpdf.text.pdf.AcroFields) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Aggregations

PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)29 IOException (java.io.IOException)13 DocumentException (com.itextpdf.text.DocumentException)10 Document (com.itextpdf.text.Document)9 PdfWriter (com.itextpdf.text.pdf.PdfWriter)9 BaseFont (com.itextpdf.text.pdf.BaseFont)8 PdfReader (com.itextpdf.text.pdf.PdfReader)7 FileOutputStream (java.io.FileOutputStream)7 Font (com.itextpdf.text.Font)6 Rectangle (com.itextpdf.text.Rectangle)6 PdfImportedPage (com.itextpdf.text.pdf.PdfImportedPage)5 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)5 Graphics2D (java.awt.Graphics2D)5 Paragraph (com.itextpdf.text.Paragraph)4 PdfGState (com.itextpdf.text.pdf.PdfGState)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)3 Image (com.itextpdf.text.Image)3 Phrase (com.itextpdf.text.Phrase)3 Color (java.awt.Color)3