How to insert Audio file in Word doc Using OpenXml - c#

I'm trying to insert audio file in Word doc using OpenXml. I've implemented my code but no audio file added after executing my Code. I take help OPEN XML SDK Tool 2.5 also. Code seems okay, but no audio inserted.Below is my tried code:
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Emf);
using (var imageStream = new ExportUtils().GetBinaryDataStream())
{
imagePart.FeedData(imageStream);
}
EmbeddedObjectPart embeddedObjectPart = mainPart.AddEmbeddedObjectPart("application/vnd.openxmlformats-officedocument.oleObject");
thumPath = GetMyFilePath(); //Audio file path
using (var objStream = new FileStream(thumPath, FileMode.Open))
{
embeddedObjectPart.FeedData(objStream);
}
AddAudio(wordprocessingDocument, mainPart.GetIdOfPart(imagePart), mainPart.GetIdOfPart(embeddedObjectPart), posX, posY);
Audio Insertion Code
private static void AddAudio(WordprocessingDocument wordDoc, string imgRelationshipId, string objRelationshipId, int x, int y)
{
string style = string.Format("position: absolute; margin - left:{0}pt; margin - top:{1}pt; width: 75.5pt; height: 49pt; z - index:-251657216; mso - position - horizontal - relative:text; mso - position - vertical - relative:text", x, y);
ow.EmbeddedObject embeddedObject = new ow.EmbeddedObject() { DxaOriginal = "1440", DyaOriginal = "1440", AnchorId = "6359C433" };
V.Shape shape = new V.Shape() { Id = "_x0000_s1026", Style = style };
V.ImageData imageData = new V.ImageData() { Title = "Voice", RelationshipId = imgRelationshipId };
shape.Append(imageData);
Ovml.OleObject oleObject = new Ovml.OleObject() { Type = Ovml.OleValues.Embed, ProgId = "Package", ShapeId = "_x0000_s1026", DrawAspect = Ovml.OleDrawAspectValues.Icon, ObjectId = "_1647627410", Id = objRelationshipId };
embeddedObject.Append(shape);
embeddedObject.Append(oleObject);
wordDoc.MainDocumentPart.Document.Body.AppendChild(new ow.Paragraph(new ow.Run(embeddedObject)));
}
My project: x64

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);
}
}

Openxml merging docx with images

