Save the output as PDF file - c#

We are currently using SoftArtisans to generate Excel and Word files.
We need to extend this to also create PDF files.
Does OfficeWriter currently support this?
If not, any plans to add this feature? Or any opensource library that can be used to convert Excel/Word files to PDF format?

PdfSharp and Migradoc as far as I know are the best and the most popular. Migradoc is the higher-level cover for PdfSharp.

Note: I work for SoftArtisans, makers of OfficeWriter.
OfficeWriter does not currently support converting Excel/Word files to PDF. We generally recommend a 3rd party component to convert to PDF. However, many of these components require having Office installed on the server, which Microsoft does not advise. Therefore, it is important that you choose a converter that either does not require having Office on the server, or manages it carefully
Here are a few solutions for converting Word to PDF that we’ve recommended to our users in the past:
• Word Services for Sharepoint – If you are using SharePoint Server 2010, then you can use Word Services to perform the format conversion. More information about this solution can be found at: http://msdn.microsoft.com/en-us/library/office/ff181518.aspx
• Rainbow PDF - rainbowpdf.com
• EasyPDF - pdfonline.com/easypdf/
For more information, please see our blog post: http://blog.softartisans.com/2011/08/05/kb-tools-to-convert-word-documents-to-pdf/

PfgSharp is quite popular. Here is an example from CodeProject on how to create a simple PDF to get some feeling on how to use it:
class Program
{
static void Main(string[] args)
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// Save the document...
const string filename = "HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
}

Related

Syncfusion DocIO: When converting from word document to pdf, footer is nowhere to be found

I am trying to simply convert a word document to a pdf and render it to the browser. When I do so, the converted PDF doesn't has the word document footer.
Here is my method:
private int ConvertShowPDF(WordDocument RevisionWordDocument)
{
RevisionWordDocument.ChartToImageConverter = new ChartToImageConverter();
DocToPDFConverter converter = new DocToPDFConverter();
try
{
PdfDocument pdfDocument = converter.ConvertToPDF(RevisionWordDocument);
MemoryStream pdfStream = new MemoryStream();
pdfDocument.Save(pdfStream);
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=RevisionesAdministrativas.PDF");
HttpContext.Current.Response.AddHeader("content-length", pdfStream.Length.ToString());
HttpContext.Current.Response.BinaryWrite(pdfStream.ToArray());
HttpContext.Current.Response.End();
pdfDocument.Close();
RevisionWordDocument.Save(HttpContext.Current.Server.MapPath("~/App_Data/Edited.docx"), FormatType.Docx);
RevisionWordDocument.Close();
return 0;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return 1;
}
}
What I am doing wrong?? This is some kind of bug? Help please. Thank you!
Thank you for using Syncfusion products.
On further analyzing with the given input document from forum #132027, we have found that document contains group shape in the footer.
Currently DocIO doesn’t provide support for grouped shapes preservation in Word to PDF conversion. We have already logged this as a feature request in our database. We will implement this feature in any of our upcoming releases and we don’t have any immediate plans to implement this feature. We usually have an interval of at least three months between releases. The feature implementation would also greatly depend on the factors such as product design, code compatibility and complexity. We will update you when this feature has been implemented.
As a work-around kindly modify the group shape as ungroup shape in the input Word document thereby it will be preserved properly in the PDF document. We have modified your Word document with image. Please find the modified document from below link.
Modified document link:
http://www.syncfusion.com/downloads/support/forum/132027/doc/RevisionesAdministrativas-Multiples_Modified-139642195.docx
Please let us know if you have any other questions.
Regards,
Ramanan H
I found the problem in my Word document
My Word document has a footer with a custom shape. As noted in the documentation, custom shapes are not supported in the DocIO Syncfusion library yet.
The workaround for me was saving the document as PDF from Microsoft Word, and with Snipping Tool, taking a cropped screenshot of the the footer, saving it as a .jpg image and inserting this image in the Word document footer.
In order to be allowed to put the image edge to edge in the document footer, you can follow this workaround.
That's it.

iText GetTextFromPage exception with inline image

I have the same problem as was discussed here, which was not solved. My objective is to extract the text from an existing pdf file. I get the error message Could not find image data or EI for a certain pdf, which I cannot share as a sample. It works for other pdfs, with the following code
string fileURI = "C:\\Test\\Sample.pdf";
PdfReader reader = new PdfReader(fileURI);
ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
string s = PdfTextExtractor.GetTextFromPage(reader, 1, strategy);
Debug.WriteLine(s);
I am using iTextSharp 5.5.0 and tried changing found == 1 to found <= 1 as suggested in other posts. It does not help.
Would it help to remove all images in the pdf? I really just need the text. Which commands from iText could help me with this?
I downloaded the trial version of Acrobat to create a version of the pdf file, that I could share. After opening the file and saving it again as "Optimized PDF" over the Acrobat, the code was working and I could extract the text.
So the solution to the problem is probably opening each file in Acrobat and saving it again with the right settings using the Acrobat reference and then extracting the text.

