Snippets
DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
spacer

Snippets

  • Tweet
  • spacer

Recent Snippets

  • Apply protection to presentation
  • Apply protection to slides
  • create PPTX presentations Android
  • Apply Protection on PPTX Shapes
  • removing Protection from Presentation
  • Android PowerPoint component

How To Apply & Remove Protection From PPTX Presentations In Android Apps

11/12/14 by David Zondray in Java
                    //Applying Protection on a PPTX Shapes 

//[Android]

//Open the desired presentation
PresentationEx pTemplate = new PresentationEx("AddingSlides.pptx");

//Slide object for holding temporary slides
SlideEx slide;

//Shape object for holding temporary shapes
ShapeEx shape;

//Traversing through all the slides in presentation
for(int slCount=0;slCount<pTemplate.getSlides().getCount();slCount++)
{
	//Accessing the slide in the presentation
	slide = pTemplate.getSlides().get_Item(slCount);
	
	//Traversing through all the shapes in the slides
	for (int count = 0; count < slide.getShapes().getCount(); count++)
	{
	    shape = slide.getShapes().get_Item(count);

	    //if shape is Autoshape
	    if (shape instanceof AutoShapeEx)
	    {
	        //Type casting to Auto shape and getting auto shape lock
	        AutoShapeEx Ashp = (AutoShapeEx)shape;
	        AutoShapeLockEx AutoShapeLock = Ashp.getShapeLock();

	        //Applying shapes locks
	        AutoShapeLock.setNoMove(true);
	        AutoShapeLock.setNoSelect(true);
	        AutoShapeLock.setNoResize(true);
	    }

	    //if shape is group shape
	    else if (shape instanceof GroupShapeEx)
	    {
	        //Type casting to group shape and getting group shape lock
	        GroupShapeEx Group = (GroupShapeEx)shape;
	        GroupShapeLockEx groupShapeLock = Group.getShapeLock();

	        //Applying shapes locks
	        groupShapeLock.setNoMove(true);
	        groupShapeLock.setNoSelect(true);
	        groupShapeLock.setNoResize(true);
	    }

	    //if shape is Connector shape
	    else if (shape instanceof ConnectorEx )
	    {
	        //Type casting to connector shape and getting connector shape lock
	        ConnectorEx Conn = (ConnectorEx)shape;
	        ConnectorLockEx ConnLock = Conn.getShapeLock();

	        //Applying shapes locks
	        ConnLock.setNoMove(true);
	        ConnLock.setNoSelect(true);
	        ConnLock.setNoResize(true);
	    }

	    //if shape is picture frame
	    else if (shape instanceof PictureFrameEx)
	    {
	        //Type casting to picture frame shape and getting picture frame shape lock
	        PictureFrameEx Pic = (PictureFrameEx)shape;
	        PictureFrameLockEx PicLock = Pic.getShapeLock();

	        //Applying shapes locks
	        PicLock.setNoMove(true);
	        PicLock.setNoSelect(true);
	        PicLock.setNoResize(true);
	    }
	}
}

//Saving the presentation file
pTemplate.write("locked.pptx");

//Removing Protection from the Presentation 

//Protection applied using Aspose.Slides for Android can only be removed with Aspose.Slides. To unlock a shape, set the value of the applied lock to false. The code sample that follows shows how to unlock shapes in a locked presentation.

//[Android]

//Open the desired presentation
PresentationEx pTemplate = new PresentationEx("locked.pptx");

//Slide object for holding temporary slides
SlideEx slide;

//Shape object for holding temporary shapes
ShapeEx shape;

