I'm currently searching for a library or a way to convert HTML OR DOCX files into PDF on the phone/tab, primarily I'am searching for a way on Android or iOS idk if its a PCL or platform specific approach. I could do this for every Platform independently, because our app requires iOS 8 or android kitkat, both supporting native PDF conversion but i want to do it seamless for the user, so the question is, if anyone has done this before, without loading it into a visible Webview at first or has knowledge of an open not GPL licensed API(can't publish the code), to do this with Xamarin.
I am aware of the possibility to do this online, but I don't want to to be dependent to a online service for this.
Help and ideas are appreciated.
Android Solution:
Call the SafeHTMLToPDF(string html, string filename) via a dependency service like
DependencyService.Get<YOURINTERFACE>().SafeHTMLToPDF(htmlString, "Invoice");
public string SafeHTMLToPDF(string html, string filename)
{
var dir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/pay&go/");
var file = new Java.IO.File(dir + "/" + filename + ".pdf");
if (!dir.Exists())
dir.Mkdirs();
int x = 0;
while (file.Exists())
{
x++;
file= new Java.IO.File(dir + "/" + filename + "( " + x + " )" + ".pdf");
}
if (webpage == null)
webpage = new Android.Webkit.WebView(GetApplicationContext());
int width = 2102;
int height = 2973;
webpage.Layout(0, 0, width, height);
webpage.LoadDataWithBaseURL("",html, "text/html", "UTF-8" , null);
webpage.SetWebViewClient(new WebViewCallBack(file.ToString()));
return file.ToString();
}
class WebViewCallBack : WebViewClient
{
string fileNameWithPath = null;
public WebViewCallBack(string path)
{
this.fileNameWithPath = path;
}
public override void OnPageFinished(Android.Webkit.WebView myWebview, string url)
{
PdfDocument document = new PdfDocument();
PdfDocument.Page page = document.StartPage(new PdfDocument.PageInfo.Builder(2120 ,3000, 1).Create());
myWebview.Draw(page.Canvas);
document.FinishPage(page);
Stream filestream = new MemoryStream();
FileOutputStream fos = new Java.IO.FileOutputStream(fileNameWithPath, false); ;
try
{
document.WriteTo(filestream);
fos.Write(((MemoryStream)filestream).ToArray(), 0, (int)filestream.Length);
fos.Close();
}
catch
{
}
}
}
And the Way to do it under iOS
public string SafeHTMLToPDF(string html, string filename)
{
UIWebView webView = new UIWebView(new CGRect(0, 0, 6.5 * 72, 9 * 72));
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var file = Path.Combine(documents, "Invoice" + "_" + DateTime.Now.ToShortDateString() + "_" + DateTime.Now.ToShortTimeString() + ".pdf");
webView.Delegate = new WebViewCallBack(file);
webView.ScalesPageToFit = true;
webView.UserInteractionEnabled = false;
webView.BackgroundColor = UIColor.White;
webView.LoadHtmlString(html, null);
return file;
}
class WebViewCallBack : UIWebViewDelegate
{
string filename = null;
public WebViewCallBack(string path)
{
this.filename = path;
}
public override void LoadingFinished(UIWebView webView)
{
double height, width;
int header, sidespace;
width = 595.2;
height = 841.8;
header = 10;
sidespace = 10;
UIEdgeInsets pageMargins = new UIEdgeInsets(header, sidespace, header, sidespace);
webView.ViewPrintFormatter.ContentInsets = pageMargins;
UIPrintPageRenderer renderer = new UIPrintPageRenderer();
renderer.AddPrintFormatter(webView.ViewPrintFormatter, 0);
CGSize pageSize = new CGSize(width, height);
CGRect printableRect = new CGRect(sidespace,
header,
pageSize.Width - (sidespace * 2),
pageSize.Height - (header * 2));
CGRect paperRect = new CGRect(0, 0, width, height);
renderer.SetValueForKey(NSValue.FromObject(paperRect), (NSString)"paperRect");
renderer.SetValueForKey(NSValue.FromObject(printableRect), (NSString)"printableRect");
NSData file = PrintToPDFWithRenderer(renderer, paperRect);
File.WriteAllBytes(filename, file.ToArray());
}
private NSData PrintToPDFWithRenderer(UIPrintPageRenderer renderer, CGRect paperRect)
{
NSMutableData pdfData = new NSMutableData();
UIGraphics.BeginPDFContext(pdfData, paperRect, null);
renderer.PrepareForDrawingPages(new NSRange(0, renderer.NumberOfPages));
CGRect bounds = UIGraphics.PDFContextBounds;
for (int i = 0; i < renderer.NumberOfPages; i++)
{
UIGraphics.BeginPDFPage();
renderer.DrawPage(i, paperRect);
}
UIGraphics.EndPDFContent();
return pdfData;
}
}
Frustrated with the existing solutions, I've built some extension methods (OpenSource, MIT Licensed) that convert HTML or the content of a Xamarin.Forms.WebView to a PDF file. Sample usage for WebView to PDF:
async void ShareButton_Clicked(object sender, EventArgs e)
{
if (Forms9Patch.ToPdfService.IsAvailable)
{
if (await webView.ToPdfAsync("output.pdf") is ToFileResult pdfResult)
{
if (pdfResult.IsError)
using (Toast.Create("PDF Failure", pdfResult.Result)) { }
else
{
var collection = new Forms9Patch.MimeItemCollection();
collection.AddBytesFromFile("application/pdf", pdfResult.Result);
Forms9Patch.Sharing.Share(collection, shareButton);
}
}
}
else
using (Toast.Create(null, "PDF Export is not available on this device")) { }
}
}
For a more complete explanation of how to use it, here's a short article: https://medium.com/#ben_12456/share-xamarin-forms-webview-as-a-pdf-a877542e824a?
You can able to convert the HTML to PDF file without any third party library. I am sharing my git repo for future reference.
https://github.com/dinesh4official/XFPDF
Yes, you can convert a Word document to PDF in Xamarin with a few lines of code easily. You need to refer Syncfusion.Xamarin.DocIORenderer from nuget.org
Assembly assembly = typeof(App).GetTypeInfo().Assembly;
// Retrieves the document stream from embedded Word document
Stream inputStream = assembly.GetManifestResourceStream("WordToPDF.Assets.GettingStarted.docx");
string fileName = "GettingStarted.pdf";
// Creates new instance of WordDocument
WordDocument wordDocument = new WordDocument(inputStream,Syncfusion.DocIO.FormatType.Automatic);
inputStream.Dispose();
// Creates new instance of DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
// Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
// Releases all resources used by the DocIORenderer and WordDocument instance
render.Dispose();
document.Close();
// Saves the converted PDF file
MemoryStream outputStream = new MemoryStream();
pdfDocument.Save(outputStream);
// Releases all resources used by the PdfDocument instance
pdfDocument.Close();
To know more about this, kindly refer here.
Related
In AutoCAD Design Automation API - How would one go about changing the Options --> Files (tab) --> Printer Support File Path --> Plot Style Table Search Path to point to another location during a work item execution? I have tried adjusting the RuntimeRequirements in the package contents xml file to point to my bundle contents, but that did not work.
<Components>
<RuntimeRequirements
OS="Win64"
Platform="AutoCAD"
SupportPath="./Contents/"/>
............
Any Suggestions? Ultimately im trying to upload a specific custom ctb file and plot with it
Thanks,
John
You need to program to copy the related files from bundle to Plotter folders in your custom plugin dll.
Suppose you placed all the related files like .pmp,.pc3 and .ctb in Contents folder, as shown in pic
Example:
Then you need to run command, prior to running any PLOT commands. This will copy files to relevant folders when your execute workItem against the bundle package you have uploaded to DA service.
[CommandMethod("PlotLayoutCommands", "COPYPLOTTERFILES", CommandFlags.Modal)]
public static void CopyPlotterFiles()
{
// Get the current document and database, and start a transaction
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//A way to copy files from bundle package to respective Printer Support Path
object roamablePath = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("ROAMABLEROOTPREFIX");
acDoc.Editor.WriteMessage("\nRoamable-Path\t:{0}\n", roamablePath.ToString());
string pc3FileFound = HostApplicationServices.Current.FindFile("DAS-PDF.pc3", acCurDb, FindFileHint.Default);
string pmpFileFound = HostApplicationServices.Current.FindFile("DAS-PDF.pmp", acCurDb, FindFileHint.Default);
string plotStyleFileFound = HostApplicationServices.Current.FindFile("DAS-PDF.ctb", acCurDb, FindFileHint.Default);
string scriptFileFound = HostApplicationServices.Current.FindFile("RunPlot.scr", acCurDb, FindFileHint.Default);
acDoc.Editor.WriteMessage("\nPC3 Config\t:{0}\n", pc3FileFound);
acDoc.Editor.WriteMessage("\nPMP File\t:{0}\n", pmpFileFound);
acDoc.Editor.WriteMessage("\nCTB File\t:{0}\n", plotStyleFileFound);
acDoc.Editor.WriteMessage("\nScript File\t:{0}\n", scriptFileFound);
//Start Copying:
string pmpFolder = "PMP Files";
string pc3Folder = "Plotters";
string plotStylesFolder = "Plot Styles";
try
{
File.Copy(pc3FileFound,
Path.Combine(roamablePath.ToString(),
pc3Folder, Path.GetFileName(pc3FileFound)));
File.Copy(pmpFileFound,
Path.Combine(roamablePath.ToString(),
pc3Folder, pmpFolder, Path.GetFileName(pmpFileFound)));
File.Copy(plotStyleFileFound, Path.Combine(roamablePath.ToString(),
pc3Folder, plotStylesFolder, Path.GetFileName(plotStyleFileFound)));
File.Copy(scriptFileFound, Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(plotStyleFileFound)));
}
catch (System.Exception ex)
{
acDoc.Editor.WriteMessage("\n" + ex.Message);
}
}
or
You can create CustomPlot command something like this, which will set PC3 and CTB on the Layout.
public static void PlotLayout()
{
// Get the current document and database, and start a transaction
Document acDoc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//A way to copy files from bundle package to respective Printer Support Path
object roamablePath = Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("ROAMABLEROOTPREFIX");
acDoc.Editor.WriteMessage("\nRoamable-Path\t:{0}\n", roamablePath.ToString());
string pc3FileFound = HostApplicationServices.Current.FindFile("DAS-PDF.pc3", acCurDb, FindFileHint.Default);
string pmpFileFound = HostApplicationServices.Current.FindFile("DAS-PDF.pmp", acCurDb, FindFileHint.Default);
string plotStyleFileFound = HostApplicationServices.Current.FindFile("DAS-PDF.ctb", acCurDb, FindFileHint.Default);
acDoc.Editor.WriteMessage("\nPC3 Config\t:{0}\n", pc3FileFound);
acDoc.Editor.WriteMessage("\nPMP File\t:{0}\n", pmpFileFound);
acDoc.Editor.WriteMessage("\nCTB File\t:{0}\n", plotStyleFileFound);
//Start Copying:
string pmpFolder = "PMP Files";
string pc3Folder = "Plotters";
string plotStylesFolder = "Plot Styles";
try
{
File.Copy(pc3FileFound,
Path.Combine(roamablePath.ToString(),
pc3Folder, Path.GetFileName(pc3FileFound)),true);
File.Copy(pmpFileFound,
Path.Combine(roamablePath.ToString(),
pc3Folder, pmpFolder, Path.GetFileName(pmpFileFound)), true);
File.Copy(plotStyleFileFound, Path.Combine(roamablePath.ToString(),
pc3Folder, plotStylesFolder, Path.GetFileName(plotStyleFileFound)), true);
}
catch (System.Exception ex)
{
acDoc.Editor.WriteMessage("\n" + ex.Message);
}
// these prompts are only seen when running in interactive mode in autocad
// when they are run from the activity, they are passed in as part of the script (like parameters)
PromptResult result = acDoc.Editor.GetString("Enter PC3 File:");
if (result.Status != PromptStatus.OK) return;
string pc3FileName = result.StringResult;
result = acDoc.Editor.GetString("Enter CTB File:");
if (result.Status != PromptStatus.OK) return;
string ctbFileName = result.StringResult;
result = acDoc.Editor.GetString("Enter PDF Name :");
if (result.Status != PromptStatus.OK) return;
string pdfFileName = result.StringResult;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Reference the Layout Manager
LayoutManager acLayoutMgr = LayoutManager.Current;
// Get the current layout and output its name in the Command Line window
Layout acLayout = acTrans.GetObject(acLayoutMgr.GetLayoutId(acLayoutMgr.CurrentLayout),
OpenMode.ForRead) as Layout;
using (OpenCloseTransaction oct = new OpenCloseTransaction())
{
using (PlotSettingsValidator plotSetVal = PlotSettingsValidator.Current)
{
acDoc.Editor.WriteMessage($"\nStyle sheet of current layout:{acLayout.CurrentStyleSheet}");
if (!acLayout.IsWriteEnabled)
{
acLayout.UpgradeOpen();
}
plotSetVal.RefreshLists(acLayout);
if (plotSetVal.GetPlotStyleSheetList().Contains(ctbFileName))
{
if (acCurDb.PlotStyleMode)
{
acDoc.Editor.WriteMessage($"\nThe plot style sheet is being set to {ctbFileName}");
plotSetVal.SetCurrentStyleSheet(acLayout, ctbFileName);
}
else
{
acDoc.Editor.WriteMessage("\nUnable to set plot style in drawing using stb\n\n");
}
}
}
oct.Commit();
}
PlotConfig acPlCfg = PlotConfigManager.SetCurrentConfig(pc3FileName);
string mediaName = acPlCfg.Comment;
// Get the PlotInfo from the layout
using (PlotInfo acPlInfo = new PlotInfo())
{
acPlInfo.Layout = acLayout.ObjectId;
// Get a copy of the PlotSettings from the layout
using (PlotSettings acPlSet = new PlotSettings(acLayout.ModelType))
{
acPlSet.CopyFrom(acLayout);
// Update the PlotSettings object
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;
// Set the plot type
acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
// Set the plot scale
acPlSetVdr.SetUseStandardScale(acPlSet, true);
acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);
// Center the plot
acPlSetVdr.SetPlotCentered(acPlSet, true);
// Set the plot device to use
acPlSetVdr.SetPlotConfigurationName(acPlSet, Path.GetFileName(pc3FileName), acPlSet.CanonicalMediaName);
// Set the plot info as an override since it will
// not be saved back to the layout
acPlInfo.OverrideSettings = acPlSet;
// Validate the plot info
using (PlotInfoValidator acPlInfoVdr = new PlotInfoValidator())
{
acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
acPlInfoVdr.Validate(acPlInfo);
// Check to see if a plot is already in progress
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
{
// Track the plot progress with a Progress dialog
using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false, 1, true))
{
using ((acPlProgDlg))
{
// Define the status messages to display
// when plotting starts
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
// Set the plot progress range
acPlProgDlg.LowerPlotProgressRange = 0;
acPlProgDlg.UpperPlotProgressRange = 100;
acPlProgDlg.PlotProgressPos = 0;
// Display the Progress dialog
acPlProgDlg.OnBeginPlot();
acPlProgDlg.IsVisible = true;
// Start to plot the layout
acPlEng.BeginPlot(acPlProgDlg, null);
// Define the plot output
acPlEng.BeginDocument(acPlInfo, acDoc.Name, null, 1, true, pdfFileName);
// Display information about the current plot
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDoc.Name + " - " + acLayout.LayoutName);
// Set the sheet progress range
acPlProgDlg.OnBeginSheet();
acPlProgDlg.LowerSheetProgressRange = 0;
acPlProgDlg.UpperSheetProgressRange = 100;
acPlProgDlg.SheetProgressPos = 0;
// Plot the first sheet/layout
using (PlotPageInfo acPlPageInfo = new PlotPageInfo())
{
acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
}
acPlEng.BeginGenerateGraphics(null);
acPlEng.EndGenerateGraphics(null);
// Finish plotting the sheet/layout
acPlEng.EndPage(null);
acPlProgDlg.SheetProgressPos = 100;
acPlProgDlg.OnEndSheet();
// Finish plotting the document
acPlEng.EndDocument(null);
// Finish the plot
acPlProgDlg.PlotProgressPos = 100;
acPlProgDlg.OnEndPlot();
acPlEng.EndPlot(null);
}
}
}
}
}
}
}
}
}
Your Activity using C# .NET core.
private async Task<string> SetupActivityAsync(string myApp)
{
Console.WriteLine("Setting up activity...");
var myActivity = $"{Owner}.{ActivityName}+{Label}";
var actResponse = await this.api.ActivitiesApi.GetActivityAsync(myActivity, throwOnError: false);
var activity = new Activity()
{
Appbundles = new List<string>()
{
myApp
},
CommandLine = new List<string>()
{
$"$(engine.path)\\accoreconsole.exe /i $(args[inputFile].path) /al $(appbundles[{PackageName}].path) /s $(settings[script].path)"
},
Engine = TargetEngine,
Settings = new Dictionary<string, ISetting>()
{
{ "script", new StringSetting() { Value = "CustomPlot\nDAS-PDF.pc3\nDAS-PDF.ctb\nresult.pdf\n" } }
},
Parameters = new Dictionary<string, Parameter>()
{
{ "inputFile", new Parameter() { Verb= Verb.Get, LocalName = "$(HostDwg)", Required = true } },
{ "outputFile", new Parameter() { Verb= Verb.Put, LocalName = "result.pdf", Required= true} }
},
Id = ActivityName
};
if (actResponse.HttpResponse.StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine($"Creating activity {myActivity}...");
await api.CreateActivityAsync(activity, Label);
return myActivity;
}
await actResponse.HttpResponse.EnsureSuccessStatusCodeAsync();
Console.WriteLine("\tFound existing activity...");
if (!Equals(activity, actResponse.Content))
{
Console.WriteLine($"\tUpdating activity {myActivity}...");
await api.UpdateActivityAsync(activity, Label);
}
return myActivity;
}
And, Workitem:
var workItemStatus = await api.CreateWorkItemAsync(new Autodesk.Forge.DesignAutomation.Model.WorkItem()
{
ActivityId = myActivity,
Arguments = new Dictionary<string, IArgument>() {
{
"inputFile",
new XrefTreeArgument() {
Url = DownloadUrl,
Verb = Verb.Get
}
}, {
"outputFile",
new XrefTreeArgument() {
Verb = Verb.Put, Url = UploadUrl
}
}
}
});
I would like to create dynamic PDF documents using HTML and dynamic images. My code works fine with standard HTML and full paths for the images, but when I try to embed the image inline in the document I get the error
Exception Details: System.IO.IOException: The document has no pages.
Is there a way to embed the images without an HTTP call per image? I don't want that because I think it will cause scalability issues and the images are sensitive.
Here is my code that gives the IOException:
public ActionResult MakePdf()
{
string html = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<!DOCTYPE html
PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN""
""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"">
<head>
<title>Minimal XHTML 1.0 Document with W3C DTD</title>
</head>
<body><img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAABQCAMAAAB24TZcAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGAUExURdSmeJp2SHlbQIRoSUg2J499a8KebqeHZuGufBEVJPz7+3NWPVxGMduwhPXEktnX1mtROLq7t5WDc2VMNv3LmKB8TMSidMbFxLGlmXlhSMSddpJUL+y8i3VlVqedlOzr6gUIF2lXRLCLY4ZyXLyYaYhtUYiJhJFyU1dBLLiVZnlwZrWRY/Hx8b+2rbySaJh9Yqeoo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/vC+gB+EBe9Xm/8KEQs67hShMmgIRsNylFuFe9UL1IGHXHNAtr77ZYN7htNB8LxJmCnyaBZULpJ6/g4ZZQCX83FAS1u3675xnTaX/GKFdLl+gIaDZeFpU78rS9oDnzZEmHstqPJKc9n90LJPThyBUZIVRtMv8Q1v9Xx8bzxigddWo1t7yZ//zgSCwRiK6CO0PUD2OR4hMnhHfiPtYiJr4a8Jj4MbHNe7UC4RtTfc5wsd+DD6RbxxTZ8chtkrcJGIlqX41GqTVzFp3wmfmCNi5rNT74Z3nwHi2BjZW11AtdzgvxIfSBl4l/Klzr+bfLvzSNYA1u9xTfmz8f4lLmA5HWfgV8eTa7BEohxox1xeZ1F5Ef4fTrYnL4oGjb7QZ3JVgk2W4KJPMZvmWbo9KWJ27QsXKHm3DkhJT/Gs6z55lo0abV5wCSL5txL/CMa4PYPUXN+5qwTj68aXwa5MP4Efj/VDA4TW3BV3PQMp7Wlgnfg555mcPFO8RbXMbXv8Oh6pG3J7IRM8bq3Q/zKLFqUQ3GteNYvbepG1XG57O0Qt9Hmd1bOKC1qbZH/zbK78FWzYMJ2aZoXPq7kr8ZvORr+iUSjJzQb/Gpa5l8BBgBZTppAyfsf0wAAAABJRU5ErkJggg==' width='62' height='80' style='float: left; margin-right: 28px;' /></body></html>";
var bytes = Encoding.UTF8.GetBytes(html);
using (MemoryStream input = new MemoryStream(bytes))
{
MemoryStream output = new MemoryStream();
using (Document document = new Document(PageSize.LETTER, 50, 50, 50, 50))
{
using (PdfWriter writer = PdfWriter.GetInstance(document, output))
{
writer.CloseStream = false;
document.Open();
XMLWorkerHelper xmlWorker = XMLWorkerHelper.GetInstance();
xmlWorker.ParseXHtml(writer, document, input, null);
document.Close();
output.Position = 0;
return new FileStreamResult(output, "application/pdf");
}
}
}
}
We need to write our own ImageTagProcessor to support processing of base 64 images:
public class CustomImageTagProcessor : iTextSharp.tool.xml.html.Image
{
public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent)
{
IDictionary<string, string> attributes = tag.Attributes;
string src;
if (!attributes.TryGetValue(HTML.Attribute.SRC, out src))
return new List<IElement>(1);
if (string.IsNullOrEmpty(src))
return new List<IElement>(1);
if (src.StartsWith("data:image/", StringComparison.InvariantCultureIgnoreCase))
{
// data:[<MIME-type>][;charset=<encoding>][;base64],<data>
var base64Data = src.Substring(src.IndexOf(",") + 1);
var imagedata = Convert.FromBase64String(base64Data);
var image = iTextSharp.text.Image.GetInstance(imagedata);
var list = new List<IElement>();
var htmlPipelineContext = GetHtmlPipelineContext(ctx);
list.Add(GetCssAppliers().Apply(new Chunk((iTextSharp.text.Image)GetCssAppliers().Apply(image, tag, htmlPipelineContext), 0, 0, true), tag, htmlPipelineContext));
return list;
}
else
{
return base.End(ctx, tag, currentContent);
}
}
}
Then we can inject this new processor into the HtmlPipelineContext:
using (var doc = new Document(PageSize.A4))
{
var writer = PdfWriter.GetInstance(doc, new FileStream("test.pdf", FileMode.Create));
doc.Open();
var html = #"<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAD4AAABQCAMAAAB24TZcAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAGAUExURdSmeJp2SHlbQIRoSUg2J499a8KebqeHZuGufBEVJPz7+3NWPVxGMduwhPXEktnX1mtROLq7t5WDc2VMNv3LmKB8TMSidMbFxLGlmXlhSMSddpJUL+y8i3VlVqedlOzr6gUIF2lXRLCLY4ZyXLyYaYhtUYiJhJFyU1dBLLiVZnlwZrWRY/Hx8b+2rbySaJh9YqeooDw4NygnKvvJlpyblzksIUhGRryYckc7MPjGlKODX5x8VVA8K+azgM3FvDInHK2JW2ZbUOHh4Xt2cFpaWKeAUM6kel1RRJmUjo5vSrWzrJJ1WFhLQCQmMuK1iJiMgmthWPPCkOm3hEtBOunm5LCNXnJtZquEXmNkYvG+i7Ctq+y5hrWRbKqSeaN/WqmFVYFgQh8aGOa4isWkd8mcby4vONDNy0AwI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4MbHNe7UC4RtTfc5wsd+DD6RbxxTZ8chtkrcJGIlqX41GqTVzFp3wmfmCNi5rNT74Z3nwHi2BjZW11AtdzgvxIfSBl4l/Klzr+bfLvzSNYA1u9xTfmz8f4lLmA5HWfgV8eTa7BEohxox1xeZ1F5Ef4fTrYnL4oGjb7QZ3JVgk2W4KJPMZvmWbo9KWJ27QsXKHm3DkhJT/Gs6z55lo0abV5wCSL5txL/CMa4PYPUXN+5qwTj68aXwa5MP4Efj/VDA4TW3BV3PQMp7Wlgnfg555mcPFO8RbXMbXv8Oh6pG3J7IRM8bq3Q/zKLFqUQ3GteNYvbepG1XG57O0Qt9Hmd1bOKC1qbZH/zbK78FWzYMJ2aZoXPq7kr8ZvORr+iUSjJzQb/Gpa5l8BBgBZTppAyfsf0wAAAABJRU5ErkJggg==' width='62' height='80' style='float: left; margin-right: 28px;' />";
var tagProcessors = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory();
tagProcessors.RemoveProcessor(HTML.Tag.IMG); // remove the default processor
tagProcessors.AddProcessor(HTML.Tag.IMG, new CustomImageTagProcessor()); // use our new processor
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
var cssResolver = new StyleAttrCSSResolver(cssFiles);
cssResolver.AddCss(#"code { padding: 2px 4px; }", "utf-8", true);
var charset = Encoding.UTF8;
var hpc = new HtmlPipelineContext(new CssAppliersImpl(new XMLWorkerFontProvider()));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors); // inject the tagProcessors
var htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer));
var pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
var worker = new XMLWorker(pipeline, true);
var xmlParser = new XMLParser(true, worker, charset);
xmlParser.Parse(new StringReader(html));
}
Process.Start("test.pdf");
string originalFile = "Original1.pdf";
string copyOfOriginal = "Re-copia.pdf";
byte[] bytes = Convert.FromBase64String(archivo);
System.IO.FileStream stream = new FileStream(originalFile, FileMode.CreateNew);
System.IO.BinaryWriter writer = new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();
PdfReader reader1 = new PdfReader(originalFile);
using (FileStream fs = new FileStream(copyOfOriginal, FileMode.Create, FileAccess.Write, FileShare.None))
// Creating iTextSharp.text.pdf.PdfStamper object to write
// Data from iTextSharp.text.pdf.PdfReader object to FileStream object
using (PdfStamper stamper = new PdfStamper(reader1, fs))
{
int pageCount = reader1.NumberOfPages;
// Create New Layer for Watermark
PdfLayer layer = new PdfLayer("WatermarkLayer", stamper.Writer);
// Loop through each Page
for (int i = pageCount; i <= pageCount; i++)
{
// Getting the Page Size
Rectangle rect = reader1.GetPageSize(i);
// Get the ContentByte object
PdfContentByte cb = stamper.GetUnderContent(i);
// Tell the cb that the next commands should be "bound" to this new layer
cb.BeginLayer(layer);
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
PdfGState gState = new PdfGState();
cb.SetGState(gState);
string codbartest = codBarras;
BarcodePDF417 bcpdf417 = new BarcodePDF417();
//Asigna el código de barras en base64 a la propiedad text del objeto..
bcpdf417.Text = ASCIIEncoding.ASCII.GetBytes(codbartest);
Image imgpdf417 = bcpdf417.GetImage();
imgpdf417.SetAbsolutePosition(50, 50);
imgpdf417.ScalePercent(100);
cb.AddImage(imgpdf417);
// Close the layer
cb.EndLayer();
}[enter image description here][1]
I have mp4+vtt subtitle video player project on Wp8 c#. I'm look at Microsoft.PlayerFramework.MediaPlayer and WebVTTPlugin, its works perfect:
https://playerframework.codeplex.com/wikipage?title=Closed%20Captions%3a%20WebVTT
im use this code and works perfect. But i have bad luck my project .vtt caption files have "," decimal seperator, this mean my app chrash i need to download subtitle and replace all "," to "." and save isolated storage, i handle it but i cant set caption source to isolated storage because isolated storage is not have uri. I know i cant tell well, i tell it with example:
my caption:
(http://dizilab.com/captions/chuck/sezon-1/tr/2.vtt?v=5.2)
1
00:00:06,600 --> 00:00:10,900
Merhaba. Benim adım Charles Bartowski,
ama bana Chuck diyebilirsiniz.
2
00:00:11,323 --> 00:00:12,711
Bunlar benim ayakkabılarım.
3
00:00:12,771 --> 00:00:14,209
Bu da benim hayatım.
4
00:00:14,249 --> 00:00:19,042
Casuslar, araba takipleri, bilgisayar
çalan ninjalar ve günü kurtaran ben.
its true caption is:
WEBVTT FILE
1
00:00:06.600 --> 00:00:10.900 Merhaba. Benim adım
Charles Bartowski, ama bana Chuck diyebilirsiniz.
2 00:00:11.323 --> 00:00:12.711 Bunlar benim ayakkabılarım.
3 00:00:12.771 --> 00:00:14.209 Bu da benim hayatım.
4 00:00:14.249 --> 00:00:19.042 Casuslar, araba takipleri, bilgisayar
çalan ninjalar ve günü kurtaran ben.
and it is code:
using Microsoft.PlayerFramework.WebVTT;
using System.IO.IsolatedStorage;
using System.IO;
using System.Threading.Tasks;
using Microsoft.PlayerFramework;
namespace PanoramaApp1
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public string alinanveri="";
public MainPage()
{
InitializeComponent();
Microsoft.PlayerFramework.MediaPlayer player =new Microsoft.PlayerFramework.MediaPlayer();
Microsoft.PlayerFramework.WebVTT.WebVTTPlugin webvttPlugin = new WebVTTPlugin();
Microsoft.PlayerFramework.Caption caption = new Microsoft.PlayerFramework.Caption();
player.IsCaptionSelectionVisible = true;
player.Plugins.Add(webvttPlugin);
altyazikaydet("http://dizilab.com/captions/chuck/sezon-1/tr/2.vtt?v=5.2");
IsolatedStorageFile kayitliDepo = IsolatedStorageFile.GetUserStoreForApplication();
var okuyucu = new StreamReader(new IsolatedStorageFileStream("altyazi.vtt", FileMode.Open, kayitliDepo));
caption.Source = new Uri("i cant use here for access isostorage"); // url points to sample.vtt file
caption.Description = "Türkçe";
player.AvailableCaptions.Add(caption);
player.SelectedCaption = player.AvailableCaptions.FirstOrDefault();
LayoutRoot.Children.Add(player);
player.Source = new Uri("https://redirector.googlevideo.com/videoplayback?requiressl=yes&shardbypass=yes&cmbypass=yes&id=eafe5f42d368b2e0&itag=18&source=picasa&cmo=secure_transport%3Dyes&ip=0.0.0.0&ipbits=0&expire=1420976098&sparams=requiressl,shardbypass,cmbypass,id,itag,source,ip,ipbits,expire&signature=6E257266C2AAADDFC3260B0AADE603F7E421E130.A933FF8365247DEC72A34B71B02FA3B13C57F291&key=lh1", UriKind.RelativeOrAbsolute); // url points to sample.mp4 fil
}
private async void altyazikaydet(string altyaziurl)
{
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(altyaziurl);
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
using (var responseStream = response.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
alinanveri = await sr.ReadToEndAsync();
}
}
}
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter yazici = new StreamWriter(new IsolatedStorageFileStream("altyazi.vtt", FileMode.Create, file));
string x=alinanveri;
x=x.Replace(",0",".0");
x=x.Replace(",1",".1");
x=x.Replace(",2",".2");
x=x.Replace(",3",".3");
x=x.Replace(",4",".4");
x=x.Replace(",5",".5");
x=x.Replace(",6",".6");
x=x.Replace(",7",".7");
x=x.Replace(",8",".8");
x=x.Replace(",9",".9");
x = "WEBVTT FILE" + Environment.NewLine + x;
yazici.WriteLine(x);
yazici.Close();
IsolatedStorageFile kayitliDepo = IsolatedStorageFile.GetUserStoreForApplication();
StreamReader okuyucu = new StreamReader(new IsolatedStorageFileStream("altyazi.vtt", FileMode.Open, kayitliDepo));
string line;
while ((line = okuyucu.ReadLine()) != null)
{
MessageBox.Show(line);
}
}
catch
{
}
}
I had the same problem in Windows UWP.
The player.Source allows an absolute local URI but the webvtt plugin does not.
Finally, I found out how to make it work:
using System.IO;
var caption = new Caption
{
Description = "whatever",
Source = new Uri(Path.Combine("ms-appdata:///Local/", "test.vtt"))
};
Player.AvailableCaptions.Add(caption);
Player.SelectedCaption = Player.AvailableCaptions.FirstOrDefault();
Not sure if this work, but from this article , you could try this
string fullpath = "";
using (IsolatedStorageFile kayitliDepo = IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream stream = kayitliDepo.OpenFile("altyazi.vtt", FileMode.Open,FileAccess.Read);
fullpath = stream.Name;
}
caption.Source = new Uri(Name,Urikind.Absolute); // url points to sample.vtt file
caption.Description = "Türkçe";
player.AvailableCaptions.Add(caption);
player.SelectedCaption = player.AvailableCaptions.FirstOrDefault();
I am making a game on cocos2d-JS for facebook in which there is a requirement of sharing a screenshot of the game.
I am able to take the screenshot but now I am unable to upload it in the Parse.com server because it requires base64 format or byte array. I am unable to find any solution of converting Sprite in to this format.. Here's my code so result when I do addchild its coming proper .. I have also added my commented code so that it will help to understand that I have tried lot of things but couldnt achieve the same.
shareToSocialNetworking: function () {
cc.director.setNextDeltaTimeZero(true);
var newsize = cc.director.getVisibleSize();
var renderText = new cc.RenderTexture(newsize.width,newsize.height);
renderText.begin();
cc.director.getRunningScene().visit();
renderText.end();
var result = cc.Sprite.create(renderText.getSprite().getTexture());
result.flippedY = true;
this._mainViewNode.addChild(result,6000);
//renderText.saveToFile("screenshot.jpg",cc.IMAGE_FORMAT_PNG);
//var based = renderText.getSprite().getTexture().getStringForFormat().toString();
//var data = based.getData();
var file = new Parse.File("screen.jpg", { base64: this.getBase64(result) });
//var file = new Parse.File("screen.jpg", data, "image/png");
var self = this;
file.save().then(function() {
// The file has been saved to Parse.
alert(file.url);
this.onSharePictureInfoLink(file.url());
}, function(error) {
// The file either could not be read, or could not be saved to Parse.
});
//
//var ccImage = renderText.newCCImage();
//
//var str = ccImage.getData();
},
is there any workaround that can be done
there is a private variable called _cacheCanvas, which is the instance of the offscreen canvas
you can simply do renderText._cacheCanvas.toDataURL()
Here's how you can take the screnshot from cocos2d-JS
screenshot: function (fileName) {
var tex = new cc.RenderTexture(winSize.width, winSize.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888);
tex.setPosition(cc.p(winSize.width / 2, winSize.height / 2));
tex.begin();
cc.director.getRunningScene().visit();
tex.end();
var imgPath = jsb.fileUtils.getWritablePath();
if (imgPath.length == 0) {
return;
}
var result = tex.saveToFile(fileName, cc.IMAGE_FORMAT_JPEG);
if (result) {
imgPath += fileName;
cc.log("save image:" + imgPath);
return imgPath;
}
return "";
}
then make a Java call from Javascript
public static void ScreenShot()
{
Bitmap imageBitmap = BitmapFactory.decodeFile(Cocos2dxHelper.getCocos2dxWritablePath() + "/" + "screenshot.png");
String fileHolder = "SampleFolder";
File filepathData = new File("/sdcard/" + fileHolder);
//~~~Create Dir
try {
if (!filepathData.exists())
{
filepathData.mkdirs();
filepathData.createNewFile();
FileWriter fw = new FileWriter(filepathData + fileHolder);
BufferedWriter out = new BufferedWriter(fw);
String toSave = String.valueOf(0);
out.write(toSave);
out.close();
}
}
catch (IOException e1) {
}
//~~~Create Image
File file = new File("/sdcard/" + "Your filename");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
imageBitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e) {}
Uri phototUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
//~~~Add Code Below
}
Do not forget to add permission for external storage
I am trying to add an element in existing isolated folder in xml
code:
public void writeToXML(List<AppTile> appList)
{
// Write to the Isolated Storage
XmlWriterSettings x_W_Settings = new XmlWriterSettings();
x_W_Settings.Indent = true;
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!ISF.FileExists("config.xml"))
{
using (IsolatedStorageFileStream stream = ISF.OpenFile("config.xml", FileMode.CreateNew))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<AppTile>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, x_W_Settings))
{
serializer.Serialize(xmlWriter, appList);
}
stream.Close();
}
}
else
{
string tileName = null;
string url = null;
string key = null;
byte [] tilePic = null;
XDocument loadedData;
if (appList != null)
{
foreach (AppTile app in appList)
{
tileName = app.TileName;
url = app.Url;
key = app.Key;
tilePic = app.TilePic;
// tilePic = Encoding.UTF8.GetString(app.TilePic,0,app.TilePic.Length);
// tilePic = Encoding.Unicode.GetString(app.TilePic,0,app.TilePic.Length);
// var writer = new BinaryWriter(tilePic);
}
using (Stream stream = ISF.OpenFile("config.xml", FileMode.Open, FileAccess.ReadWrite))
{
loadedData = XDocument.Load(stream);
var RootNode = new XElement("AppTile");
RootNode.Add(new XElement("TileName", tileName));
RootNode.Add(new XElement("Key", key));
RootNode.Add(new XElement("Url", url));
RootNode.Add(new XElement("TilePic", tilePic));
// Find Root Element And Descedents and Append New Node After That
var root = loadedData.Element("ArrayOfAppTile");
var rows = root.Descendants("AppTile");
var lastRow = rows.Last();
lastRow.AddAfterSelf(RootNode);
}
// Save To ISOconfig.xml File
using (IsolatedStorageFileStream newStream = new IsolatedStorageFileStream("config.xml", FileMode.Create, ISF))
{
loadedData.Save(newStream);
newStream.Close();
}
}
}
}
}
while reading from xml i am using xmlWriter and deserialize it to get List
when ever i am trying to acces the tilePic of AppTile type i am getting error :The component cannot be found. with the following code::
Image img = new Image();
MemoryStream imgStream = new MemoryStream(NewApp.TilePic);//NewApp is AppTile type
BitmapImage imgSource = new BitmapImage();
imgSource.SetSource(imgStream);//here i get error
img.Source = imgSource;
Its most likely with the TilePic i am saving is not formatted correctly to be retrieved.
Please Help!!
Why don't you use the SaveJpeg extension methodoogy in creating the byte array?
This one might help you!
Windows Phone - byte array to BitmapImage converter throws exception
solved the issue:
while saving the byte array it is encoded to some format not recognizable while reading,So what i have done is saved it as a string.So the updated code is:
else
{
string tileName = null;
string url = null;
string key = null;
string tilePicString = null;
XDocument loadedData;
System.Windows.Media.Imaging.BitmapImage imgSource = new System.Windows.Media.Imaging.BitmapImage();
if (appList != null)
{
foreach (AppTile app in appList)
{
tileName = app.TileName;
url = app.Url;
key = app.Key;
//changed here
tilePicString = System.Convert.ToBase64String(app.TilePic);
}
//same code as above