I am Using Xamarin form.
I want to convert Webview to byte[] for printing functionality.
my code is below
// Generate the HTML
var htmlString = printTemplate.GenerateString();
// Create a source for the web view
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = htmlString;
// Create and populate the Xamarin.Forms.WebView
var browser = new WebView();
browser.Source = htmlSource;
var printService = DependencyService.Get<IPrintService>();
printService.Print({{I WANT byte[] /Image here}});
Related
PdfReader reader = new PdfReader(sourceXfaPath);
PdfWriter writer = new PdfWriter(exportPdf);
PdfDocument pdfDoc = new PdfDocument(reader, writer, new StampingProperties().UseAppendMode());
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
XfaForm xfa = form.GetXfaForm();
xfa.FillXfaForm(new FileStream(exportXfaXml,FileMode.Open));
xfa.Write(pdfDoc);
pdfDoc.Close();
reader.Close();
codes are above:
but it doesn't create pdf document, I'm not sure why it doesn't create it.
I just tried xfa.FillXfaForm(new FileStream(exportXfaXml,FileMode.Open)); to xfa.FillXfaForm(XmlReader.Create(path));
but it shows me same error
public void customGenerate(string sourceFilePath, string destinationtFilePath, string replacementXmlFilePath)
{
PdfReader pdfReader = new PdfReader(sourceFilePath);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(pdfReader, ms, '\0', true))
//using (PdfStamper stamper = new PdfStamper(pdfReader, ms))
{
XfaForm xfaForm = new XfaForm(pdfReader);
XmlDocument doc = new XmlDocument();
doc.Load(replacementXmlFilePath);
xfaForm.DomDocument = doc;
xfaForm.Changed = true;
XfaForm.SetXfa(xfaForm, stamper.Reader, stamper.Writer);
stamper.Close();
}
var bytes = ms.ToArray();
System.IO.File.WriteAllBytes(destinationtFilePath, bytes);
ms.Close();
}
pdfReader.Close();
}
This is my code to add Javacript to XFA PDF.
I'm going to add JavaScript[handle button , checkbox] to XFA pdf with avoid signature.
i'm trying to create a pdf using PdfSharpCore for Xamarin Forms but I've run into some issues.
private void CreatePdf(object sender)
{
PdfDocument pdf = new PdfDocument();
PdfPage pdfPage = pdf.AddPage();
XGraphics graph = XGraphics.FromPdfPage(pdfPage);
XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
graph.DrawString("This is my first PDF document", font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string localFilename = "test.pdf";
string localPath = Path.Combine(documentsPath, localFilename);
MemoryStream stream = new MemoryStream();
pdf.Save(stream, false);
byte[] bytes = stream.ToArray();
File.WriteAllBytes(localPath, bytes);
}
This is my function that generate the pdf and save it but when I press the button that invoke it, nothing happens.
I've already add the the permissions on the AndroidManifest.xml file like this:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I've tried using a library called Xamarin.Forms.SaveOpenPDFPackage. With this library you can
save and open your brand new pdf by doing this:
await CrossXamarinFormsSaveOpenPDFPackage.Current.SaveAndView("myFile1.pdf", "application/pdf", stream, PDFOpenContext.InApp);
It half works: it opens my new pdf but it doesn't save it.
Do you have any tip?
You could try to use Syncfusion.Xamarin.PDF.
Install from NuGet package first.
Creating a PDF document with table:
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page.
PdfPage page = doc.Pages.Add();
//Create a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
//Add values to list
List<object> data = new List<object>();
Object row1 = new { ID = "E01", Name = "Clay" };
Object row2 = new { ID = "E02", Name = "Thomas" };
Object row3 = new { ID = "E03", Name = "Andrew" };
Object row4 = new { ID = "E04", Name = "Paul" };
Object row5 = new { ID = "E05", Name = "Gray" };
data.Add(row1);
data.Add(row2);
data.Add(row3);
data.Add(row4);
data.Add(row5);
//Add list to IEnumerable
IEnumerable<object> dataTable = data;
//Assign data source.
pdfGrid.DataSource = dataTable;
//Draw grid to the page of PDF document.
pdfGrid.Draw(page, new PointF(10, 10));
//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
doc.Save(stream);
//Close the document.
doc.Close(true);
//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
For more details about the ISave, you could get the whole code from Syncfusion official document.
I have 2 PDF files. under is single-page PDF file and must be a background for every page of original...
This is not working
public static byte[] overlay(byte[] original, byte[] under)
{
using (var resultStream = new MemoryStream())
{
var pdfWriter = new PdfWriter(resultStream);
var pdfReader = new PdfReader(new MemoryStream(original));
var pdfDoc = new PdfDocument(pdfReader, pdfWriter);
for (var p = 1; p <= pdfDoc.GetNumberOfPages(); p++)
{
var pdfUnder = new PdfDocument(new PdfReader(new MemoryStream(under)));
var pdfUnderPage = pdfUnder.GetFirstPage().CopyAsFormXObject(pdfDoc);
var page = pdfDoc.GetPage(p);
var canvas = new PdfCanvas(page.NewContentStreamBefore(),
page.GetResources(), pdfDoc);
canvas.AddXObjectAt(pdfUnderPage, 0, 0);
pdfUnder.Close();
}
pdfDoc.Close();
return resultStream.ToArray();
}
}
I have been trying to make an application that invokes camera through page rendering. I have used Custom Renderer from Xamarin. My problem is I need to send the picture to the other page/activity in the "Native" after clicking, but currently it is saving the picture in the gallery of the device.
For example: I click the image and then the image gets displayed with the message "Do you want to save it?". This has to be done in native rather than PCL. I have been trying through intent but that doesn't work.
All my code right now doing is saving the image to the gallery.
try
{
var absolutePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim).AbsolutePath;
var folderPath = absolutePath + "/Camera";
var filePath = System.IO.Path.Combine(folderPath, string.Format("photo_{0}.jpg", Guid.NewGuid()));
var fileStream = new FileStream(filePath, FileMode.Create);
await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, fileStream);
fileStream.Close();
image.Recycle();
// imageByte = ((byte[])image);
var intent = new Android.Content.Intent(Android.Content.Intent.ActionMediaScannerScanFile);
var file = new Java.IO.File(filePath);
var uri = Android.Net.Uri.FromFile(file);
intent.SetData(uri);
//intent.PutExtra("image", imageByte);
MainActivity.Instances.SendBroadcast(intent);
}
Solved it. Passing bitmap through intent.
Activity 1:
byte[] imageByte;
var image = textureView.Bitmap;
MemoryStream memStream = new MemoryStream();
// ByteArrayOutputStream _bs = new ByteArrayOutputStream();
await image.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, memStream);
imageByte = memStream.ToArray();
Intent i = new Intent(this.Context, typeof(CameraDisplay));
i.PutExtra("image", imageByte);
activity.StartActivity(i);
Activity 2:
byte[] Image = Intent.GetByteArrayExtra("image");
imageView = FindViewById(Resource.Id.imageView1);
Bitmap bitmap = BitmapFactory.DecodeByteArray(Image, 0, Image.Length);
imageView.SetImageBitmap(bitmap);
I need to make a GET request that requires oauth, for and image to display in a page. Is there a way to do this without building a custom webrequest or httpwebrequest?
If it's an image make sure that you are reading the stream as binary data.
Something like:
var httpRequest = (HttpWebRequest)ar.AsyncState;
var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(ar);
int lengthInBytes = Convert.ToInt32(httpResponse.ContentLength);
BinaryReader br = new BinaryReader(httpResponse.GetResponseStream());
byte[] imageInBytes = new byte[lengthInBytes];
using (br)
{
for (int i = 0; i < lengthInBytes; i++)
{
imageInBytes[i] = br.ReadByte();
}
}
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
MemoryStream rawBytesStream = new MemoryStream(imageInBytes);
BitmapImage img = new BitmapImage();
img.SetSource(rawBytesStream);
imageInUI.Source = img;
});