//Traversing through all the slides in presentation
for(int slCount=0;slCount<pTemplate.getSlides().getCount();slCount++)
{
	//Accessing the slide in the presentation
	slide = pTemplate.getSlides().get_Item(slCount);
	
	//Traversing through all the shapes in the slides
	for (int count = 0; count < slide.getShapes().getCount(); count++)
	{
	    shape = slide.getShapes().get_Item(count);

	    //if shape is Autoshape
	    if (shape instanceof AutoShapeEx)
	    {
	        //Type casting to Auto shape and getting auto shape lock
	        AutoShapeEx Ashp = (AutoShapeEx)shape;
	        AutoShapeLockEx AutoShapeLock = Ashp.getShapeLock();

	        //Applying shapes locks
	        AutoShapeLock.setNoMove(false);
	        AutoShapeLock.setNoSelect(false);
	        AutoShapeLock.setNoResize(false);
	    }

	    //if shape is group shape
	    else if (shape instanceof GroupShapeEx)
	    {
	        //Type casting to group shape and getting group shape lock
	        GroupShapeEx Group = (GroupShapeEx)shape;
	        GroupShapeLockEx groupShapeLock = Group.getShapeLock();

	        //Applying shapes locks
	        groupShapeLock.setNoMove(false);
	        groupShapeLock.setNoSelect(false);
	        groupShapeLock.setNoResize(false);
	    }

	    //if shape is Connector shape
	    else if (shape instanceof ConnectorEx )
	    {
	        //Type casting to connector shape and getting connector shape lock
	        ConnectorEx Conn = (ConnectorEx)shape;
	        ConnectorLockEx ConnLock = Conn.getShapeLock();

	        //Applying shapes locks
	        ConnLock.setNoMove(false);
	        ConnLock.setNoSelect(false);
	        ConnLock.setNoResize(false);
	    }

	    //if shape is picture frame
	    else if (shape instanceof PictureFrameEx)
	    {
	        //Type casting to picture frame shape and getting picture frame shape lock
	        PictureFrameEx Pic = (PictureFrameEx)shape;
	        PictureFrameLockEx PicLock = Pic.getShapeLock();

	        //Applying shapes locks
	        PicLock.setNoMove(false);
	        PicLock.setNoSelect(false);
	        PicLock.setNoResize(false);
	    }
	}
}

//Saving the presentation file
pTemplate.write("Unlocked.pptx");
                
0 Replies
  • SQL Server
  • .net c#
  • Bulk Insert
  • ASP.NET

Sql Server Bulk Insert In ASP.net

11/09/14 by P Rohit Kumar in C#
                                    
0 Replies
  • add links inside a PDF file
  • update existing PDF link to go to page
  • Update LinkAnnotation text color
  • Set Link Target to Another PDF File
  • Set Link Destination to a Web Address
  • Set Link Target to a PDF Page
  • Update link in PDF file

Update PDF Links: Set Link To A Page, Web Address Or PDF File Using .NET

11/07/14 by David Zondray in C#
                    //Set Link Target to a Page in the Same Document

//The following code snippet shows you how to update a link in a PDF file and set its target to the second page of the document.

//C# Code Sample

// Load the PDF file
Document doc = new Document("Source.pdf");
// Get the first link annotation from first page of document
LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];
// Modification link: change link destination
GoToAction goToAction = (GoToAction)linkAnnot.Action;
// Specify the destination for link object
// The first parameter is document object, second is destination page number.
// The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
goToAction.Destination = new Aspose.Pdf.InteractiveFeatures.XYZExplicitDestination(doc,1, 1, 2, 2);
// Save the document with updated link
doc.Save("PDFLINK_Modified_output.pdf");

//VB.NET Code Sample

' Load the PDF file
Dim doc As Document = New Document("source.pdf")
' Get the first link annotation from first page of document
Dim linkAnnot As LinkAnnotation = CType(doc.Pages(1).Annotations(1), LinkAnnotation)
' Modification link: change link destination
Dim goToAction As Aspose.Pdf.InteractiveFeatures.GoToAction = CType(linkAnnot.Action, Aspose.Pdf.InteractiveFeatures.GoToAction)
' Specify the destination for link object
' The first parameter is document object, second is destination page number.
' The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
goToAction.Destination = New Aspose.Pdf.InteractiveFeatures.XYZExplicitDestination(doc, 1, 1, 2, 2)
' Save the document with updated link
doc.Save("PDFLINK_Modified_output.pdf") 

//Set Link Destination to a Web Address

//To update the hyperlink so that it points the web address, instantiate the GoToURIAction object and pass it to the LinkAnnotation's Action property. The following code snippet shows how to update a link in a PDF file and set its target to a web address.

//C# Code Sample

// Load the PDF file
Document doc = new Document("Source.pdf");
// Get the first link annotation from first page of document
LinkAnnotation linkAnnot = (LinkAnnotation)doc.Pages[1].Annotations[1];
// Modification link: change link action and set target as web address
linkAnnot.Action = new GoToURIAction("www.aspose.com");
// Save the document with updated link
doc.Save("PDFLINK_Modified_output.pdf");

//VB.NET Code Sample

