Java Code Examples for com.itextpdf.text.pdf.PdfContentByte#endLayer()

The following examples show how to use com.itextpdf.text.pdf.PdfContentByte#endLayer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StampInLayer.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
public static byte[] stampLayer(InputStream _pdfFile, Image iImage, int x, int y, String layername, boolean readLayers) throws IOException, DocumentException
{
    PdfReader reader = new PdfReader(_pdfFile);

    try (   ByteArrayOutputStream ms = new ByteArrayOutputStream()  )
    {
        PdfStamper stamper = new PdfStamper(reader, ms);
        //Don't delete otherwise the stamper flattens the layers
        if (readLayers)
            stamper.getPdfLayers();

        PdfLayer logoLayer = new PdfLayer(layername, stamper.getWriter());
        PdfContentByte cb = stamper.getUnderContent(1);
        cb.beginLayer(logoLayer);

        //300dpi
        iImage.scalePercent(24f);
        iImage.setAbsolutePosition(x, y);
        cb.addImage(iImage);

        cb.endLayer();
        stamper.close();

        return (ms.toByteArray());
    }
}
 
Example 2
Source File: TrpPdfDocument.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
private void addTextAndImage(PcGtsType pc, int cutoffLeft, int cutoffTop, Image img, boolean imageOnly, ExportCache cache) throws DocumentException, IOException {
		lineAndColorList.clear();
		
		PdfContentByte cb = writer.getDirectContentUnder();

		cb.setColorFill(BaseColor.BLACK);
		cb.setColorStroke(BaseColor.BLACK);
		//BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", BaseFont.NOT_EMBEDDED);
		if (!imageOnly){
			cb.beginLayer(ocrLayer);
			//cb.setFontAndSize(bfArial, 32);
			cb.setFontAndSize(mainExportBaseFont, 32);
					
			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));
			TrpShapeTypeUtils.sortShapesByReadingOrderOrCoordinates(regions);
	
			for(RegionType r : regions){
				//TODO add paths for tables etc.
				if (r instanceof TrpTableRegionType){
					
					exportTable(r, cb, cutoffLeft, cutoffTop, false, cache);

				}
				else if(r instanceof TextRegionType){
					TextRegionType tr = (TextRegionType)r;
					//PageXmlUtils.buildPolygon(tr.getCoords().getPoints()).getBounds().getMinX();
					addTextFromTextRegion(tr, cb, cutoffLeft, cutoffTop, mainExportBaseFont, cache);
				}
			}
			
			//scale after calculating lineMeanHeightForAllRegions
			//lineMeanHeight = lineMeanHeight/scaleFactorX;
			
			cb.endLayer();
		}
				
		cb.beginLayer(imgLayer);		
		cb.addImage(img);
		cb.endLayer();
		
		if (highlightTags || highlightArticles){
			
			highlightAllTagsOnImg(lineAndColorList, cb, cutoffLeft, cutoffTop);
		}
		
		/*
		 * draw tag lines
		 */
		

		
//		addTocLinks(doc, page,cutoffTop);
	}
 
Example 3
Source File: TrpPdfDocument.java    From TranskribusCore with GNU General Public License v3.0 4 votes vote down vote up
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(mainExportBaseFont, 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));
		TrpShapeTypeUtils.sortShapesByReadingOrderOrCoordinates(regions);
		
		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();
				
//				logger.debug("region "+ ++i);
//				logger.debug("region minX " + minX);
//				logger.debug("region maxX " + tr.getBoundingBox().getMaxX());

				//if there is only one text region at this vertical section start the text block at the second twelfth
				//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];
					}
					//if textregion contains only one line this is probably a headline
					else 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, mainExportBaseFont, textBlockXStart, cache);
			}
		}
		
		cb.endLayer();	
		
//		addTocLinks(doc, page,cutoffTop);
	}