I am trying to set the page margins on an excel spreadsheet that is being generated by open xml sdk. I am not opening an excel document that already exists, it is generated from scratch. I am using the PageMargins class but am not sure how to attach this instance to the worksheet. The SDK productivity tool gives this code:
PageMargins pageMargins1 = worksheet.GetFirstChild<PageMargins>();
pageMargins1.Left = 0.45D;
pageMargins1.Right = 0.45D;
pageMargins1.Top = 0.5D;
pageMargins1.Bottom = 0.5D;
The GetFirstChild() function returns null. I also tried to do
worksheet.Append(pageMargins1);
but no luck.
Also using the code from this example: How change excel 2007 document orientation to landscape by OpenXML sdk
to set the page orientation does not work if creating the document from scratch. How do you add a PageSetup & PageMargin instance to the document?
Any one have knowledge of this SDK and knows how to use the margins or page setup class?
The order that things are appended to the worksheet is important. PageMargins needs to be appended before PageSetup, and they both need to be at the end after the SheetData. Also, all settings need to be set. I used this code:
PageMargins pageMargins1 = new PageMargins();
pageMargins1.Left = 0.45D;
pageMargins1.Right = 0.45D;
pageMargins1.Top = 0.5D;
pageMargins1.Bottom = 0.5D;
pageMargins1.Header = 0.3D;
pageMargins1.Footer = 0.3D;
worksheetPart.Worksheet.AppendChild(pageMargins1);
PageSetup pageSetup = new PageSetup();
pageSetup.Orientation = OrientationValues.Landscape;
pageSetup.FitToHeight = 2;
pageSetup.HorizontalDpi = 200;
pageSetup.VerticalDpi = 200;
worksheetPart.Worksheet.AppendChild(pageSetup);
A neat trick to be used along with the sdk productivity tool is to rename the .xlsx file to .zip, then extract the contents. Then open /xl/worksheets/sheet.xml and compare the markup to the markup of an excel file created by excel.
Related
After struggling whole day, I identified the issue but this didn't solve my problem.
On short:
I need to open a PDF, convert to BW (grayscale), search some words and insert some notes nearby found words. At a first look it seems easy but I discovered how hard PDF files are processed (having no "words" concepts and so on).
Now the first task, converting to grayscale just drove me crazy. I didn't find a working solution either commercial or free. I came up with this solution:
open the PDF
print with windows drivers, some free PDF printers
This is quite ugly since I will force the C# users to install such 3'rd party SW but.. that is fpr the moment. I tested FreePDF, CutePDF and PDFCreator. All of them are working "stand alone" as expected.
Now when I tried to print from C#, obviously, I don't want the print dialog, just select BW option and print (aka. convert)
The following code just uses a PDF library, shown for clarity only.
Aspose.Pdf.Facades.PdfViewer viewer = new Aspose.Pdf.Facades.PdfViewer();
viewer.BindPdf(txtPDF.Text);
viewer.PrintAsGrayscale = true;
//viewer.RenderingOptions = new RenderingOptions { UseNewImagingEngine = true };
//Set attributes for printing
//viewer.AutoResize = true; //Print the file with adjusted size
//viewer.AutoRotate = true; //Print the file with adjusted rotation
viewer.PrintPageDialog = true; //Do not produce the page number dialog when printing
////PrinterJob printJob = PrinterJob.getPrinterJob();
//Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
//System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument();
//prtdoc.PrinterSettings = ps;
//Set printer name
//ps.PrinterName = prtdoc.PrinterSettings.PrinterName;
ps.PrinterName = "CutePDF Writer";
ps.PrintToFile = true;
ps.PrintFileName = #"test.pdf";
//
//ps.
//Set PageSize (if required)
//pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
//Set PageMargins (if required)
//pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
//Print document using printer and page settings
viewer.PrintDocumentWithSettings(ps);
//viewer.PrintDocument();
//Close the PDF file after priting
What I discovered and seems to be little explained, is that if you select
ps.PrintToFile = true;
no matter C# PDF library or PDF printer driver, Windows will just skip the PDF drivers and instead of PDF files will output PS (postscript) ones which obviously, will not be recognized by Adobe Reader.
Now the question (and I am positive that others who may want to print PDFs from C# may be encountered) is how to print to CutePDF for example and still suppress any filename dialog?
In other words, just print silently with programmatically selected filename from C# application. Or somehow convince "print to file" to go through PDF driver, not Windows default PS driver.
Thanks very much for any hints.
I solved conversion to grayscale with a commercial component with this post and I also posted there my complete solution, in care anyone will struggle like me.
Converting PDF to Grayscale pdf using ABC PDF
I have a Microsoft word 2010 add-in project in visual studio, I just followed the MSDN guide to making a new tab with custom functionality on the ribbon. I've done some googling, but I cant seem to find any examples (or if its even possible) to use the C# to find a bookmark, then use the bookmarks name in an SQL query and populate it. The documents I am working with can have dozens of bookmarks, and there are hundreds of documents. Automating this process is a high priority.
So basically if you want to automate word documents (building word document templates via word bookmarks) Here's how I normally go about doing it.
Copy The Template
Work On The Template
Save In Desired Format
Delete Template Copy
Each of the sections that you are replacing within your word document you have to insert a bookmark for that location (easiest way to input text in an area).
I always create a function to accomplish this, and I end up passing in the path - as well as all of the text to replace my in-document bookmarks. The function call can get long sometimes, but it works for me.
Application app = new Application();
Document doc = app.Documents.Open("sDocumentCopyPath.docx");
if (doc.Bookmarks.Exists("bookmark_1"))
{
object oBookMark = "bookmark_1";
doc.Bookmarks.get_Item(ref oBookMark).Range.Text = My Text To Replace bookmark_1;
}
if (doc.Bookmarks.Exists("bookmark_2"))
{
object oBookMark = "bookmark_2";
doc.Bookmarks.get_Item(ref oBookMark).Range.Text = My Text To Replace bookmark_2;
}
doc.ExportAsFixedFormat("myNewPdf.pdf", WdExportFormat.wdExportFormatPDF);
((_Document)doc).Close();
((_Application)app).Quit();
This code should get you up and running unless you want to pass in all the values into a function.
Sometimes if you have large amounts of fields you can build objects/classes to contain the values.
If you need more examples I'm working on a blog post as well, so I have a lot more detail if this wasn't clear enough for your use case.
You can use Spire.Doc or FreeSpire.Doc library for this purpose. I have a github repo, that I showed an example how to working it.
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Configuration;
using System.Drawing;
using System.IO;
namespace WorkingDocAndPdf
{
class Program
{
static void Main(string[] args)
{
var sourceFilePath = ConfigurationManager.AppSettings["SourceFilePath"];
var saveFilePath = ConfigurationManager.AppSettings["SaveFilePath"];
var document = new Document(sourceFilePath);
var bookmarksNavigator = new BookmarksNavigator(document);
bookmarksNavigator.MoveToBookmark("client_name");
bookmarksNavigator.ReplaceBookmarkContent("Ramil", true);
bookmarksNavigator.MoveToBookmark("client_taxno");
bookmarksNavigator.ReplaceBookmarkContent("VN-12300254178XY6", true);
bookmarksNavigator.MoveToBookmark("amount");
bookmarksNavigator.ReplaceBookmarkContent("871 AZN", true);
bookmarksNavigator.MoveToBookmark("date");
bookmarksNavigator.ReplaceBookmarkContent(DateTime.Now.ToString("dd.MM.yyyy"), true);
//It is for picture
var sealPath = ConfigurationManager.AppSettings["SealPath"];
bookmarksNavigator.MoveToBookmark("seal", true, true);
var section = document.AddSection();
var image = Image.FromFile(sealPath);
var paragraph = section.AddParagraph();
paragraph.AppendPicture(image);
bookmarksNavigator.InsertParagraph(paragraph);
document.Sections.Remove(section);
if (!Directory.Exists(saveFilePath))
Directory.CreateDirectory(saveFilePath);
var saveFileFullPath = $"{saveFilePath}\\{Guid.NewGuid()}.pdf";
//It is for refresh cross reference bookmark, that you can use one bookmark on different location in document. In word shortcut it is `CTRL A + F9`
document.IsUpdateFields = true;
document.SaveToFile(saveFileFullPath, FileFormat.PDF);
}
}
}
For more information, you can visit my github repo: WorkingDocAndPdf_FreeSpireDoc
My article about FreeSpire.Doc (but written in Azerbaijani): C# working Word and PDF files. Print forms
Is there any way to set the page break in EXCEL file using NPOI in C#..?
I have follow the solution on this link, but still nothing happend on my generated excel files.
I'm using NPOI 2.0.1.0.
var doc = new XWPFDocument();
var pageBreak = doc.CreateParagraph();
var pageBreakRun = pageBreak.CreateRun();
pageBreakRun.AddBreak(BreakType.PAGE);
I am opening existing .docx files from a SharePoint Document Library over the SharePoint web services, and am attempting to attach a new Template to them. The current code for this piece seems to not be doing anything at all.
XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
OpenXmlPart documentSettingsPart = document.MainDocumentPart.DocumentSettingsPart;
XDocument documentSettingsXDoc = documentSettingsPart.GetXDocument();
documentSettingsPart.AddExternalRelationship("http://schemas.openxmlformats/org/officeDocument/2006/relationships/attachedTemplate", new Uri(_outLibraryTemplate, UriKind.Absolute));
using (XmlWriter xw = XmlWriter.Create(documentSettingsPart.GetStream(FileMode.Create, FileAccess.Write)))
documentSettingsXDoc.Save(xw);
Does anyone have any thoughts as to why this isn't working - and what I need to do to get this going?
This may help. It creates a new docx file from a dotx file.
I modified it a little for my own use - I added the external relationship (a dotm) to an existing file. Unfortunately I can't work out yet if I can easily programatically update the styles without having to actually open the file.
https://web.archive.org/web/20150716111136/http://blogs.msdn.com/b/vsod/archive/2012/02/18/how-to-create-a-document-from-a-template-dotx-dotm-and-attach-to-it-using-open-xml-sdk.aspx
I am trying to use C# and Open XML to insert an image from a url into a doc. The image may change so I don't want to download it, I want it to remain an external reference.
I've found several examples like this one that allow me to add a local image:
http://msdn.microsoft.com/en-us/library/bb497430.aspx
How can I adapt that to take a URI? Or is there another approach altogether?
You can add an external image to an word document via a quick parts field.
For a description please see the following answer on superuser.
To realize the described steps programmatically you have to
use an external releationship to include an image from an URL.
Here are the steps to accomplish this:
Create an instance of the Picture class.
Add a Shape to specify the style of the picture (width/height).
Use the ImageData class to specify the ID of the external releationship.
Add an external releationship to the main document part. Give the external
releationship the same ID you specified in step 3.
The following code just implements the steps described above. The image is added to the
first paragraph in the word document.
using (WordprocessingDocument newDoc = WordprocessingDocument.Open(#"c:\temp\external_img.docx", true))
{
var run = new Run();
var picture = new Picture();
var shape = new Shape() { Id = "_x0000_i1025", Style = "width:453.5pt;height:270.8pt" };
var imageData = new ImageData() { RelationshipId = "rId56" };
shape.Append(imageData);
picture.Append(shape);
run.Append(picture);
var paragraph = newdoc.MainDocumentPart.Document.Body.Elements<Paragraph>().FirstOrDefault();
paragraph.Append(run);
newDoc.MainDocumentPart.AddExternalRelationship(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
new System.Uri("<url to your picture>", System.UriKind.Absolute), "rId56");
}
In the code above I've omitted the code to define the shape type. I advise you to use a
tool like the OpenXML SDK productivity tool
to inspect a word document with an external releationship to an image.