Print BING Map Location in the PDF programmatically

I am using Silverlight BING Map Api to show the location (by providing the address) on my website..
And I want to show this Map location in the PDF file programmatically, I tried alot using 'wkhtmltopdf' but all in-vain, It shows empty space instead of BING MAP...
Please guide me in this case, I am open in using any other open source PDF generation tool.
Thanks
Xtremist
If you want insert image to PDF use iText library. It is very easy to start using it:
string pdfFilename = #"c:\temp\test.pdf";
string imageFilename = #"C:\map.jpg";
// Create PDF writer, document and image to put
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilename);
Document doc = new Document();
PdfWriter pdfW = PdfWriter.GetInstance(doc, new FileStream(pdfFilename, FileMode.Create));
// Open created PDF and insert image to it
doc.Open();
doc.Add(image);
doc.Close();
Or do you want to save Bing map as image?
I don't think that there's an easy way to do it through the Silverlight SDK. Instead, I think Microsoft prefers that you use the Bing Maps SOAP Services. Specifically you'll want to look at the Imagery Service where you can send a ImageryMetadataRequest with location/zoom info set in the ImageryMetadataOptions. Some basic sample code can be found here.
Once you get the images you can pretty easily add them to a PDF.

Can a PDF be converted to a vector image format that can be printed from .NET?