' Load the PDF file
Dim doc As Document = New Document("source.pdf")
' Get the first link annotation from first page of document
Dim linkAnnot As LinkAnnotation = CType(doc.Pages(1).Annotations(1), LinkAnnotation)
' Modification link: change link action and set target as web address
linkAnnot.Action = New Aspose.Pdf.InteractiveFeatures.GoToURIAction("www.aspose.com")
' Save the document with updated link
doc.Save("PDFLINK_Modified_output.pdf")
 
//Set Link Target to Another PDF File

//The following code snippet shows how to update a link in a PDF file and set its target to another PDF file.

//C# Code Sample

Document document = new Document("c:/pdftest/This is a test Zoom Fit Page.pdf");
LinkAnnotation linkAnnot = (LinkAnnotation)document.Pages[1].Annotations[1];

GoToRemoteAction goToR = (GoToRemoteAction)linkAnnot.Action;
// Next line update destination, do not update file
goToR.Destination = new XYZExplicitDestination(document, 2, 0, 0, 1.5);
// Next line update file
goToR.File = new FileSpecification("c:/pdftest/PDFInput (1).pdf");

document.Save("c:/pdftest/ResultantFile.pdf");

//VB.NET Code Sample

Dim document As Document = New Document("c:/pdftest/This is a test Zoom Fit Page.pdf")
Dim linkAnnot As LinkAnnotation = TryCast(document.Pages(1).Annotations(1), LinkAnnotation)

Dim goToR As InteractiveFeatures.GoToRemoteAction = TryCast(linkAnnot.Action, InteractiveFeatures.GoToRemoteAction)
' Next line update destination, do not update file
goToR.Destination = New InteractiveFeatures.XYZExplicitDestination(document, 2, 0, 0, 1.5)
' Next line update file
goToR.File = New FileSpecification("c:/pdftest/PDFInput (1).pdf")

document.Save("c:/pdftest/ResultantFile.pdf")

//Update LinkAnnotation text color

//The Link annotation does not contain text. Text is placed in the contents of the page under the annotation. Therefore in order to change color of the text, we need to replace color of the page text instead of trying change color of the annotation. The following code snippet shows how to update the color of link annotation in PDF file.

//C# Code Sample

Document doc = new Document("AsposeCaseExample.pdf");
foreach (Annotation annotation in doc.Pages[1].Annotations)
{
    if (annotation is LinkAnnotation)
    {
        //search text under the annotation
        TextFragmentAbsorber ta = new TextFragmentAbsorber();
        Rectangle rect = annotation.Rect;
        rect.LLX -= 10;
        rect.LLY -= 10;
        rect.URX += 10;
        rect.URY += 10;
        ta.TextSearchOptions = new TextSearchOptions(rect);
        ta.Visit(doc.Pages[1]);
        //change color of the text.
        foreach (TextFragment tf in ta.TextFragments)
        {
            tf.TextState.ForegroundColor = Color.Red;
        }
    }
 
}
doc.Save("output.pdf");

//VB.NET Code Sample

Dim doc As Document = New Document("AsposeCaseExample.pdf")
For Each annotation As Aspose.Pdf.InteractiveFeatures.Annotations.Annotation In doc.Pages(1).Annotations

    If TypeOf annotation Is InteractiveFeatures.Annotations.LinkAnnotation Then

        'search text under the annotation
        Dim ta As Aspose.Pdf.Text.TextFragmentAbsorber = New Aspose.Pdf.Text.TextFragmentAbsorber()
        Dim rect As Aspose.Pdf.Rectangle = annotation.Rect
        rect.LLX -= 10
        rect.LLY -= 10
        rect.URX += 10
        rect.URY += 10
        ta.TextSearchOptions = New TextSearchOptions(rect)
        ta.Visit(doc.Pages(1))
        'change color of the text.
        For Each tf As Aspose.Pdf.Text.TextFragment In ta.TextFragments

            tf.TextState.ForegroundColor = Color.Red
        Next
    End If

Next
doc.Save("output.pdf")
                
0 Replies
  • regex
  • hashmap
  • pattern matching

Java Regex Matching Hashmap

11/07/14 by Sutanu Dalui in Java
                    import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.regex.Pattern;


public class RegexHashMap<V> implements Map<String, V>
{
	private class PatternMatcher
	{
		private final String regex;
		private final Pattern compiled;
		
		PatternMatcher(String name)
		{
			regex = name;
			compiled = Pattern.compile(regex);
			
		}
				
