RichEditDocumentServer docx conversion to pdf not working - devexpress-wpf

I am trying to convert a word document(.docx) into PDF. I am trying to pass Stream object into load document method of RichEditDocumentServer,but my bad ,I am getting a blank pdf. I tried passing the file path in load document method ,which worked fine. But my requirement has to meet with stream object. Can anyone help me to fix the issue. A sample code has been added below.
private Stream ConvertToPdf(Stream fileStream)
{
RichEditDocumentServer server = new RichEditDocumentServer();
fileStream.Seek(0, SeekOrigin.Begin);
server.LoadDocument(fileStream, DocumentFormat.Doc);
Stream convertStream = new MemoryStream();
server.ExportToPdf(convertStream);
convertStream.Seek(0, SeekOrigin.Begin);
return convertStream;
}

it worked with the below link
https://www.devexpress.com/support/center/Question/Details/T340655#answer-9c3224dd-383a-4faa-9672-9b34e36c1c7a
server.LoadDocument(fileStream, DocumentFormat.OpenXml);

Related

iText - add image to an existing PDF

I am doing the following using iText.
I have a PDF
I add an image to the PDF
I save the modified PDF.
To add image in PDF I am using this method itext-add.
This worked fine until I got a certain PDF. In this PDF, the adding-image-to-the-pdf method doesn't work. Moreover, it corrupts the PDF.
Points to note:
I'm getting PDFs from a third party and these are contractual PDFs. So it is possible that they have added some restrictions.
And one fun fact, when I add an annotation on the same page where I want to add that image, that image starts coming!
I'm using iText 7.1.10
String srcFileName = "/Users/kalpit/Desktop/step1-stack.pdf";
String destFileName = "step1-test1-kd2.pdf";
File destFile = new File(destFileName);
PdfDocument pdf = new PdfDocument(new PdfReader(srcFileName), new PdfWriter(destFile),new StampingProperties().useAppendMode());
Document document = new Document(pdf);
String imFile = "/Users/kalpit/Desktop/sample-image.png";
ImageData data = ImageDataFactory.create(imFile);
Image image = new Image(data);
document.add(image);
document.close();
and here is the PDF : https://ipupload.com/tP6/step1.pdf
As #Nikita found out, the problem does only occur if working in append mode. The cause is that in unfortunate conditions the changed resources are not stored, a bug.
The unfortunate conditions here are that on page 1
the content stream array already is an indirect object in its own right, so only this indirect object (and not the page object) is marked as changed when the instructions for showing the image are added; and
the resources and resource type dictionaries are direct objects but only they (and not the page object, the indirect object holding them) are marked as changed when the image resource is added.
Thus, the changes to the page content streams are stored but the page object is not.
So there now is an instruction to draw an image from an image page resource which is not there.
One way to work around this is not to use append mode as proposed by #Nikita.
Alternatively, if you are required to use append mode, you can explicitly mark your page as changed:
Document document = new Document(pdf);
String imFile = "/Users/kalpit/Desktop/sample-image.png";
ImageData data = ImageDataFactory.create(imFile);
Image image = new Image(data);
document.add(image);
pdf.getFirstPage().setModified(); // <-----
document.close();
Well, something wrong with appendMode. Do you really need him? Try to add image via the next code:
PdfDocument pdf = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFileName));
Document doc = new Document(pdf);
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.createPng(UrlUtil.toURL(pathToYourimage.png)));
Image image = new Image(xObject, 100).setHorizontalAlignment(HorizontalAlignment.RIGHT);
doc.add(image);
doc.close();
The result is the next

Combine two PDF-a documents using ITextSharp

hoping that someone can see the flaw in my code to merge to PDF-a documents using ITextSharp. Currently it complains about missing metadata which PDF-a requires.
Document document = new Document();
MemoryStream ms = new MemoryStream();
using (PdfACopy pdfaCopy = new PdfACopy(document, ms, PdfAConformanceLevel.PDF_A_1A))
{
document.Open();
using (PdfReader reader = new PdfReader("Doc1.pdf"))
{
pdfaCopy.AddDocument(reader);
}
using (PdfReader reader = new PdfReader("doc2.pdf"))
{
pdfaCopy.AddDocument(reader);
}
}
The exact error received is
Unhandled Exception: iTextSharp.text.pdf.PdfAConformanceException: The document catalog dictionary of a PDF/A conforming file shall contain
the Metadata key
I was hoping that the 'document catalog dictionary' would be copied as well, but I guess the 'new Document()' creates an empty non-conforming document or something.
Thanks! Hope you can help
Wouter
You need to add this line:
copy.CreateXmpMetadata();
This will create some default XMP metadata. Of course: if you want to create your own XMP file containing info about the documents you're about to merge, you can also use:
copy.XmpMetadata = myMetaData;
where myMetaData is a byte array containing a correct XMP stream.
I hope you understand that iText can't automatically create the correct metadata. Providing metadata is something that needs human attention.