We have a .NET app which prints to both real printers and PDF, currently using PDFsharp, although that part can be changed if there's a better option. Most of the output is generated text or images, but there can be one or more pages that get appended to the end. That page(s) are provided by the end-user in PDF format.
When printing to paper, our users use pre-printed paper, but in the case of an exported PDF, we concatenate those pages to the end, since they're already in PDF format.
We want to be able to embed those PDFs directly into the print stream so they don't need pre-printed paper. However, there aren't really any good options for rendering a PDF to a GDI page (System.Drawing.Graphics).
Is there a vector format the PDF could be converted to by some external program, that could rendered to a GDI+ page without being degraded by conversion to a bitmap first?
In an article titled "How To Convert PDF to EMF In .NET," I have shown how to do this using our PDFOne .NET product. EMFs are vector graphics and you can render them on the printer canvas.
A simpler alternative for you is PDF overlay explained in another article titled "PDF Overlay - Stitching PDF Pages Together in .NET." PDFOne allows x-y offsets in overlays that allows you stitch pages on the edges. In the article cited here, I have overlaid the pages one over another by setting the offsets to zero. You will have set it to page width and height.
DISCLAIMER: I work for Gnostice.
Ghostscript can output PostScript (which is a vector file) which can be directly sent to some types of printers. For example, if you're using an LPR capable printer, the PS file can be directly set to that printer using something like this project: http://www.codeproject.com/KB/printing/lpr.aspx
There are also some commercial options which can print a PDF (although I'm not sure if the internal mechanism is vector or bitmap based), for example http://www.tallcomponents.com/pdfcontrols2-features.aspx or http://www.tallcomponents.com/pdfrasterizer3.aspx
I finally figured out that there is an option that addresses my general requirement of embedding a vector format into a print job, but it doesn't work with GDI based printing.
The XPS file format created by Microsoft XPS Writer print driver can be printed from WPF, using the ReachFramework.dll included in .NET. By using WPF for printing instead of GDI, it's possible to embed an XPS document page into a larger print document.
The downside is, WPF printing works quite a bit different, so all the support code that directly uses stuff in the Sytem.Drawing namespace has to be re-written.
Here's the basic outline of how to embed the XPS document:
Open the document:
XpsDocument xpsDoc = new XpsDocument(filename, System.IO.FileAccess.Read);
var document = xpsDoc.GetFixedDocumentSequence().DocumentPaginator;
// pass the document into a custom DocumentPaginator that will decide
// what order to print the pages:
var mypaginator = new myDocumentPaginator(new DocumentPaginator[] { document });
// pass the paginator into PrintDialog.PrintDocument() to do the actual printing:
new PrintDialog().PrintDocument(mypaginator, "printjobname");
Then create a descendant of DocumentPaginator, that will do your actual printing. Override the abstract methods, in particular the GetPage should return DocumentPages in the correct order. Here's my proof of concept code that demonstrates how to append custom content to a list of Xps documents:
public override DocumentPage GetPage(int pageNumber)
{
for (int i = 0; i < children.Count; i++)
{
if (pageNumber >= pageCounts[i])
pageNumber -= pageCounts[i];
else
return FixFixedPage(children[i].GetPage(pageNumber));
}
if (pageNumber < PageCount)
{
DrawingVisual dv = new DrawingVisual();
var dc = dv.Drawing.Append();
dc = dv.RenderOpen();
DoRender(pageNumber, dc); // some method to render stuff to the DrawingContext
dc.Close();
return new DocumentPage(dv);
}
return null;
}
When trying to print to another XPS document, it gives an exception "FixedPage cannot contain another FixedPage", and a post by H.Alipourian demonstrates how to fix it: http://social.msdn.microsoft.com/Forums/da/wpf/thread/841e804b-9130-4476-8709-0d2854c11582
private DocumentPage FixFixedPage(DocumentPage page)
{
if (!(page.Visual is FixedPage))
return page;
// Create a new ContainerVisual as a new parent for page children
var cv = new ContainerVisual();
foreach (var child in ((FixedPage)page.Visual).Children)
{
// Make a shallow clone of the child using reflection
var childClone = (UIElement)child.GetType().GetMethod(
"MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic
).Invoke(child, null);
// Setting the parent of the cloned child to the created ContainerVisual
// by using Reflection.
// WARNING: If we use Add and Remove methods on the FixedPage.Children,
// for some reason it will throw an exception concerning event handlers
// after the printing job has finished.
var parentField = childClone.GetType().GetField(
"_parent", BindingFlags.Instance | BindingFlags.NonPublic);
if (parentField != null)
{
parentField.SetValue(childClone, null);
cv.Children.Add(childClone);
}
}
return new DocumentPage(cv, page.Size, page.BleedBox, page.ContentBox);
}
Sorry that it's not exactly compiling code, I just wanted to provide an overview of the pieces of code necessary to make it work to give other people a head start on all the disparate pieces that need to come together to make it work. Trying to create a more generalized solution would be much more complex than the scope of this answer.
While not open source and not .NET native (Delphi based I believe, but offers a precompiled .NET library), Quick PDF can render a PDF to an EMF file which you could load into your Graphics object.

Generate a thumbnail of a Word document

I have a website where users upload Word documents and I want to display thumbnails of these word documents. If anyone of you knows how to display the first page of a Word file as an image using C# please tell me.
Also if you know a trusted .NET library to convert word files to images that requires no office interop that would be great.
http://blogs.msdn.com/windowssdk/archive/2009/06/12/windows-api-code-pack-for-microsoft-net-framework.aspx
ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;
It's Microsoft's API Code Pack
I found this question (7 yrs later) while searching for a similar solution. I'm evaluating 2JPEG and it appears to support 275 formats including Word, Excel, Publisher & Powerpoint files. fCoder recommends running 2JPEG as a scheduled background task. The command line syntax is pretty comprehensive.
Here's a sample snippet to generate a thumbnail for a specific file:
2jpeg.exe -src "c:\files\myfile.docx" -dst "c:\files" -oper Resize size:"100 200" fmode:fit_width -options pages:"1" scansf:no overwrite:yes template:"{Title}_thumb.jpg" silent:yes
A preview image of the 1st page of a .doc or .docx document can easily be created with a tool called Free Spire.Doc for .NET (a totally free word API for commercial and personal use). I found it to be fast and accurate.
Note from the developer's page:
"The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013, Word 2016 and Word 2019) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc.
Friendly Reminder:
Free version is limited to 500 paragraphs and 25 tables... "
This C# code creates a System.Drawing.Image object of the 1st page of a .docx file:
using Spire.Doc
byte[] docContent = File.ReadAllBytes(#"C:\Temp\word_document.docx");
using (MemoryStream ms = new MemoryStream(docContent))
{
// Creates a Spire.Doc object to work with
Spire.Doc.Document doc = new Spire.Doc.Document(ms, Spire.Doc.FileFormat.Auto);
// SaveToImages creates an array of System.Drawing.Image, we take only the 1st element
System.Drawing.Image img = doc.SaveToImages(0, 1, Spire.Doc.Documents.ImageType.Bitmap)[0];
}
To create the thumbnail image, the following C# example includes a second using block to do it, and then converts to a base64 string:
using Spire.Doc
byte[] docContent = File.ReadAllBytes(#"C:\Temp\word_document.docx");
using (MemoryStream ms = new MemoryStream(docContent))
{
// Creates a Spire.Doc object to work with
Spire.Doc.Document doc = new Spire.Doc.Document(ms, Spire.Doc.FileFormat.Auto);
// SaveToImages creates an array of System.Drawing.Image, we take only the 1st element
System.Drawing.Image img = doc.SaveToImages(0, 1, Spire.Doc.Documents.ImageType.Bitmap)[0];
using (var ms2 = new MemoryStream())
{
// Auxiliary object needed for GetThumbnailImage
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
// We create a thumbnail (0.5 width and height = 50%)
img.GetThumbnailImage((int)(img.Width * 0.5), (int)(img.Height * 0.5), myCallback, IntPtr.Zero).Save(ms2, System.Drawing.Imaging.ImageFormat.Png);
// Convert to Base64 string representation of the image
return Convert.ToBase64String(ms2.ToArray());
}
}
In addition, the library can also convert in other ways, for instance this function returns .SVG files with each page:
doc.SaveToFile("resulting_file_name.svg", Spire.Doc.FileFormat.SVG);

Categories