In short: I would like to insert the content of a docx that contains images and bullets in another docx.
My problem: I used two approaches:
Manual merge
Altchunk
With both of them I got a corrupted word document as result.
If I remove the images from the docx that I would like to insert in another one, the result docx is OK.
My code:
Manual merge (thanks to https://stackoverflow.com/a/48870385/10075827):
private static void ManualMerge(string firstPath, string secondPath, string resultPath)
{
if (!System.IO.Path.GetFileName(firstPath).StartsWith("~$"))
{
File.Copy(firstPath, resultPath, true);
using (WordprocessingDocument result = WordprocessingDocument.Open(resultPath, true))
{
using (WordprocessingDocument secondDoc = WordprocessingDocument.Open(secondPath, false))
{
OpenXmlElement p = result.MainDocumentPart.Document.Body.Descendants<Paragraph>().Last();
foreach (var e in secondDoc.MainDocumentPart.Document.Body.Elements())
{
var clonedElement = e.CloneNode(true);
clonedElement.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().ToList().ForEach(blip =>
{
var newRelation = result.CopyImage(blip.Embed, secondDoc);
blip.Embed = newRelation;
});
clonedElement.Descendants<DocumentFormat.OpenXml.Vml.ImageData>().ToList().ForEach(imageData =>
{
var newRelation = result.CopyImage(imageData.RelationshipId, secondDoc);
imageData.RelationshipId = newRelation;
});
result.MainDocumentPart.Document.Body.Descendants<Paragraph>().Last();
if (clonedElement is Paragraph)
{
p.InsertAfterSelf(clonedElement);
p = clonedElement;
}
}
}
}
}
}
public static string CopyImage(this WordprocessingDocument newDoc, string relId, WordprocessingDocument org)
{
var p = org.MainDocumentPart.GetPartById(relId) as ImagePart;
var newPart = newDoc.MainDocumentPart.AddPart(p);
newPart.FeedData(p.GetStream());
return newDoc.MainDocumentPart.GetIdOfPart(newPart);
}
Altchunk merge (from http://www.karthikscorner.com/sharepoint/use-altchunk-document-assembly/):
private static void AltchunkMerge(string firstPath, string secondPath, string resultPath)
{
WordprocessingDocument mainDocument = null;
MainDocumentPart mainPart = null;
var ms = new MemoryStream();
#region Prepare - consuming application
byte[] bytes = File.ReadAllBytes(firstPath);
ms.Write(bytes, 0, bytes.Length);
mainDocument = WordprocessingDocument.Open(ms, true);
mainPart = mainDocument.MainDocumentPart;
#endregion
#region Document to be imported
FileStream fileStream = new FileStream(secondPath, FileMode.Open);
#endregion
#region Merge
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, "AltChunkId101");
chunk.FeedData(fileStream);
var altChunk = new AltChunk(new AltChunkProperties() { MatchSource = new MatchSource() { Val = new OnOffValue(true) } });
altChunk.Id = "AltChunkId101";
mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
mainPart.Document.Save();
#endregion
#region Mark dirty
var listOfFieldChar = mainPart.Document.Body.Descendants<FieldChar>();
foreach (FieldChar current in listOfFieldChar)
{
if (string.Compare(current.FieldCharType, "begin", true) == 0)
{
current.Dirty = new OnOffValue(true);
}
}
#endregion
#region Save Merged Document
mainPart.DocumentSettingsPart.Settings.PrependChild(new UpdateFieldsOnOpen() { Val = new OnOffValue(true) });
mainDocument.Close();
FileStream file = new FileStream(resultPath, FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
ms.Close();
#endregion
}
I spent hours searching for a solution and the most common one I found was to use altchunk. So why is it not working in my case?
If you are able to use the Microsoft.Office.Interop.Word namespace, and able to put a bookmark in the file you want to merge into, you can take this approach:
using Microsoft.Office.Interop.Word;
...
// merge by putting second file into bookmark in first file
private static void NewMerge(string firstPath, string secondPath, string resultPath, string firstBookmark)
{
var app = new Application();
var firstDoc = app.Documents.Open(firstPath);
var bookmarkRange = firstDoc.Bookmarks[firstBookmark];
// Collapse the range to the end, as to not overwrite it. Unsure if you need this
bookmarkRange.Collapse(WdCollapseDirection.wdCollapseEnd);
// Insert into the selected range
// use if relative path
bookmarkRange.InsertFile(Environment.CurrentDirectory + secondPath);
// use if absolute path
//bookmarkRange.InsertFile(secondPath);
}
Related:
C#: Insert and indent bullet points at bookmark in word document using Office Interop libraries

How can I add a header and footer to the generated PDF

I'm using Gmanny's Pechkin Pdf library and it's working perfectly. Here's is my code:
private void CreatePdfPechkin(string htmlString, string fileName)
{
//Transform the HTML into PDF
var pechkin = Factory.Create(new GlobalConfig()
.SetMargins(new Margins(100, 50, 100, 100))
.SetDocumentTitle("Test document")
.SetPaperSize(PaperKind.A4)
.SetCopyCount(1)
//.SetPaperOrientation(true)
// .SetOutputFile("F:/Personal/test.pdf")
);
ObjectConfig oc = new ObjectConfig();
oc.Footer.SetLeftText("[page]");
oc.Footer.SetTexts("[page]", "[date]", "[time]");
oc.Header.SetCenterText("TEST HEADER TEST1");
oc.Header.SetHtmlContent("<h1>TEST HEADER V2</h1>");
oc.SetAllowLocalContent(true);
//// create converter
//IPechkin ipechkin = new SynchronizedPechkin(pechkin);
// set it up using fluent notation
var pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1.5)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true), htmlString);
//Return the PDF file
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename=test.pdf; size={0}", pdf.Length));
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
// byte[] pdf = new Pechkin.Synchronized.SynchronizedPechkin(
//new Pechkin.GlobalConfig()).Convert(
// new Pechkin.ObjectConfig()
// .SetLoadImages(true)
// .SetPrintBackground(true)
// .SetScreenMediaType(true)
// .SetCreateExternalLinks(true), htmlString);
// using (FileStream file = System.IO.File.Create(#"F:\Pankaj WorkSpace\"+ fileName))
// {
// file.Write(pdf, 0, pdf.Length);
// }
}
But now I want to add header, footer and page Number, can somebody suggest how to do that?
I know it is possible through Object config I tried but its not working..
Headers and footers using the below very basic examples work for me.
Using Pechkin:
GlobalConfig gc = new GlobalConfig();
gc.SetMargins(new Margins(300, 100, 150, 100))
.SetDocumentTitle("Test document")
.SetPaperSize(PaperKind.Letter);
IPechkin pechkin = new SynchronizedPechkin(gc);
ObjectConfig oc = new ObjectConfig();
oc.SetCreateExternalLinks(false);
oc.SetFallbackEncoding(Encoding.ASCII);
oc.SetLoadImages(false);
oc.Footer.SetCenterText("I'm a footer!");
oc.Footer.SetLeftText("[page]");
oc.Header.SetCenterText("I'm a header!");
byte[] result = pechkin.Convert(oc, "<h1>My Website</h1>");
System.IO.File.WriteAllBytes(#"c:\pechkinTest.pdf", result);
I'd recommend you switch to Tuespechkin though. This is an active fork of Pechkin which includes many bugfixes. Unfortunately active development on Pechkin ceased since 2013.
Using Tuespechkin:
var document = new HtmlToPdfDocument
{
GlobalSettings =
{
ProduceOutline = true,
DocumentTitle = "My Website",
PaperSize = PaperKind.A4,
Margins =
{
All = 1.375,
Unit = Unit.Centimeters
}
},
Objects = {
new ObjectSettings
{
HtmlText = "<h1>My Website</h1>",
HeaderSettings = new HeaderSettings{CenterText = "I'm a header!"},
FooterSettings = new FooterSettings{CenterText = "I'm a footer!", LeftText = "[page]"}
}
}
};
IPechkin converter = Factory.Create();
byte[] result = converter.Convert(document);
System.IO.File.WriteAllBytes(#"c:\tuespechkinTest.pdf", result);
Your issue is this. Instead of creating a new second ObjectConfig, you need to pass the OC you created before which includes the header and footer. Just combine them like this:
ObjectConfig oc = new ObjectConfig();
oc.SetLoadImages(true);
oc.SetZoomFactor(1.5);
oc.SetPrintBackground(true);
oc.SetScreenMediaType(true);
oc.SetCreateExternalLinks(true);
oc.Footer.SetLeftText("[page]");
oc.Footer.SetCenterText("I'm a footer!");
oc.Header.SetCenterText("TEST HEADER TEST1");
var result = pechkin.Convert(oc, "<h1>My Website</h1>");
System.IO.File.WriteAllBytes(#"c:\pechkinTest.pdf", result);

How to import a 3D object from one PDF to another using iTextSharp

I'm using iTextSharp 5.5 to construct PDF documents. The documents start with text information and end with imported JPEG images and multi-page PDF files. Some of the PDFs contain annotations, specifically 3D models.
Edit (3/21/2014): Here is a complete, simplified example that illustrates what I'm trying to accomplish, and where the error occurs in AddPdf().
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace PdfTest
{
class Program
{
private const string path = "w:\\tmp\\pdf";
static void Main(string[] args)
{
using (var ms = new MemoryStream())
{
var document = new Document(PageSize.LETTER, 40, 40, 30, 30);
var writer = PdfWriter.GetInstance(document, ms); // Without this, I get a zero-length file
document.Open();
AddText(document, "TEST");
AddImage(document, Path.Combine(path, "import1.jpg"));
AddPdf(document, ms, Path.Combine(path, "import2.pdf"));
document.Close();
File.WriteAllBytes(Path.Combine(path, "test.pdf"), ms.ToArray());
}
}
private static void AddText(Document document, string text)
{
document.Add(new Paragraph(text, FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12f)));
}
private static void AddImage(Document document, string sourcePath)
{
var pic = Image.GetInstance(sourcePath);
var maxWidth = document.PageSize.Width - 72f;
var maxHeight = document.PageSize.Height - 150f;
if (pic.Width > maxWidth || pic.Height > maxHeight)
{
pic.ScaleToFit(maxWidth, maxHeight);
}
document.NewPage();
document.Add(pic);
}
private static void AddPdf(Document document, Stream stream, string sourcePath)
{
var copy = new PdfCopy(document, stream);
// Read the source PDF
var reader = new PdfReader(sourcePath);
var pageCount = reader.NumberOfPages;
// Import each page
for (var i = 0; i < pageCount; i++)
{
var pageNum = i + 1;
document.SetPageSize(reader.GetPageSizeWithRotation(pageNum));
document.NewPage(); // <--- "Document is not open" error here
var page = copy.GetImportedPage(reader, pageNum);
copy.AddPage(page);
}
}
}
}
What is the correct way to construct a document by adding elements and imported pages?

Images are not coming after creating word Document through Open XML

I am Creating a word document through the c# with the use of OpenXMl sdk.
I am converting all my html page to word document but while converting i am giving a absolute address for my images and after converting it is coming perfectly in my system but when i am trying to take this document to other system the Images are Not Coming there.
I checked the media Directory all images are there but with different Name.
my document is converted but I am Using this mathod.
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentPath, true))
{
XNamespace w =
"http://schemas.openxmlformats.org/wordprocessingml/2006/main";
XNamespace r =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships";
string altChunkId = "AltChunkId1";
MainDocumentPart mainPart = myDoc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart("application/xhtml+xml", altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (StreamWriter stringStream = new StreamWriter(chunkStream))
stringStream.Write(html);
XElement altChunk = new XElement(w + "altChunk",
new XAttribute(r + "id", altChunkId)
);
XDocument mainDocumentXDoc = GetXDocument(myDoc);
mainDocumentXDoc.Root
.Element(w + "body")
.Elements(w + "p")
.Last()
.AddAfterSelf(altChunk);
SaveXDocument(myDoc, mainDocumentXDoc);
}
private static XDocument GetXDocument(WordprocessingDocument myDoc)
{
// Load the main document part into an XDocument
XDocument mainDocumentXDoc;
using (Stream str = myDoc.MainDocumentPart.GetStream())
using (XmlReader xr = XmlReader.Create(str))
mainDocumentXDoc = XDocument.Load(xr);
return mainDocumentXDoc;
}
private static void SaveXDocument(WordprocessingDocument myDoc,
XDocument mainDocumentXDoc)
{
// Serialize the XDocument back into the part
using (Stream str = myDoc.MainDocumentPart.GetStream(
FileMode.Create, FileAccess.Write))
using (XmlWriter xw = XmlWriter.Create(str))
mainDocumentXDoc.Save(xw);
}
and this will generate a afchunk.dat file which is showing in the content and the Absolute path.
Basically i doesn't want to create a file through all coding i just want to convert the .html to .docx file .
so can any one tell me how can i convert without getting error in html.
Is there a reason you aren't embedding the images? Here's a link with sample code to show you how.
http://msdn.microsoft.com/en-us/library/bb497430.aspx
Try to create a DocumentResource (Item->Add new) and associate the images there.
Call the Document
using (Stream imgStream = ip.GetStream())
{
System.Drawing.Bitmap logo = DocumentResources._default;
logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
Drawing drawing = BuildImage(imageRelationshipID, "_default.jpg", 200, 30);
And create the method to build image in the header or footer;
private static Drawing BuildImage(string imageRelationshipID, string imageName, int pixelWidth, int pixelHeight)
{
int emuWidth = (int)(pixelWidth * EMU_PER_PIXEL);
int emuHeight = (int)(pixelHeight * EMU_PER_PIXEL);
Drawing drawing = new Drawing();
d.Wordprocessing.Inline inline = new d.Wordprocessing.Inline { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 };
d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor();
d.Wordprocessing.SimplePosition simplePos = new d.Wordprocessing.SimplePosition { X = 0, Y = 0 };
d.Wordprocessing.Extent extent = new d.Wordprocessing.Extent { Cx = emuWidth, Cy = emuHeight };
d.Wordprocessing.DocProperties docPr = new d.Wordprocessing.DocProperties { Id = 1, Name = imageName };
d.Graphic graphic = new d.Graphic();
d.GraphicData graphicData = new d.GraphicData { Uri = GRAPHIC_DATA_URI };
d.Pictures.Picture pic = new d.Pictures.Picture();
d.Pictures.NonVisualPictureProperties nvPicPr = new d.Pictures.NonVisualPictureProperties();
d.Pictures.NonVisualDrawingProperties cNvPr = new d.Pictures.NonVisualDrawingProperties { Id = 2, Name = imageName };
d.Pictures.NonVisualPictureDrawingProperties cNvPicPr = new d.Pictures.NonVisualPictureDrawingProperties();
d.Pictures.BlipFill blipFill = new d.Pictures.BlipFill();
d.Blip blip = new d.Blip { Embed = imageRelationshipID };
d.Stretch stretch = new d.Stretch();
d.FillRectangle fillRect = new d.FillRectangle();
d.Pictures.ShapeProperties spPr = new d.Pictures.ShapeProperties();
d.Transform2D xfrm = new d.Transform2D();
d.Offset off = new d.Offset { X = 0, Y = 0 };
d.Extents ext = new d.Extents { Cx = emuWidth, Cy = emuHeight };
d.PresetGeometry prstGeom = new d.PresetGeometry { Preset = d.ShapeTypeValues.Rectangle };
d.AdjustValueList avLst = new d.AdjustValueList();
xfrm.Append(off);
xfrm.Append(ext);
prstGeom.Append(avLst);
stretch.Append(fillRect);
spPr.Append(xfrm);
spPr.Append(prstGeom);
blipFill.Append(blip);
blipFill.Append(stretch);
nvPicPr.Append(cNvPr);
nvPicPr.Append(cNvPicPr);
pic.Append(nvPicPr);
pic.Append(blipFill);
pic.Append(spPr);
graphicData.Append(pic);
graphic.Append(graphicData);
inline.Append(extent);
inline.Append(docPr);
inline.Append(graphic);
drawing.Append(inline);
return drawing;
}

Categories