Sending email with attachment using scala and Liftweb

This is the first time i am integrating Email service with liftweb
I want to send Email with attachments(Like:- Documents,Images,Pdfs)
my code looking like below
case class CSVFile(bytes: Array[Byte],filename: String = "file.csv",
mime: String = "text/csv; charset=utf8; header=present" )
val attach = CSVFile(fileupload.mkString.getBytes("utf8"))
val body = <p>Please research the enclosed.</p>
val msg = XHTMLPlusImages(body,
PlusImageHolder(attach.filename, attach.mime, attach.bytes))
Mailer.sendMail(
From("vyz#gmail.com"),
Subject(subject(0)),
To(to(0)),
)
this code is taken from LiftCookbook its not working like my requirement
its working but only the Attached file name is coming(file.csv) no data in it(i uploaded this file (gsy.docx))
Best Regards
GSY
You don't specify what type fileupload is, but assuming it is of type net.liftweb.http. FileParamHolder then the issue is that you can't just call mkString and expect it to have any data since there is no data in the object, just a fileStream method for retrieving it (either from disk or memory).
The easiest to accomplish what you want would be to use a ByteArrayInputStream and copy the data to it. I haven't tested it, but the code below should solve your issue. For brevity, it uses Apache IO Commons to copy the streams, but you could just as easily do it natively.
val data = {
val os = new ByteArrayOutputStream()
IOUtils.copy(fileupload.fileStream, os)
os.toByteArray
}
val attach = CSVFile(data)
BTW, you say you are uploading a Word (DOCX) file and expecting it to automatically be CSV when the extension is changed? You will just get a DOCX file with a csv extension unless you actually do some conversion.

iTextSharp dll 4.1.2.0 issue causing blank pages in a merged pdf

I am using iTextSharp dll version 4.1.2.0 for pdf merging.But it is causing some pages blank in the final merged pdf. but this issue is not present in its latest dll.
I am using .net framework 1.1 , so i can't use latest dll cause it doesn't support.
So,Please give suggestion what should i do for this.
Thanks
Yes, I have got the solutions like "if we will use latest dll then it is ok, no problem but what if we are using a dll that supports .net framework 1.1 that might be before the latest dll.
The problem in my case are some pdfs are corrupted and not able to parse correctly, that's why it is throwing an exception as "Attempt to Read Past the End of The Stream" .And i found in web that is some pdfs having some more characters after EOF Marker then it is the problem .So,we have to just remove all the characters from the file and test in the newly created pdf file. It has worked for me.
public void RemoveExtraBytes(string ofilepath,nfilepath)
{
string oldfilePath =ofilepath;
string newFilePath="nfilepath";
WebClient client = new WebClient();
byte[] buffer = client.DownloadData(filePath);
string str;
int position = 0;
str = ASCIIEncoding.ASCII.GetString(buffer);
if (str.Contains("%%EOF"))
{
position = str.LastIndexOf("%%EOF");
}
Stream stream = new System.IO.FileStream(newfilepath, FileMode.Create);
stream.Write(buffer, 0, position);
stream.Close();
}

error in generating pdf using iTextSharp means previous pdf file is diplayed

I am working in asp .net mvc3.
I have these statements in controller class:
PdfWriter.GetInstance(doc, new FileStream((Request.PhysicalApplicationPath + "\\Receipt5.pdf"),
FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(2);
table.AddCell("tt[0]");
table.AddCell("tt[1]");
doc.close();
All time my values are changing but in pdf sometimes showing old result. please tell me what should i do for it that whenever i press done button then new pdf document should generate.
i am using iTextSharp to generate pdf.
It seems that you're not able to replace the old file cause it is locked.
Try to delete it and see what happens.
Anyway, consider that if more than one user tries to print the same document you can have a concurrency problem.
I would suggest you to use a generated file name:
var newFile = System.IO.Path.Combine(Request.PhysicalApplicationPath, Guid.NewGuid().ToString() + ".pdf");