how to Wrap Text across an image in docx file in c# - c#

I am making a docx generator using Docx.dll. So far i have been able to insert images and text into the document. The images and paragraph are not aligned. I need to wrap text the image. How do i do it?
I looked for it in google and found this link
Adding Images to Documents in Word 2007 by Using the Open XML SDK 2.0. The code is working and creating the word document too, but the docx file is not opening.
How do i wrap text 'In Front Of Text' in c#?
public static DocX CreateDocumentFile(List<CompanyInfo> info)
{
DocX document = DocX.Load(#"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\RetailWrite.docx");
foreach (var companies in info)
{
Formatting fm = new Formatting();
/*Inserting Image*/
Novacode.Image img = document.AddImage(#"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\logos\slime.png");
Novacode.Paragraph companyLogo = document.InsertParagraph("");
Picture pic1 = img.CreatePicture();
companyLogo.InsertPicture(pic1, 0);
Novacode.Paragraph CompanyName = document.InsertParagraph(companies.Name.ToString());
CompanyName.StyleName = "COMPANY";
Novacode.Paragraph CompanyPosition = document.InsertParagraph(companies.Position.ToString());
CompanyPosition.StyleName = "posit";
Novacode.Paragraph CompanyDescription = document.InsertParagraph(companies.Description.ToString());
CompanyDescription.StyleName = "descrip";
Novacode.Paragraph blankPara = document.InsertParagraph(" ");
Novacode.Paragraph blankPara2 = document.InsertParagraph(" ");
}
return document;
}

Solution to the problem: I used the Interop of MS-Word to apply word-wrap across images.
public static void FormatImages()
{
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
string filePath = #"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\Temp.docx";
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(filePath, false);
object save_changes = false;
foreach (Microsoft.Office.Interop.Word.InlineShape item in wordApp.ActiveDocument.InlineShapes)
{
if (item != null)
{
if (item.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
{
item.Select();
Microsoft.Office.Interop.Word.Shape shape = item.ConvertToShape();
shape.WrapFormat.Type = WdWrapType.wdWrapFront;
}
}
}
doc.SaveAs(#"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\RetailWrite.docx");
doc.Close(save_changes);
wordApp.Quit(save_changes);
if (System.IO.File.Exists(#"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\Temp.docx"))
{
System.IO.File.Delete(#"C:\Users\newton.sheikh\Documents\Visual Studio 2010\Projects\MSOffice\OpenXML\OpenXML\Temp.docx");
}
}

Related

How to extract all pages and attachments from PDF to PNG

I am trying to create a process in .NET to convert a PDF and all it's pages + attachments to PNGs. I am evaluating libraries and came across PDFiumSharp but it is not working for me. Here is my code:
string Inputfile = "input.pdf";
string OutputFolder = "Output";
string fileName = Path.GetFileNameWithoutExtension(Inputfile);
using (PdfDocument doc = new PdfDocument(Inputfile))
{
for (int i = 0; i < doc.Pages.Count; i++)
{
var page = doc.Pages[i];
using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, false))
{
page.Render(bitmap);
var targetFile = Path.Combine(OutputFolder, fileName + "_" + i + ".png");
bitmap.Save(targetFile);
}
}
}
When I run this code, I get this exception:
screenshot of exception
Does anyone know how to fix this? Also does PDFiumSharp support extracting PDF attachments? If not, does anyone have any other ideas on how to achieve my goal?
PDFium does not look like it supports extracting PDF attachments. If you want to achieve your goal, then you can take a look at another library that supports both extracting PDF attachments as well as converting PDFs to PNGs.
I am an employee of the LEADTOOLS PDF SDK which you can try out via these 2 nuget packages:
https://www.nuget.org/packages/Leadtools.Pdf/
https://www.nuget.org/packages/Leadtools.Document.Sdk/
Here is some code that will convert a PDF + all attachments in the PDF to separate PNGs in an output directory:
SetLicense();
cache = new FileCache { CacheDirectory = "cache" };
List<LEADDocument> documents = new List<LEADDocument>();
if (!Directory.Exists(OutputDir))
Directory.CreateDirectory(OutputDir);
using var document = DocumentFactory.LoadFromFile("attachments.pdf", new LoadDocumentOptions { Cache = cache, LoadAttachmentsMode = DocumentLoadAttachmentsMode.AsAttachments });
if (document.Pages.Count > 0)
documents.Add(document);
foreach (var attachment in document.Attachments)
documents.Add(document.LoadDocumentAttachment(new LoadAttachmentOptions { AttachmentNumber = attachment.AttachmentNumber }));
ConvertDocuments(documents, RasterImageFormat.Png);
And the ConvertDocuments method:
static void ConvertDocuments(IEnumerable<LEADDocument> documents, RasterImageFormat imageFormat)
{
using var converter = new DocumentConverter();
using var ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
ocrEngine.Startup(null, null, null, null);
converter.SetOcrEngineInstance(ocrEngine, false);
converter.SetDocumentWriterInstance(new DocumentWriter());
foreach (var document in documents)
{
var name = string.IsNullOrEmpty(document.Name) ? "Attachment" : document.Name;
string outputFile = Path.Combine(OutputDir, $"{name}.{RasterCodecs.GetExtension(imageFormat)}");
int count = 1;
while (File.Exists(outputFile))
outputFile = Path.Combine(OutputDir, $"{name}({count++}).{RasterCodecs.GetExtension(imageFormat)}");
var jobData = new DocumentConverterJobData
{
Document = document,
Cache = cache,
DocumentFormat = DocumentFormat.User,
RasterImageFormat = imageFormat,
RasterImageBitsPerPixel = 0,
OutputDocumentFileName = outputFile,
};
var job = converter.Jobs.CreateJob(jobData);
converter.Jobs.RunJob(job);
}
}

Converting word doc to pdf - getting blank cells on the tables(Server side)

So this code down here works fine, it does the job, word -> PDF.
Only got a problem, when the word doc has tables with united cells, when converting the word doc to pdf the pdf show the united cells combine but with no value inside.
Someone knows why? Is the any solution for this?
NOTE: This is converting on server side.
if (nomeDocTemplate == attachment)
{
web.AllowUnsafeUpdates = true;
string wordEdit =item2.Attachments.UrlPrefix + "Template" + item.ID + ".docx";
string wordAutomationServiceName = "Word Automation Services";
string filenamedest = web.Url + item.File.ServerRelativeUrl;
SPFile attachmentFile = item2.Web.GetFile(wordEdit);
using (MemoryStream destinationStream = new MemoryStream())
{
//Call the syncConverter class, passing in the name of the Word Automation Service for your Farm.
SyncConverter sc = new SyncConverter(wordAutomationServiceName);
////Pass in your User Token or credentials under which this conversion job is executed.
sc.UserToken = SPContext.Current.Site.UserToken;
sc.Settings.UpdateFields = true;
//Save format
sc.Settings.OutputFormat = SaveFormat.PDF;
////Convert to PDF by opening the file stream, and then converting to the destination memory stream.
ConversionItemInfo info = sc.Convert(attachmentFile.OpenBinaryStream(), destinationStream);
var filename = Path.GetFileNameWithoutExtension(item.File.Name) + ".pdf";
if (info.Succeeded)
{
//File conversion successful, then add the memory stream to the SharePoint list.
SPFile newfile = web.Lists["Tramitar"].RootFolder.Files.Add(filename, destinationStream, true);
}
else if (info.Failed)
{
throw new Exception(info.ErrorMessage);
}
}
//ConcatAndAddContent(anexosProcesso);
return;
}
Here is the doc and the pdf, to understand better the problem I'm having:
WORD:
PDF:

Openxml - Embedded excel corrupts ppt

Powerpoint shows "Unable to read an object" after I embed the excel into powerpoint file via openxml.
Am using a simple excel with a chart and a pptx created already.
This fails in both .NetCore and .NetFramework
Here is my code.
string PPTFileName = #"c:\tmp\Example04.pptx";
string spreadsheetFileName = #"c:\tmp\demoOut5.xlsx";
using (PresentationDocument myPresDoc = PresentationDocument.Open(PPTFileName, true))
{
SlidePart slidePartBookMark = null;
string chartPartIdBookMark = "";
foreach (var slidePart in myPresDoc.PresentationPart.SlideParts)
{
if (slidePart.ChartParts.Any())
{
slidePartBookMark = slidePart;
var chartPart = slidePart.ChartParts.First();
chartPartIdBookMark = slidePart.GetIdOfPart(chartPart);
slidePart.DeletePart(chartPart);
slidePart.Slide.Save();
break;
}
}
var newChartPart = slidePartBookMark.AddNewPart<ChartPart>(chartPartIdBookMark);
EmbeddedPackagePart embPackage = newChartPart
.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "rId1");
embPackage.FeedData(new FileStream(#"c:\tmp\demoOut4.xlsx", FileMode.Open, FileAccess.Read));
DocumentFormat.OpenXml.Drawing.Charts.ExternalData ed = new DocumentFormat.OpenXml.Drawing.Charts.ExternalData();
ed.Id = "rId1";
slidePartBookMark.Slide.Save();
myPresDoc.Close();
Console.WriteLine("Done");
Console.ReadKey();
}
Any help or direction would be appreciated. Thanks.

C# OpenXML How to Replace \r\n with Break()?

I have a text field in my database and it has a text with many lines.
When generating a MS Word document using OpenXML and bookmarks, the text become one single line.
I've noticed that in each new line the bookmark value show the characters "\r\n".
Looking for a solution, I've found some answers which helped me, but I'm still having a problem.
I've used the run.Append(new Break()); solution, but the text replaced is showing the name of the bookmark as well.
For example:
bookmark test = "Big text here in first paragraph\r\nSecond paragraph".
It is shown in MS Word document like:
testBig text here in first paragraph
Second paragraph
Can anyone, please, help me to eliminate the bookmark name?
Here is my code:
public void UpdateBookmarksVistoria(string originalPath, string copyPath, string fileType)
{
string wordmlNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
// Make a copy of the template file.
File.Copy(originalPath, copyPath, true);
//Open the document as an Open XML package and extract the main document part.
using (WordprocessingDocument wordPackage = WordprocessingDocument.Open(copyPath, true))
{
MainDocumentPart part = wordPackage.MainDocumentPart;
//Setup the namespace manager so you can perform XPath queries
//to search for bookmarks in the part.
NameTable nt = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
nsManager.AddNamespace("w", wordmlNamespace);
//Load the part's XML into an XmlDocument instance.
XmlDocument xmlDoc = new XmlDocument(nt);
xmlDoc.Load(part.GetStream());
//pega a url para exibir as fotos
string url = HttpContext.Current.Request.Url.ToString();
string enderecoURL;
if (url.Contains("localhost"))
enderecoURL = url.Substring(0, 26);
else if (url.Contains("www."))
enderecoURL = url.Substring(0, 24);
else
enderecoURL = url.Substring(0, 20);
//Iterate through the bookmarks.
int cont = 56;
foreach (KeyValuePair<string, string> bookmark in bookmarks)
{
var res = from bm in part.Document.Body.Descendants<BookmarkStart>()
where bm.Name == bookmark.Key
select bm;
var bk = res.SingleOrDefault();
if (bk != null)
{
Run bookmarkText = bk.NextSibling<Run>();
if (bookmarkText != null) // if the bookmark has text replace it
{
var texts = bookmark.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < texts.Length; i++)
{
if (i > 0)
bookmarkText.Append(new Break());
Text text = new Text();
text.Text = texts[i];
bookmarkText.Append(text); //HERE IS MY PROBLEM
}
}
else // otherwise append new text immediately after it
{
var parent = bk.Parent; // bookmark's parent element
Text text = new Text(bookmark.Value);
Run run = new Run(new RunProperties());
run.Append(text);
// insert after bookmark parent
parent.Append(run);
}
bk.Remove(); // we don't want the bookmark anymore
}
}
//Write the changes back to the document part.
xmlDoc.Save(wordPackage.MainDocumentPart.GetStream(FileMode.Create));
wordPackage.Close();
}}

Change word document body font and Footnotes font

I have MS Word document how can I change it body font to "Arial" and Footnotes font to "Times New Roman" using C# , (Interop.Word or any free library).
I Did Search a lot before posting this Question.. But find nothing.
I found this Question but it dose not really help
How to search for a specific font in a Word document with iterop
This is some sample code. I have set the font for both body text and footnote text. The code reads "test.doc" from C drive.
using System;
using Microsoft.Office.Interop.Word;
namespace Word
{
class Program
{
static void Main(string[] args)
{
Application wordApp = new Application();
string filename = #"C:\test.doc";
Document myDoc = wordApp.Documents.Open(filename);
if (myDoc.Paragraphs.Count > 0)
{
foreach (Paragraph p in myDoc.Paragraphs)
{
p.Range.Font.Name = "Calibri";
p.Range.Text = "I have changed this text I entered previously";
}
}
if (myDoc.Footnotes.Count > 0)
{
foreach (Footnote fn in myDoc.Footnotes)
{
fn.Range.Font.Name = "Arial";
}
}
myDoc.Save();
myDoc.Close();
myDoc = null;
wordApp.Quit();
wordApp = null;
}
}
}
If you are looking for MSDN documentation on using Word Interop then this is the link.

Categories