I found a FlowDocument conversion that passes to XPS and then puts it in my FixedDocument so I can view and print it, it was working perfectly. But now the images no longer appear in the print what could it be?
Thanks in advance for all the help.
FlowDocument doc = new FlowDocument(); //FlowDocument with a image
var paginator = ((IDocumentPaginatorSource)doc).DocumentPaginator;
var package = Package.Open(new MemoryStream(), FileMode.Create, FileAccess.ReadWrite);
var packUri = new Uri("pack://temp.xps");
PackageStore.RemovePackage(packUri);
PackageStore.AddPackage(packUri, package);
var xps = new XpsDocument(package, CompressionOption.NotCompressed, packUri.ToString());
XpsDocument.CreateXpsDocumentWriter(xps).Write(paginator);
FixedDocument TESTE = xps.GetFixedDocumentSequence().References[0].GetDocument(true);
Microsoft publish a workaround for this issue :
https://support.microsoft.com/en-us/topic/kb5022083-change-in-how-wpf-based-applications-render-xps-documents-a4ae4fa4-bc58-4c37-acdd-5eebc4e34556
Related
I am trying to add a signature to the 2nd page of a PDF Form. The PDF was created outside of IText, but it has fields that were populated using IText in C#/MVC. That part works fine.
The issue is getting the signature image to the 2nd page. I can get the image onto the 1st page and position it, but cannot get it to the 2nd page.
I have tried to using the var PdfDocument class' GetPage(x) method without any luck. I have also tried to use GetLastPage(). No luck.
I tried the above approach before flattening the fields and after but no luck.
PdfReader reader = new PdfReader(fileSource);
System.IO.MemoryStream m = new System.IO.MemoryStream();
PdfWriter writer = new PdfWriter(m);
PdfDocument pdf = new PdfDocument(reader, writer);
Document document = new Document(pdf);
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
String imageFile = "C:/Temp/signature.png";
ImageData data = ImageDataFactory.Create(imageFile);
Image img = new Image(data);
img.SetFixedPosition(50, 50);
pdf.GetPage(2);
document.Add(img);
form.FlattenFields();
document.Close();
pdf.Close();
pdfBytes = m.ToArray();
return new FileContentResult(pdfBytes, "application/pdf");
Any help would be appreciated.
I am using ITextSharp to convert HTML to PDF but i want the PDF to be generated of size 5cm width. I used the following code
var pgSize = new iTextSharp.text.Rectangle(2.05f, 2.05f);
Document doc = new Document(pgSize);
but it is just resizing the pdf and my data disappeared in the pdf or get hide.
How can i align the data in the center in PDF or resize the pdf? Here is my code
public void ConvertHTMLToPDF(string HTMLCode)
{
try
{
System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
StringReader reader = new StringReader(HTMLCode);
var pgSize = new iTextSharp.text.Rectangle(2.05f, 2.05f);
Document doc = new Document(pgSize);
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~") + "/App_Data/HTMLToPDF.pdf",
FileMode.Create));
doc.Open();
foreach (IElement element in HTMLWorker.ParseToList(
new StringReader(HTMLCode), null))
{
doc.Add(element);
}
doc.Close();
Response.End();
}
catch (Exception ex)
{
}
}
You are creating a PDF that measures 0.0723 cm by 0.0723 cm. That is much too small to add any content. If you want to create a PDF of 5 cm by 5 cm, you need to create your document like this:
var pgSize = new iTextSharp.text.Rectangle(141.732f, 141.732f);
Document doc = new Document(pgSize);
As for the alignment, that should be defined in the HTML, but you are using an old version of iText and you are using the deprecated HTMLWorker.
You should upgrade to iText 7 and pdfHTML as described here: Converting HTML to PDF using iText
Also: the size of the page can be defined in the #page-rule of the CSS. See Huge white space after header in PDF using Flying Saucer
Why would you make it difficult for yourself by using an old iText version, when the new version allows you to do this:
#page {
size: 5cm 5cm;
}
I have a simple questions. How can you show a PDf file by using PagePreview?
I have a full pathname document.FileName = "c:\scans\Insurance_34345.pdf";
pagePreview.Preview(document.FileName); or something...
If there another way for showing a pdf. It's okay. I want to show it on a WinForms Form.
I tried this. I don't know what I have to do...
in the Designer
private MigraDoc.Rendering.Forms.DocumentPreview dpvScannedDoc;
Part of the code
string fullPadnaam = Path.Combine(defaultPath, document.FileName);
//PdfDocument pdfDocument = new PdfDocument(fullPadnaam);
//PdfPage page = new PdfPage(pdfDocument);
//XGraphics gfx = XGraphics.FromPdfPage(page);
MigraDoc.DocumentObjectModel.Document pdfDocument = new MigraDoc.DocumentObjectModel.Document();
pdfDocument.ImagePath = fullPadnaam;
var docRenderer = new DocumentRenderer(pdfDocument);
docRenderer.PrepareDocument();
var inPdfDoc = PdfReader.Open(fullPadnaam, PdfDocumentOpenMode.ReadOnly);
for (var i = 0; i < inPdfDoc.PageCount; i++)
{
pdfDocument.AddSection();
docRenderer.PrepareDocument();
var page = inPdfDoc.Pages[i];
var gfx = XGraphics.FromPdfPage(page);
docRenderer.RenderPage(gfx, i + 1);
}
var renderer = new PdfDocumentRenderer();
renderer.Document = pdfDocument;
renderer.RenderDocument();
// MigraDoc.DocumentObjectModel.IO.DdlWriter dw = new MigraDoc.DocumentObjectModel.IO.DdlWriter("HelloWorld.mdddl");
// dw.WriteDocument(pdfDocument);
// dw.Close();
//renderer.PdfDocument.rea(outFilePath);
//string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document1);
dpvScannedDoc.Show( pdfDocument);
PDFsharp does not render PDF files. You cannot show PDF files using the PagePreview.
If you use the XGraphics class for drawing then you can use shared code that draws on the PagePreview and on PDF pages.
The PagePreview sample can be found in the sample package and here:
http://www.pdfsharp.net/wiki/Preview-sample.ashx
If you have code that creates a new PDF file using PDFsharp then you can use the PagePreview to show on screen what you would otherwise draw on PDF pages. You cannot draw existing PDF pages using the PagePreview because PDF does not render PDF.
The MigraDoc DocumentPreview can display MDDDL files (your sample code creates a file "HelloWorld.mdddl"), but it cannot display PDF files.
If the MDDDL uses PDF files as images, they will not show up in the preview. They will show when creating a PDF from the MDDDL.
I have been following this answer in trying to read text from an XPS document.
I have the following code:
string filePath = #"C:\A.xps";
XpsDocument _xpsDocument = new XpsDocument(filePath, System.IO.FileAccess.Read);
IXpsFixedDocumentSequenceReader fixedDocSeqReader = _xpsDocument.FixedDocumentSequenceReader;
The problem is fixedDocSeqReader is null.
I tried using this instead:
var seq = _xpsDocument.GetFixedDocumentSequence();
But seq is also null.
Any ideas?
I have a DocumentViewer with a fixedDocument (constructed in XAML) I then add content to the fixedDocument in code and it displays perfecty on screen.
My problem is when I try to create an XPS file from the fixedDocument, im getting an 'its already a child of another element' error.
I cant find a DocumentViewer.Children.Clear method, How can I remove/detach the fixedDocument so I can use it to create the file?
for completeness, here's the code where im getting the error:
public void CreateXPSFile()
{
// 1 - create xps_file
string OutputPath = baseDir + pathAdjust + "test.xps";
using (FileStream fs = File.Create(OutputPath))
{
ConvertToXps(fixedDocument, fs);
}
// open the document using the system default xps viewer
Process.Start(OutputPath);
}
public static void ConvertToXps(FixedDocument fd, Stream outputStream)
{
var package = Package.Open(outputStream, FileMode.Create);
var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
// xps documents are built using fixed document sequences
var fixedDocSeq = new FixedDocumentSequence();
var docRef = new DocumentReference();
docRef.BeginInit();
docRef.SetDocument(fd);
docRef.EndInit();
((IAddChild)fixedDocSeq).AddChild(docRef); <<<<<----- Error occurs here
// write out our fixed document to xps
xpsWriter.Write(fixedDocSeq.DocumentPaginator);
xpsDoc.Close();
package.Close();
}
Thanks
You should just be able to set the Document to null.
DocumentViewer dv;
dv.Document = null;
Since you are not loading the XPS into dv then setting the dv.Document = null; might not do the trick. Rather than Process.Start(OutputPath); load the xps into dv. Or you can assign the process to a name so you can close it. But I would explicitly load into dv.
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
// ...
myProcess.Close();