		boolean matched(String string)
		{
			if(compiled.matcher(string).matches())
			{
				ref.put(string, regex);
				return true;
			}
			return false;
		}
	}
	/**
	 * Map of input to pattern
	 */
	private final Map<String, String> ref;
	
	/**
	 * Map of pattern to value
	 */
	private final Map<String, V> map;
	
	/**
	 * Compiled patterns
	 */
	private final List<PatternMatcher> matchers;
	
	@Override
	public String toString()
	{
		return "RegexHashMap [ref=" + ref + ", map=" + map + "]";
	}

	/**
	 * 
	 */
	public RegexHashMap()
	{
		ref = new WeakHashMap<>();
		map = new HashMap<>();
		matchers = new ArrayList<>();
	}
			
	/**
	 * Returns the value to which the specified key pattern is mapped, or null if this map contains no mapping 
 		for the key pattern
	 */
	@Override
	public V get(Object weakKey)
	{
		if(!ref.containsKey(weakKey))
		{
			for(PatternMatcher matcher : matchers)
			{
				if(matcher.matched((String) weakKey))
				{
					break;
				}
			}
			
		}
		if(ref.containsKey(weakKey))
		{
			return map.get(ref.get(weakKey));
			
		}
		return null;
	}
	
	/**
	 * Associates a specified regular expression to a particular value
	 */
	@Override
	public V put(String key, V value)
	{
		V v = map.put(key, value);
		if (v == null)
		{
			matchers.add(new PatternMatcher(key));
		}
		return v;
		
	}

	/**
	 * Removes the regular expression key
	 */
	@Override
	public V remove(Object key) 
	{
		V v = map.remove(key);
		if(v != null)
		{
			for(Iterator<PatternMatcher> iter = matchers.iterator(); iter.hasNext();)
			{
				PatternMatcher matcher = iter.next();
				if(matcher.regex.equals(key))
				{
					iter.remove();
					break;
				}
			}
			
			for(Iterator<Entry<String, String>> iter = ref.entrySet().iterator(); iter.hasNext();)
			{
				Entry<String, String> entry = iter.next();
				if(entry.getValue().equals(key))
				{
					iter.remove();
				}
			}
		}
		return v;
        
    }
	/**
	 * Set of view on the regular expression keys
	 */
	@Override
	public Set<java.util.Map.Entry<String, V>> entrySet()
	{
		return map.entrySet();
	}

	@Override
	public void putAll(Map<? extends String, ? extends V> m) 
	{
		for(Entry<? extends String, ? extends V> entry : m.entrySet())
		{
			put(entry.getKey(), entry.getValue());
		}
    }

	@Override
	public int size()
	{
		return map.size();
	}

	@Override
	public boolean isEmpty()
	{
		return map.isEmpty();
	}

	/**
	 * Returns true if this map contains a mapping for the specified regular expression key.
	 */
	@Override
	public boolean containsKey(Object key)
	{
		return map.containsKey(key);
	}
	
	/**
	 * Returns true if this map contains a mapping for the specified regular expression matched pattern.
	 * @param key
	 * @return
	 */
	public boolean containsKeyPattern(Object key)
	{
		return ref.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value)
	{
		return map.containsValue(value);
	}

	@Override
	public void clear()
	{
		map.clear();
		matchers.clear();
		ref.clear();
	}
	
	/**
	 * Returns a Set view of the regular expression keys contained in this map.
	 */
	@Override
	public Set<String> keySet()
	{
		return map.keySet();
	}
	
	/**
	 * Returns a Set view of the regex matched patterns contained in this map. The set is backed by the map, so changes to 
 		the map are reflected in the set, and vice-versa.
	 * @return
	 */
	public Set<String> keySetPattern()
	{
		return ref.keySet();
	}

	@Override
	public Collection<V> values()
	{
		return map.values();
	}
	
	/**
	 * Produces a map of patterns to values, based on the regex put in this map
	 * @param patterns
	 * @return
	 */
	public Map<String, V> transform(List<String> patterns)
	{
		for(String pattern : patterns)
		{
			get(pattern);
		}
		
		Map<String, V> transformed = new HashMap<>();
		for(Entry<String, String> entry : ref.entrySet())
		{
			transformed.put(entry.getKey(), map.get(entry.getValue()));
		}
		return transformed;
		
	}
	public static void main(String...strings)
	{
		RegexHashMap<String> rh = new RegexHashMap<>();
		rh.put("[o|O][s|S][e|E].?[1|2]", "This is a regex


gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.