Vaadin: Opening a new window with PDF content placed in a String variable - string

I'd like to open a new window with PDF content that is placed within a String variable.
I already have a button with an event connected. In this event I want to call the new window.
The method looks like this:
private void show_archivobjekt(String data) {
String pdf = anfrage.get_archivobjectdata(data);
System.out.println(pdf); // This shows my PDF content in console and works!
// How to convert this String into a StreamSource
StreamResource streamResource = new StreamResource(pdfss, "test.pdf", myView);
streamResource.setCacheTime(5000);
streamResource.setMIMEType("application/pdf");
myView.getMainWindow().open(streamResource, "_blank");
}
myView is the Application.
How can I convert the String pdf to a StreamSource (pdfss)? Do I have to save it as file at first or is it possible to convert it to a StreamSource directly in memory?
The console output shows me the typically PDF content starting with %PDF-1.3 ... and so on.
Any help would be appreciated. Thanks in advance!
Rainer

The answer to this question is available through the official support forum here: https://vaadin.com/forum#!/thread/148544
Simply create your StreamResource like this, by using the byte representation of your string to create a ByteArrayInputStream as the source:
StreamResource streamResource = new StreamResource(
new StreamResource.StreamSource() {
public InputStream getStream() {
return new ByteArrayInputStream(pdf.getBytes());
}
}, "test.pdf");

Related

Need to dispaly svg on an image in Xamarin

I have an url that gets team logos but it returns svg https://www.mlbstatic.com/team-logos/141.svg.
How can i display this in a Image for xamarin forms?
Searched and only found complex huge amounts of code.
looking for
Download image -- I have this but what do i need to save it in GetResponsestream preferrable i would like to stay in memory and not write to disk or file.
Attach it to an image to display.
Thanks.
Ok, thought i would post my solution here.
I used SkiSharp:
SkiaSharp.Extended.Svg.SKSvg svg = new SkiaSharp.Extended.Svg.SKSvg();
using (WebClient client = new WebClient())
{
// ie for theurl: https://www.mlbstatic.com/team-logos/141.svg
svg.Load(new MemoryStream(client.DownloadData(new Uri(theurl))));
var bitmap = new SKBitmap((int)svg.CanvasSize.Width, (int)svg.CanvasSize.Height);
var canvas = new SKCanvas(bitmap);
canvas.DrawPicture(svg.Picture);
canvas.Flush();
canvas.Save();
string filename = "";
using (var image = SKImage.FromBitmap(bitmap))
using (var data = image.Encode(SKEncodedImageFormat.Png, 80))
{
// save the data to a stream
filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "temp.png");
using (var stream = File.OpenWrite(filename ))
{
data.SaveTo(stream);
}
}
}
use FileName from above to assign source to Xamarin image.
this accomplished the task with the least amount of code lines i tried.

SAP HYBRIS : how to use media to convert csv file to media hmc

I am a beginner in the Sap Hybris. I created a CronJob that works perfectly. which returns all the products with status approved and generated CSV file in local C://...
But I want to create or convert my CSV file to a media in HMC MEDIA? can someone help me?
I already have gone through Hybris wiki but I didn't understand.
Thank u for all !!
To achieve this, you only need to create your Media object, and attach your file to the created object, something like :
private MediaModel createMedia(final File file) throws MediaIOException, IllegalArgumentException, FileNotFoundException
{
final CatalogVersionModel catalogVersion = catalogVersionService.getCatalogVersion("MY_MEDIA_CATALOG", "VERSION");
MediaModel mediaModel;
try
{
mediaModel = mediaService.getMedia(catalogVersion, file.getName());
}
catch (final UnknownIdentifierException e)
{
mediaModel = modelService.create(MediaModel.class);
}
mediaModel.setCode(file.getName());
mediaModel.setCatalogVersion(catalogVersion);
mediaModel.setMime("text/csv");
mediaModel.setRealFileName(file.getName());
modelService.save(mediaModel);
mediaService.setStreamForMedia(mediaModel, new FileInputStream(file));
//Remove file
FileUtils.removeFile(file);
return mediaModel;
}

GET TEXT FROM IMAGE EMBEDDED IN A .docx FILE USING TIKA

