Loading an online image in a word document - c#

Im developing a wpf app that simply inserts an image to a word document. Each time the word document is open i want the picture to call the image from a server, for example (server.com/Images/image_to_be_insert.png)
My code is as follow:
Application application = new Application();
Document doc = application.Documents.Open(file);
var img = doc.Application.Selection.InlineShapes.AddPicture("server.com/Images/img.png");
img.Height = 20;
img.Width = 20;
document.Save();
document.Close();
Basically what my code does is, download the image then add it to the document. What i want to do is that i want the image to be loaded from the server whenever the word document is opened.

Instead of using the Office Interop libraries, you could achieve this using the new OpenXML SDK which does not require MS Office to be installed in order to work.
Requirements
Install the OpenXML NuGet from Visual Studio: DocumentFormat.OpenXml
Add the required namespaces:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Vml;
using DocumentFormat.OpenXml.Wordprocessing;
The code
using (WordprocessingDocument package = WordprocessingDocument.Create(#"c:/temp/img.docx", WordprocessingDocumentType.Document))
{
package.AddMainDocumentPart();
var picture = new Picture();
var shape = new Shape() { Style="width: 272px; height: 92px" };
var imageData = new ImageData() { RelationshipId = "rId1" };
shape.Append(imageData);
picture.Append(shape);
package.MainDocumentPart.Document = new Document(
new Body(
new Paragraph(
new Run(picture))));
package.MainDocumentPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
new System.Uri("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", System.UriKind.Absolute), "rId1");
package.MainDocumentPart.Document.Save();
}
This will create a new Word document that will load the Google logo from the provided URL when open it.
References
https://msdn.microsoft.com/en-us/library/dd440953(v=office.12).aspx
How can I add an external image to a word document using OpenXml?

Related

save as iText.Layout.Element.Image in folder

i want to save pdf page as image but no success. i am able to create iText.Layout.Element.Image from pdf with itext7 but stuck here
using var pdfreader = new PdfReader("../../../documents/valid.pdf");
PdfDocument origPdf = new PdfDocument(pdfreader);
PdfPage origPage = origPdf.GetPage(1);
using var stream = new MemoryStream();
using var pdfwriter = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(pdfwriter);
Document document = new Document(pdf);
PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
Image image = new Image(pageCopy);
// want to save this image
The iText 7 Image class is (according to JavaDocs) a layout element that represents an image for inclusion in the document model. It essentially can wrap arbitrary contents to be added to the contents of some page (or form XObject, ...) in an image like manner. It is not, however, an arbitrary-content-to-bitmap converter.
If you want to render a page as a bitmap using iText 7 components, consider using the iText 7 Core add-on pdfRender.

Insert text and then merge pdf

I'm using IText7 version 7.0.2.2, I'm new with it, I'm trying to merge several pdfs at the same time into one that I'm uploading first, that is working fine, the problem is when I try dynamically to insert some text in one of the pdfs and then merge it, I'm using PdfWriter to write some content into the pdf and then try to merge it, but I'm getting this exception: 'Cannot copy indirect object from the document that is being written.
This is some of the code I'm using:
private byte[] MergePdfForms( HttpPostedFileBase firstPdf, List<SectionAndPdfs> sectionsAndPdf)
{
var dest = new MemoryStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
firstSourcePdf = new PdfDocument(new PdfReader(keyValuePair.Value), new PdfWriter(dest));
Document document = new Document(firstSourcePdf);
document.Add(new Paragraph(sectionsAndPdf[i].Key).SetBackgroundColor(iText.Kernel.Colors.Color.GRAY));
merger.Merge(firstSourcePdf, 1, subPages); //I'm getting the exception here..
firstSourcePdf.Close();
}
This is a known bug in the class PdfDestination. It was fixed, and will be present in our next release. At the moment you can of course use the snapshot release, which should solve the problem.

Footer not showing in iTextSharp v4

I'm trying to create a PDF from HTML, and am using iTextSharp for that. From my reading, the license that covers the newer versions of iTextSharp would require me to make the source code available. We can't do that, so we're using version 4, which is under the LGPL.
I'm trying to get a footer to appear along with the HTML, but it's not working for some reason. I've tried removing the HTML and just using text. Just putting a chunk in the footer. Multiple pages vs single pages. Hopefully I'm just missing something easy, but from the examples I've seen it should be super easy.
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document(PageSize.LETTER, 35,35,35,70);
var font = FontFactory.GetFont("arial", 8f);
font.Color = Color.BLACK;
var chunk = new Chunk("Footer", font);
var phrase = new Phrase(chunk);
var footer = new HeaderFooter(phrase, true);
footer.Alignment = 1;
footer.Border = Rectangle.NO_BORDER;
doc.Footer = footer;
//doc.Footer = new HeaderFooter(new Phrase("Footer"),false);
var writer = PdfWriter.GetInstance(doc, ms);
var htmlWorker = new HTMLWorker(doc);
using (var sr = new StringReader(html))
{
doc.Open();
doc.Add(new Chunk("Text"));
//htmlWorker.Parse(sr);
doc.Close();
}
return ms.ToArray();
}
Watch your HeaderFooter ctor. The signature your are using might lead to setting the header text only.
Anyway maybe use the PdfWriter.PageEvent and some class deriving from PdfPageEventHelper to implement header and footer (instead of HeaderFooter())

C# How to insert a frame into a wordprocessingdocument using the openxml sdk v 2.5

does anybody know how to use frames in wordprocessingdocument using the openxml sdk v 2.5 ? I'm using the openxml sdk v 2.5 to edit docx files in a WPF application. I have several Objects like Tables Text and Images and i want a frame around them, so they won't split at the end of a page.
tried this:
Frame frame = new Frame(xmlElementList);
document.MainDocumentPart.Document.AppendChild(frame);
but after adding the frame to the document the docx file is not valid and cannot be open in word
You can create ParagraphProperties and add it to your Paragraph like this
ParagraphProperties paragraphProperties = new ParagraphProperties
{
KeepNext = new KeepNext(),
FrameProperties = new FrameProperties
{
HorizontalSpace = "141",
Wrap = TextWrappingValues.Around,
HorizontalPosition = HorizontalAnchorValues.Text,
VerticalPosition = VerticalAnchorValues.Text, Y = "1"
}
};
Paragraph paragraph=new Paragraph();
paragragh.ParagraphProperties =paragraphProperties;

How to add hyperlinks into Word docx using open XML?

I am having a trouble adding hyperlinks to my word document. I don't know how to do it. I would like to make a link in a word document from my C# code using open xml. Is ther a different solution using only href or sth similar? There is a HyperLink class on the net from Open XML but how to use it?
Try this
using (WordprocessingDocument doc = WordprocessingDocument.Open("", true))
{
doc.MainDocumentPart.Document.Body.AppendChild(
new Paragraph(
new Hyperlink(new Run(new Text("Click here")))
{
Anchor = "Description",
DocLocation = "location",
}
)
);
doc.MainDocumentPart.Document.Save();
}

Categories

Resources