I've been working on Text Extractor that works on .docx file using Tika. And it is working file for basic text and text in tables and textboxes, but it fails for images.
How do I get text from Image, tesseract along with tika can be used to get text from an image alone but for that I would need to extract out the image from document. How do I do this?
Kindly help if anybody has worked upon something like this.
This the code that works fine for text, textbox and tables,but not for images:
public class BasicDocumentExtractor {
public static void main(final String[] args) throws IOException,SAXException, TikaException {
//detecting the file type
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
FileInputStream inputstream=new FileInputStream(new File("D:\\Nidhi\\sw\\ws\\Hello.docx"));
ParseContext pcontext=new ParseContext();
//OOXml parser
OOXMLParser msofficeparser=new OOXMLParser ();
msofficeparser.parse(inputstream, handler,metadata,pcontext);
System.out.println("Contents of the document:" +handler.toString());
/*System.out.println("Metadata of the document:");
String[] metadataNames = metadata.names();
for(String name : metadataNames){
System.out.println(name + ": " + metadata.get(name));
}*/
}
}
You need to enable recursion in Tika in order to get the embedded images. The simplest way is normally just to use the RecursiveParserWrapper to do it for you.
If you use it, your code would instead be roughly
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
TikaInputStream input = TikaInputStream.get(new File("D:\\Nidhi\\sw\\ws\\Hello.docx"));
Parser wrapped = new AutoDetectParser();
RecursiveParserWrapper wrapper = new RecursiveParserWrapper(wrapped,
new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.TEXT, 60));
wrapper.parse(stream, handler, metadata, context);
// Get metadata from children
List<Metadata> list = wrapper.getMetadata();
// Get metadata from main document
System.out.println("Main doc name is " + metadata.get(TikaCoreProperties.TITLE));
System.out.println("Contents of the document:" +handler.String());
As I was trying really hard to do this since las 24hours, I figured out a way, a pretty easy one. Since, Tika is built on the top of POI, using POI this task can be efficiently executed. Also, it is not a direct solution so alomost no tutorials are available for this purpose, I hope nobody else has to face this issue in future. This is the running code that extracts all images from a .docx document:
public static void getImages() throws Exception {
XWPFDocument doc=new XWPFDocument(new FileInputStream("D:\\Nidhi\\CDAC\\Images\\test1.docx"));
List images=doc.getAllPictures();
int i =0;
while (i<images.size()) {
XWPFPictureData pic= (XWPFPictureData) images.get(i);
System.out.println(pic.getFileName() + " "+ pic.getPictureType() +" "+ pic.getData());
FileOutputStream fos=new FileOutputStream("D:\\Nidhi\\CDAC\\Images\\b" + i+".jpg");
fos.write(pic.getData());
i++;
}
}
Also, if it will work on all MS Office 2007+ files, for .doc and such files use HWPF in the exactly same manner.

Exception when open excel: File contains corrupted data

I am trying to read an excel with OpenXML.
What I did is simply as following:
private WorkbookPart wbPart = null;
private SpreadsheetDocument document = null;
public byte[] GetExcelReport()
{
byte[] original = File.ReadAllBytes(this.originalFilename);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(original, 0, original.Length);
using (SpreadsheetDocument excel = SpreadsheetDocument.Open(stream, true))
{
this.document = excel;
this.wbPart = document.WorkbookPart;
UpdateValue();
}
stream.Seek(0, SeekOrigin.Begin);
byte[] data = stream.ToArray();
return data;
}
}
I initialized this.originalFilename in the constructor. It is the filename ended with '.xlsx' which i created with excel 2010.
But this line of code
using (SpreadsheetDocument excel = SpreadsheetDocument.Open(stream, true))
gives the exception: Message: System.IO.FileFormatException: File contains corrupted data.
The StackTrace:
Does anyone know how to solve this problem? At the beginning, I didn't use the Stream, I just use SpreadsheetDocument.Open(filename, true). However, it turns out to be exactly the same exception.
I've tried to create a new .xlsx file, but it's still the same.
There is a MSDN page which describes the process of reading and writing Excel file using stream and open xml SDK.
http://msdn.microsoft.com/en-us/library/office/ff478410.aspx
Try extracting the document contents through zip application and check whether you are getting the standard folders inside like xl,docProps and _rels etc.,
This is a method to find whether the package is properly packaged as archive or not.
Hope this helps.

Modify SharePoint Master Page text using C#

I have a SharePoint Event Receiver for a feature that I am writing. On the FeatureActivated method, I want to add a control to the SharePoint Master Pages. In order to do this, I have elected to inject some text into the master page to add the control by opening the the file with a StreamReader, reading the stream to text, inserting the text, then saving the file back with a SharePoint Object model.
I'm having an issue with the StreamReader. When it reads the stream to text, all I get back are a bunch of question marks.
Here is the code:
foreach(SPFile file in files)
{
switch(new FileInfo(file.Name).Extension)
{
case ".master":
string masterString = string.Empty;
using(StreamReader reader = new StreamReader(file.OpenBinaryStream()))
{
masterString = reader.ReadToEnd();
}
//
// Inject the text I want into the masterString
// Convert masterString into byte []
// Save the byte [] back to SharePoint, overwriting the master
// page
//
break;
}
}
The masterString always comes back as ???????????????????????....
I need it to come back as plain text.
Any ideas?
Experiment with different encodings (UTF-8, UTF-16, etc.)
public StreamReader(
string path,
Encoding encoding)

Resources